<Android 基础(三十二)> ViewFlipper

简介

View Flipper,是ViewAnimator的子类,而ViewAnimator又是继承自FrameLayout,而FrameLayout就是平时基本上只显示一个子视图的布局,由于FrameLayout下不好确定子视图的位置,所以很多情况下子视图之前存在相互遮挡,这样就造成了很多时候我们基本上只要求FrameLayout显示一个子视图,然后通过某些控制来实现切换。正好,ViewFlipper帮我们实现了这个工作,我们需要做的就是,选择恰当的时机调用其恰当的方法即可

类结构

这里写图片描述

方法意义
startFlipping开始浏览
stopFlipping停止浏览
setFlipInterval设置View之间切换的时间间隔
getAccessibilityClassName获取类名称
isFlipping判断是否正在浏览
setAutoStart设置是否自动开始浏览
isAutoStart判断是否为自动开始浏览

基本使用

1. 动画定义
scalein.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="1000"
        android:fromXScale="0.2"
        android:fromYScale="0.2"
        android:toYScale="1"
        android:toXScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        >
    </scale>
</set>

scaleout.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="1000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:toYScale="0.2"
        android:toXScale="0.2"
        android:pivotX="50%"
        android:pivotY="50%">
    </scale>
</set>

2. 布局文件
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="jzfp.gs.com.animationdemo.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"></android.support.v7.widget.Toolbar>

    <!--渐入动画 和 渐出动画定义-->
    <ViewFlipper
        android:id="@+id/vf"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inAnimation="@anim/scalein"
        android:outAnimation="@anim/scaleout">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@mipmap/one" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@mipmap/two" />


        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@mipmap/three" />

    </ViewFlipper>

</LinearLayout>

3. 左右滑动切换

public class MainActivity extends AppCompatActivity {

    private ViewFlipper viewFlipper = null;
    float PosX = 0, CurrentX = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.left);
        setSupportActionBar(toolbar);//设置ActionBar

        viewFlipper = (ViewFlipper) findViewById(R.id.vf);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                PosX = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                CurrentX = event.getX();
                break;
            case MotionEvent.ACTION_UP:
                if (CurrentX - PosX > 25.0) {//向右滑动切换到上一页
                    viewFlipper.showPrevious();
                } else if (CurrentX - PosX < -25.0) {//向左滑动,切换到下一页
                    viewFlipper.showNext();
                }
        }
        return true;
    }
}

实际效果

这里写图片描述

转载于:https://www.cnblogs.com/lanzhi/p/6467155.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值