天天看点

Android动画效果之渐入渐出

MainActivity.java

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.AnimationUtils;

public class MainActivity extends Activity {

        private View head;

        private View content;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_main);

        head = findViewById(R.id.head);

        content = findViewById(R.id.content);

        content.setOnClickListener(listener);

    }

    private boolean k = true;

    private OnClickListener listener = new OnClickListener() {

                @Override

                public void onClick(View v) {

                        // TODO Auto-generated method stub

                        if (k) {

                                showHead(head, content);

                                setTitle("显示head");

                        } else {

                                hideHead(head, content);

                                setTitle("隐藏head");

                        }

                        k = !k;

                }

        };

        private void showHead(View head, View content) {

            head.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_top));

            content.startAnimation(AnimationUtils.loadAnimation(this, R.anim.content_down));

            content.setPadding(0, 200, 0, 0);

    }

        private void hideHead(View head, View content) {

                content.setPadding(0, 0, 0, 0);

                head.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_top));

                content.startAnimation(AnimationUtils.loadAnimation(this, R.anim.content_up));

    }

}

content_down.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/decelerate_interpolator"

    >

    <translate

        android:duration="5000"

        android:fromYDelta="-200"

        android:toYDelta="0" />

</set>

content_up.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/decelerate_interpolator"

    >

    <translate

        android:duration="5000"

        android:fromYDelta="+200"

        android:toYDelta="0" />

</set>

slide_in_top.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/decelerate_interpolator"

    >

    <translate

        android:duration="5000"

        android:fromYDelta="+200"

        android:toYDelta="0" />

</set>

slide_out_top.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/decelerate_interpolator"

    >

    <translate

        android:duration="5000"

        android:fromYDelta="-200"

        android:toYDelta="0" />

</set>

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="200dp"

        android:background="#33ff33"

        android:id="@+id/head"

        />

    <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:id="@+id/content">

            <TextView

                android:layout_width="fill_parent"

                android:layout_height="fill_parent"

                android:background="#aaaaaa"

                />

    </LinearLayout>

    <TextView 

        android:layout_width="fill_parent"

        android:layout_height="200dp"

        android:background="#3355ff"

        android:id="@+id/footer"

        android:text="I am footer"

        android:layout_alignParentBottom="true"

        />

</RelativeLayout>