Android之ViewFlipper

來源:互聯網
上載者:User

標籤:

首先看一下它的繼承圖:

viewAnimator 直接繼承於FrameLayout,其中ViewSwitcher和ViewFlipper是它的直接子類。

 

 

 

 

今天我們就來分析一下ViewFlipper。它可以用來實現圖片的自動展示

效果如下:

 

 

代碼實現部分:

1、MainActivity.java

簡簡單單幾行代碼,就是任性

 

public class MainActivity extends Activity {private ViewFlipper mViewFlipper;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mViewFlipper =(ViewFlipper) findViewById(R.id.mviewFlipper);             mViewFlipper.setInAnimation(this,R.animator.solide_in);  //設定圖片進入時的動畫        mViewFlipper.setOutAnimation(this, R.animator.solide_out);//設定圖片切出時的動畫        mViewFlipper.startFlipping();        }}



 

2、xml檔案

 在ViewFlipper中加入3個子View(中顯示的3張圖片)

 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <ViewFlipper         android:id="@+id/mviewFlipper"        android:layout_width="match_parent"        android:layout_height="match_parent"                >        <ImageView             android:id="@+id/iamge1"            android:layout_height="match_parent"            android:layout_width="match_parent"            android:src="@drawable/a"            />        <ImageView             android:id="@+id/iamge2"            android:layout_height="match_parent"            android:layout_width="match_parent"            android:src="@drawable/b"            />        <ImageView             android:id="@+id/iamge3"            android:layout_height="match_parent"            android:layout_width="match_parent"            android:src="@drawable/c"            />            </ViewFlipper></LinearLayout>


3、兩個動畫檔案:從螢幕外進入與從螢幕中出去

 

進入:

 

<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator"  >      <translate          android:duration="500"          android:fromXDelta="100%p"          android:toXDelta="0" />    <span style="white-space:pre"></span>此處為從右邊進入螢幕</set>  


出去:

 

 

<set xmlns:android="http://schemas.android.com/apk/res/android"      android:interpolator="@android:anim/decelerate_interpolator"     >     <translate  android:duration = "500"android:fromXDelta = "0"android:toXDelta = "-100%p"/>    此處為從螢幕<span style="font-family: Arial, Helvetica, sans-serif;">左邊出去</span></set>


4、上面顯示的圖片檔案,可選自己喜歡的圖片代替,這裡就不給出了。

 

 

你也可以設定不同的view進出方向:

 

android:fromXDelta="0" android:toXDelta="-100%p" 往左邊消失

android:fromXDelta="-100%p" android:toXDelta="0" 從左邊進

android:fromXDelta="0" android:toXDelta="100%p" 往右邊消失

android:fromXDelta="100%p" android:toXDelta="0" 從右邊進

Android之ViewFlipper

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.