Android控制之垂直滾動廣告條ViewFLipper解析

最近在作公司項目的時候發現,像淘寶首頁和京東首頁同樣有一個垂直的廣告滾動條,效果以下圖。 這樣的效果能夠用Android的原生控件ViewFlipper來簡單實現android

ViewFlipper

使用方法

先來看看咱們的佈局文件bash

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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/view_flipper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inAnimation="@anim/anim_in"
        android:outAnimation="@anim/anim_out"
        android:layout_gravity="center"/>


</FrameLayout>
複製代碼

就是在FrameLayout裏嵌入一個ViewFlipperapp

這裏的屬性 inAnimation和outAnimation是子元素的出入動畫,經過設置這兩個值才能展示動畫效果,接下來看一下這個動畫的xml代碼,首先是anim_in.xml,很簡單的進入動畫,Y軸位置從下面100%移動到位置0,動畫持續1side

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="100%p"
        android:toYDelta="0"
        android:duration="1000"/>
</set>
複製代碼

而後是anim_out.xml,一樣的,出動畫。從0位置移動到-100%的位置,動畫持續1s佈局

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0"
        android:toYDelta="-100%p"
        android:duration="1000"/>
</set>
複製代碼

接下來看一下咱們的ViewFlipper的子項佈局post

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/music" />

    <TextView
        android:layout_width="73dp"
        android:layout_height="16dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="84dp"
        android:layout_marginStart="84dp"
        android:layout_marginTop="8dp"
        android:text="@string/app_name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageView"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
複製代碼

很簡單,就是一個圖片+一個標題而已。 接着就能夠設置ViewFlipper的子項了。動畫

public class MainActivity extends AppCompatActivity {

    private ViewFlipper mViewFlipper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewFlipper = (ViewFlipper)findViewById(R.id.view_flipper);
        for(int i=0;i<3;i++){
            View view = getLayoutInflater().inflate(R.layout.view_flipper_content,null);
            mViewFlipper.addView(view);
        }
        mViewFlipper.setFlipInterval(2000);
        mViewFlipper.startFlipping();
    }
}
複製代碼

這裏獲取控件ViewFlipper以後,對控件addView,而後設置每一個控件滑動到下一個控件的時間,以後調用startFlipping()就能夠開始滾動啦。ui

源碼解析

ViewFlipper繼承自ViewAnimator,實質上只是封裝了一些ViewAnimator的方法來調用,真正執行操做的是ViewAnimator。 咱們來看ViewFlipper的核心方法startFlipping()。spa

public void startFlipping() {
        mStarted = true;
        updateRunning();
    }
複製代碼

這裏調用的是updateRuning(),咱們接着跳進去看看3d

private void updateRunning() {
        updateRunning(true);
    }
複製代碼

一個私有方法,調用了重載的updateRunning,咱們繼續跳進去看看。

private void updateRunning(boolean flipNow) {
        boolean running = mVisible && mStarted && mUserPresent;
        if (running != mRunning) {
            if (running) {
                showOnly(mWhichChild, flipNow);
                postDelayed(mFlipRunnable, mFlipInterval);
            } else {
                removeCallbacks(mFlipRunnable);
            }
            mRunning = running;
        }
        if (LOGD) {
            Log.d(TAG, "updateRunning() mVisible=" + mVisible + ", mStarted=" + mStarted
                    + ", mUserPresent=" + mUserPresent + ", mRunning=" + mRunning);
        }
    }
複製代碼

在一系列的判斷後調用基類的方法showOnly,咱們跳轉過去看看。

void showOnly(int childIndex, boolean animate) {
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (i == childIndex) {
                if (animate && mInAnimation != null) {
                    child.startAnimation(mInAnimation);
                }
                child.setVisibility(View.VISIBLE);
                mFirstTime = false;
            } else {
                if (animate && mOutAnimation != null && child.getVisibility() == View.VISIBLE) {
                //對可見子項調用退出動畫
                    child.startAnimation(mOutAnimation);
                } else if (child.getAnimation() == mInAnimation)
                //不可見子項取消動畫
                    child.clearAnimation();
                //同時設置不可見
                child.setVisibility(View.GONE);
            }
        }
    }
複製代碼

根據官方文檔這裏的childIndex是要顯示的子項的位置,animate是否啓用動畫,默認是啓用的。

這個方法就是核心方法,這裏遍歷每個子項,若是找到childIndex的話則對他調用進入的動畫,對可見的子項則調用退出的動畫。

ViewAnimator繼承自FrameLayout,它的使用方法和ViewFLipper相似。只不過沒法自動滾動,而ViewFLipper使用Handler機制封裝了自動滾動的功能。ViewAnimator則須要調用showNext()來顯示下一個子項。自此咱們的分析結束。

結語

看了公司的自定義垂直滾動條感受仍是晦澀難懂,沒想到原生的控件源碼這麼簡單。看來仍是得多看看源碼纔好。

相關文章
相關標籤/搜索