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;
/**
 * 
 * @author jayzhou215@163.com
 * @time 2012/5/25
 */
public class MainActivity extends Activity {


        private View head;
        private View content;
        /** Called when the activity is first created. */
    @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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值