ViewFlipper 可以包含多個View 且View之間的切換可以通過Animation來設置切換效果,如漸變。
xml布局:
ViewFlipper裡面有四個頁面,下面通過手勢切換頁面。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:background="@drawable/static_bg_snowy_day"
- android:layout_height="fill_parent">
- <ViewFlipper android:id="@+id/flipper"
- android:layout_width="fill_parent" android:layout_height="fill_parent">
- <include android:id="@+id/firstlayout" layout="@layout/first"></include>
- <include android:id="@+id/secondlayout" layout="@layout/second"></include>
- <include android:id="@+id/thirdlayout" layout="@layout/third"></include>
- <include android:id="@+id/fourthlayout" layout="@layout/fourth"></include>
- </ViewFlipper>
- </LinearLayout>
- public class viewflipper extends Activity implements OnGestureListener,
- OnTouchListener {
- private ViewFlipper mFlipper;
- GestureDetector mGestureDetector;
- private int mCurrentLayoutState;
- private static final int FLING_MIN_DISTANCE = 100;
- private static final int FLING_MIN_VELOCITY = 200;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.flipper);
- mFlipper = (ViewFlipper) findViewById(R.id.flipper);
- // 注冊一個用於手勢識別的類
- mGestureDetector = new GestureDetector(this);
- // 給mFlipper設置一個listener
- mFlipper.setOnTouchListener(this);
- mCurrentLayoutState = 0;
- // 允許長按住ViewFlipper,這樣才能識別拖動等手勢
- mFlipper.setLongClickable(true);
- }
-
- public boolean onDown(MotionEvent e) {
-
- return false;
- }
-
- /**
- * 用戶按下觸摸屏、快速移動後松開即觸發這個事件 e1:第1個ACTION_DOWN MotionEvent e2:最後一個ACTION_MOVE
- * MotionEvent velocityX:X軸上的移動速度,像素/秒 velocityY:Y軸上的移動速度,像素/秒 觸發條件 :
- * X軸的坐標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個像素/秒
- */
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
- && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
- // 當像左側滑動的時候
- // 設置View進入屏幕時候使用的動畫
- mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
- R.anim.push_left_in))
- // 設置View退出屏幕時候使用的動畫
- mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
- R.anim.push_left_out));
- mFlipper.showNext();
- } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
- && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
- // 當像右側滑動的時候
- mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
- R.anim.push_right_in));
- mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
- R.anim.push_right_out));
- mFlipper.showPrevious();
- }
- return false;
- }
-
- public void onLongPress(MotionEvent e) {
- }
-
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
- float distanceY) {
- return false;
- }
-
- public void onShowPress(MotionEvent e) {
- }
-
- public boolean onSingleTapUp(MotionEvent e) {
- return false;
- }
-
- public boolean onTouch(View v, MotionEvent event) {
- // 一定要將觸屏事件交給手勢識別類去處理(自己處理會很麻煩的)
- return mGestureDetector.onTouchEvent(event);
- }
- }
進入動畫
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate android:fromXDelta="100%p" android:toXDelta="0"
- android:duration="500" />
- <alpha android:fromAlpha="0.1" android:toAlpha="1.0"
- android:duration="500" />
- </set>