最近項目中用到了ViewFlipper這個類,感覺效果真的很炫,今天自己也試著做了下,確實還不錯。
首先在layout下定義viewflipper.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/viewFlipper1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/b" /> <ImageView android:id="@+id/imageView2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/c" /> <ImageView android:id="@+id/imageView3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/d" /> <ImageView android:id="@+id/imageView4" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/f" /> <ImageView android:id="@+id/imageView5" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/g" /> </ViewFlipper></LinearLayout>
ViewFlipper中包含了5張圖片 用來手勢切換。
public class ViewFlipperActivity extends Activity implements OnTouchListener, android.view.GestureDetector.OnGestureListener { private ViewFlipper flipper; GestureDetector mGestureDetector; private int mCurrentLayoutState; private static final int FLING_MIN_DISTANCE = 80; private static final int FLING_MIN_VELOCITY = 150; @Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.viewflipper);flipper=(ViewFlipper) this.findViewById(R.id.viewFlipper1); //註冊一個用於手勢識別的類 mGestureDetector = new GestureDetector(this); //給mFlipper設定一個listener flipper.setOnTouchListener(this); mCurrentLayoutState = 0; //允許長按住ViewFlipper,這樣才能識別拖動等手勢 flipper.setLongClickable(true); }/** * 此方法在本例中未用到,可以指定跳轉到某個頁面 * @param switchTo */ public void switchLayoutStateTo(int switchTo) { while (mCurrentLayoutState != switchTo) { if (mCurrentLayoutState > switchTo) { mCurrentLayoutState--; flipper.setInAnimation(inFromLeftAnimation()); flipper.setOutAnimation(outToRightAnimation()); flipper.showPrevious(); } else { mCurrentLayoutState++; flipper.setInAnimation(inFromRightAnimation()); flipper.setOutAnimation(outToLeftAnimation()); flipper.showNext(); } } } /** * 定義從右側進入的動畫效果 * @return */ protected Animation inFromRightAnimation() { Animation inFromRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); inFromRight.setDuration(200); inFromRight.setInterpolator(new AccelerateInterpolator()); return inFromRight; } /** * 定義從左側退出的動畫效果 * @return */ protected Animation outToLeftAnimation() { Animation outtoLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); outtoLeft.setDuration(200); outtoLeft.setInterpolator(new AccelerateInterpolator()); return outtoLeft; } /** * 定義從左側進入的動畫效果 * @return */ protected Animation inFromLeftAnimation() { Animation inFromLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); inFromLeft.setDuration(200); inFromLeft.setInterpolator(new AccelerateInterpolator()); return inFromLeft; } /** * 定義從右側退出時的動畫效果 * @return */ protected Animation outToRightAnimation() { Animation outtoRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); outtoRight.setDuration(200); outtoRight.setInterpolator(new AccelerateInterpolator()); return outtoRight; } public boolean onDown(MotionEvent e) {// TODO Auto-generated method stubreturn 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進入螢幕時候使用的動畫 flipper.setInAnimation(inFromRightAnimation()); //設定View退出螢幕時候使用的動畫 flipper.setOutAnimation(outToLeftAnimation()); flipper.showNext(); } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { // 當像右側滑動的時候 flipper.setInAnimation(inFromLeftAnimation()); flipper.setOutAnimation(outToRightAnimation()); flipper.showPrevious(); } return false; }public void onLongPress(MotionEvent e) {// TODO Auto-generated method stub}public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {// TODO Auto-generated method stubreturn false;}public void onShowPress(MotionEvent e) {// TODO Auto-generated method stub}public boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubreturn false;}public boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stub // 一定要將觸屏事件交給手勢識別類去處理(自己處理會很麻煩的) return mGestureDetector.onTouchEvent(event); }}
這樣就OK了 看下效果
因為是手勢識別效果不太好示範,所以大家自己實現了以後就可以看到效果啦!