ViewFlipper+ImageView实现切换动画

效果图:
这里写图片描述
先建立布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.viewflipperapplication.MainActivity">

    <ViewFlipper
        android:id="@+id/flipper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/m3"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/m2"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/m1"/>

            <Button
                android:id="@+id/btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="立即使用"
                android:textSize="24dp"
                android:padding="20dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"/>
        </RelativeLayout>
    </ViewFlipper>

</LinearLayout>

然后是MainActivity:

public class MainActivity extends AppCompatActivity {
    private ViewFlipper myFlipper;
    private float startX; //手指按下时的x坐标
    private float endX; //手指抬起时的x坐标
    private float moveX = 100F; //判断是否切换页面的标准值
    private Button myBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化flipper
        myFlipper = (ViewFlipper) findViewById(R.id.flipper);

//使用触摸监听事件实现
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();//手指按下时的X坐标
                break;
            case MotionEvent.ACTION_UP:
                endX = event.getX();//手指抬起时的X坐标
                if(startX-endX> moveX){//向左滑
                    myFlipper.setInAnimation(this,R.anim.right_in);
                    myFlipper.setOutAnimation(this,R.anim.left_out);
                    myFlipper.showNext();
                }
                if(endX - startX > moveX){//向右滑
                    myFlipper.setInAnimation(this,R.anim.left_in);
                    myFlipper.setOutAnimation(this,R.anim.right_out);
                    myFlipper.showPrevious();
                }
                break;
        }
        return super.onTouchEvent(event);
    }
}

这里需要注意还有四个布局文件,在在Android Studio的res中创建如下图的文件:
这里写图片描述

第一个文件名为left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="-100%p"
        android:toXDelta="0"/>
</set>

第二个文件名为left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%p"/>
</set>

第三个文件名为right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0"/>
</set>

第四个文件名为right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="100%p"/>
</set>

其中:
fromXDelta 属性为动画起始时 X坐标上的位置
toXDelta 属性为动画结束时 X坐标上的位置
fromYDelta 属性为动画起始时 Y坐标上的位置
toYDelta 属性为动画结束时 Y坐标上的位置

duration 属性为动画持续时间 ( 时间以毫秒为单位)

在这些属性里面还可以加上%和p,例如:

android:toXDelta=”100%”,表示自身的100%,也就是从View自己的位置开始。

android:toXDelta=”80%p”,表示父层View的80%,是以它父层View为参照的。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值