android基础--屏幕切换动画

 

要求触摸滑动屏幕实现翻页,从左往右,从右往左的动画效果。

界面main.xml

<?xml version="1.0" encoding="utf-8"?>

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

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <ViewFlipper  

        android:layout_width="fill_parent" 

        android:layout_height="fill_parent" 

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

        <!-- 第一页 -->

        <LinearLayout 

        android:orientation="vertical"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent">

        <TextView 

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="第一屏"/>

        </LinearLayout>

        <!-- 第2页 -->

         <LinearLayout 

        android:orientation="vertical"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:background="#ffffff">

        <TextView 

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="第二屏"/>

        </LinearLayout>

    </ViewFlipper>

</LinearLayout>


在res目录下新建文件夹anim,里面四个XML文件如下:

in_lefttoright.xml

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

       android:shareInterpolator="false">

    <!-- 从左向右移进动画效果 -->

    <translate

        android:fromXDelta="-100%p"

        android:toXDelta="0" 

        android:duration="1000"/>

</set>


in_righttoleft.xml

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

       android:shareInterpolator="false">

    <!-- 从右向左移进动画效果 -->

    <translate

        android:fromXDelta="100%p"

        android:toXDelta="0" 

        android:duration="1000"/>

</set>


out_lefttoright.xml

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

       android:shareInterpolator="false">

    <!-- 从左向右移出动画效果 -->

    <translate

        android:fromXDelta="0"

        android:toXDelta="100%p" 

        android:duration="1000"/>

</set>


out_righttoleft.xml

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

       android:shareInterpolator="false">

    <!-- 从右向左移出动画效果 -->

    <translate

        android:fromXDelta="0"

        android:toXDelta="-100%p" 

        android:duration="1000"/>

</set>


MainActivity.java

public class MainActivity extends Activity {

       private ViewFlipper flipper;

       private float startX;

       private Animation in_lefttorightAnimation;

       private Animation out_lefttorightAnimation;

       private Animation in_righttoleftAnimation;

       private Animation out_righttoleftAnimation;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        flipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);

        in_lefttorightAnimation = AnimationUtils.loadAnimation(this, R.anim.in_lefttoright);//从左向右进入屏幕动画效果

        out_lefttorightAnimation = AnimationUtils.loadAnimation(this, R.anim.out_lefttoright);//从左向右移出屏幕动画效果

        in_righttoleftAnimation = AnimationUtils.loadAnimation(this, R.anim.in_righttoleft);//从右向左进入屏幕动画效果

        out_righttoleftAnimation = AnimationUtils.loadAnimation(this, R.anim.out_righttoleft);//从右向左移出屏幕动画效果

    }

public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN://手指按下屏幕

               startX = event.getX();//取得按下时刻的点

               break;

 

        case MotionEvent.ACTION_UP://手指抬起

               float endX = event.getX();//取得抬起时刻的点

               if(endX > startX){//向右

                      flipper.setInAnimation(in_lefttorightAnimation);//设置从左向右进入屏幕动画效果

                      flipper.setOutAnimation(out_lefttorightAnimation);//设置从左向右移出屏幕动画效果

                      flipper.showNext();//显示下一页 

               }else if(endX < startX){//向左

                      flipper.setInAnimation(in_righttoleftAnimation);//设置从右向左进入屏幕动画效果

                      flipper.setOutAnimation(out_righttoleftAnimation);//设置从右向左移出屏幕动画效果

                      flipper.showPrevious();//显示上一页

               }

               break;

        }

        

        return super.onTouchEvent(event);

}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值