Android利用ViewFlipper實現屏幕切換動畫效果
ViewFlipper
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.
該類有如下幾個和動畫相關的函數:
setInAnimation:設置View進入屏幕時候使用的動畫,該函數有兩個版本,一個接受單個參數,類型爲android.view.animation.Animation;一個接受兩個參數,類型爲Context和int,分別爲Context對象和定義Animation的resourceID。
setOutAnimation: 設置View退出屏幕時候使用的動畫,參數setInAnimation函數一樣。
showNext: 調用該函數來顯示FrameLayout裏面的下一個View。
showPrevious: 調用該函數來顯示FrameLayout裏面的上一個View。
3、首選看一下定義四個動畫的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>
- <?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、效果圖:
從左向右滑滑到的結果
from:http://blog.csdn.net/rhljiayou/article/details/7195998#comments