Android ViewFlipper(翻轉(zhuǎn)視圖)的基本使用

2023-03-31 13:43 更新

本節(jié)引言:

本節(jié)給大家?guī)Я说氖荲iewFlipper,它是Android自帶的一個(gè)多頁面管理控件,且可以自動(dòng)播放! 和ViewPager不同,ViewPager是一頁頁的,而ViewFlipper則是一層層的,和ViewPager一樣,很多時(shí)候, 用來實(shí)現(xiàn)進(jìn)入應(yīng)用后的引導(dǎo)頁,或者用于圖片輪播,本節(jié)我們就使用ViewFlipper寫一個(gè)簡單的圖片 輪播的例子吧~官方API:ViewFlipper


1.為ViewFlipper加入View的兩種方法

1)靜態(tài)導(dǎo)入

所謂的靜態(tài)導(dǎo)入就是像圖中這樣,把個(gè)個(gè)頁面添加到ViewFlipper的中間!


2)動(dòng)態(tài)導(dǎo)入

通過addView方法填充View


2.常用的一些方法

  • setInAnimation:設(shè)置View進(jìn)入屏幕時(shí)使用的動(dòng)畫
  • setOutAnimation:設(shè)置View退出屏幕時(shí)使用的動(dòng)畫
  • showNext:調(diào)用該方法來顯示ViewFlipper里的下一個(gè)View
  • showPrevious:調(diào)用該方法來顯示ViewFlipper的上一個(gè)View
  • setFilpInterval:設(shè)置View之間切換的時(shí)間間隔
  • setFlipping:使用上面設(shè)置的時(shí)間間隔來開始切換所有的View,切換會(huì)循環(huán)進(jìn)行
  • stopFlipping:停止View切換

3.使用實(shí)例

1)示例1:使用ViewFlipper實(shí)現(xiàn)圖片輪播(靜態(tài)導(dǎo)入)

實(shí)現(xiàn)效果圖

實(shí)現(xiàn)代碼

每個(gè)頁面的布局都是一個(gè)簡單的ImageView,這里就不貼了~先貼下兩個(gè)進(jìn)入以及 離開的動(dòng)畫:

right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="2000"
        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="2000"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />

</set>

然后是activity_main.xml布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ViewFlipper
        android:id="@+id/vflp_help"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inAnimation="@anim/right_in"
        android:outAnimation="@anim/right_out"
        android:flipInterval="3000">

        <include layout="@layout/page_help_one" />

        <include layout="@layout/page_help_two" />

        <include layout="@layout/page_help_three" />

        <include layout="@layout/page_help_four" />

    </ViewFlipper>

</RelativeLayout>

這里我們?cè)O(shè)置了flipInterval = 3000,即每隔3000ms切還一個(gè)~ 最后我們只需在MainActivity.java中調(diào)用ViewFlipper的startFlipping()方法開始滑動(dòng)!

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private ViewFlipper vflp_help;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vflp_help = (ViewFlipper) findViewById(R.id.vflp_help);
        vflp_help.startFlipping();
    }
}

2)示例2:支持手勢滑動(dòng)的ViewFlipper(動(dòng)態(tài)導(dǎo)入)

實(shí)現(xiàn)效果圖

代碼實(shí)現(xiàn)

因?yàn)槲覀兎譃檫M(jìn)入上一頁,進(jìn)入下一頁,所以除了上面的兩個(gè)動(dòng)畫外,我們?cè)偬砑觾蓚€(gè)動(dòng)畫:

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>

MainActivity.java

public class MainActivity extends AppCompatActivity  {

    private Context mContext;
    private ViewFlipper vflp_help;
    private int[] resId = {R.mipmap.ic_help_view_1,R.mipmap.ic_help_view_2,
            R.mipmap.ic_help_view_3,R.mipmap.ic_help_view_4};

    private final static int MIN_MOVE = 200;   //最小距離
    private MyGestureListener mgListener;
    private GestureDetector mDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        //實(shí)例化SimpleOnGestureListener與GestureDetector對(duì)象
        mgListener = new MyGestureListener();
        mDetector = new GestureDetector(this, mgListener);
        vflp_help = (ViewFlipper) findViewById(R.id.vflp_help);
        //動(dòng)態(tài)導(dǎo)入添加子View
        for(int i = 0;i < resId.length;i++){
            vflp_help.addView(getImageView(resId[i]));
        }

    }

    //重寫onTouchEvent觸發(fā)MyGestureListener里的方法
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mDetector.onTouchEvent(event);
    }

    //自定義一個(gè)GestureListener,這個(gè)是View類下的,別寫錯(cuò)哦?。?!
    private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) {
            if(e1.getX() - e2.getX() > MIN_MOVE){
                vflp_help.setInAnimation(mContext,R.anim.right_in);
                vflp_help.setOutAnimation(mContext, R.anim.right_out);
                vflp_help.showNext();
            }else if(e2.getX() - e1.getX() > MIN_MOVE){
                vflp_help.setInAnimation(mContext,R.anim.left_in);
                vflp_help.setOutAnimation(mContext, R.anim.left_out);
                vflp_help.showPrevious();
            }
            return true;
        }
    }

    private ImageView getImageView(int resId){
        ImageView img = new ImageView(this);
        img.setBackgroundResource(resId);
        return img;
    }
}

代碼要點(diǎn)解析

1.這里我們通過動(dòng)態(tài)的方法添加View,這里只是簡單的ImageView,可根據(jù)自己需求進(jìn)行擴(kuò)展! 2.相信細(xì)心的你發(fā)現(xiàn)了,這里我們的手勢用的不是通過onTouchEvent直接判斷的,然后重寫 onTouch事件,對(duì)Action進(jìn)行判斷,然后如果是MotionEvent.ACTION_MOVE的話,就執(zhí)行下述代碼:

后來發(fā)現(xiàn),模擬器上因?yàn)槭鞘髽?biāo)的關(guān)系,并不會(huì)頻繁的抖動(dòng),而真機(jī)上,因?yàn)槭种敢恢笔穷澏兜?所以ACTION_MOVE會(huì)一直觸發(fā),然后View一直切換,后來考慮了Berial(B神)的建議,加入了 一個(gè)值來進(jìn)行判斷,就是添加一個(gè)標(biāo)志:

可以是可以,不過感覺還是有點(diǎn)不流暢,怪怪的,后來想想還是用手勢類,直接在onFling處理 就好,于是就有了上面的代碼,運(yùn)行起來杠杠滴~ 當(dāng)然,如果你對(duì)Gesture手勢不熟悉的話,可以參見之前寫過的一篇文章:Android基礎(chǔ)入門教程——3.8 Gesture(手勢)


4.代碼示例下載

ViewFlipperDemo.zip

ViewFlipperDemo2.zip


本節(jié)小結(jié):

好的,本節(jié)給大家講解了ViewFlipper(翻轉(zhuǎn)視圖)的基本使用,以后做圖片輪播和引導(dǎo)頁, 你就多了一個(gè)選擇了~嗯,就說這么多,謝謝~


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)