Android利用ViewFlipper實現螢幕切換動畫效果

來源:互聯網
上載者:User
1、螢幕切換指的是在同一個Activity內螢幕見的切換,最長見的情況就是在一個FrameLayout內有多個頁面,比如一個系統設定頁面;一個個人化頁面。2、介紹ViewFilpper類ViewFlipperextends
ViewAnimator
java.lang.Object
   ↳ android.view.View

   ↳ android.view.ViewGroup


   ↳ android.widget.FrameLayout



   ↳ android.widget.ViewAnimator




   ↳ android.widget.ViewFlipper
Class Overview

Simple ViewAnimator that will animate between two or more views that have been added to it. Only one child
is shown at a time. If requested, can automatically flip between each child at a regular interval.

意思是:簡單的ViewAnimator之間,兩個或兩個以上的view加上動畫效果。只有一個小孩會顯示在一個時間。如果需要,每個孩子能自動翻轉之間在固定的時間間隔。

該類繼承了Framelayout類,ViewAnimator類的作用是為FrameLayout裡面的View切換提供動畫效果。

該類有如下幾個和動畫相關的函數:

 setInAnimation:設定View進入螢幕時候使用的動畫,該函數有兩個版本,一個接受單個參數,類型為android.view.animation.Animation;一個接受兩個參數,類型為Context和int,分別為Context對象和定義Animation的resourceID。  

 setOutAnimation: 設定View退出螢幕時候使用的動畫,參數setInAnimation函數一樣。

showNext: 調用該函數來顯示FrameLayout裡面的下一個View。

showPrevious: 調用該函數來顯示FrameLayout裡面的上一個View。

3、首選看一下定義四個動畫的xml檔案:in_leftright.xml——從左至右進入螢幕

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

out_leftright.xml——從左至右出去螢幕

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

in_rightleft.xml——從右至左進入螢幕

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

out_rightleft.xml——從右至左出去螢幕

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

4、定義main.xml檔案

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical"><ViewFlipper android:id="@+id/viewFlipper"android:layout_width="fill_parent" android:layout_height="fill_parent"><!-- 第一個頁面 --><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent" android:gravity="center"><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/a1" /></LinearLayout><!-- 第二個頁面 --><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent" android:gravity="center"><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/a2"android:gravity="center" /></LinearLayout><!-- 第三個頁面 --><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent" android:gravity="center"><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/a3"android:gravity="center" /></LinearLayout><!-- 第四個頁面 --><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent" android:gravity="center"><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/a4"android:gravity="center" /></LinearLayout></ViewFlipper></LinearLayout>

5、java代碼實現:

public class SwitchTest2Activity extends Activity {ViewFlipper viewFlipper = null;float startX;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init() {viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);}public boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:startX = event.getX();break;case MotionEvent.ACTION_UP:if (event.getX() > startX) { // 向右滑動viewFlipper.setInAnimation(this, R.anim.in_leftright);viewFlipper.setOutAnimation(this, R.anim.out_leftright);viewFlipper.showNext();} else if (event.getX() < startX) { // 向左滑動viewFlipper.setInAnimation(this, R.anim.in_rightleft);viewFlipper.setOutAnimation(this, R.anim.out_rightleft);viewFlipper.showPrevious();}break;}return super.onTouchEvent(event);}}

6、:在這裡看不出,我貼幾張圖片吧!! 從左向右滑滑到的結果



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.