Android手势识别ViewFlipper触摸动画


最近项目中用到了ViewFlipper这个类,感觉效果真的很炫,今天自己也试着做了下,确实还不错。

首先在layout下定义viewflipper.xml        

[html]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ViewFlipper  
  8.         android:id="@+id/viewFlipper1"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent" >  
  11.   
  12.         <ImageView  
  13.             android:id="@+id/imageView1"  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="fill_parent"  
  16.             android:src="@drawable/b" />  
  17.          <ImageView  
  18.             android:id="@+id/imageView2"  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="fill_parent"  
  21.             android:src="@drawable/c" />  
  22.           <ImageView  
  23.             android:id="@+id/imageView3"  
  24.             android:layout_width="fill_parent"  
  25.             android:layout_height="fill_parent"  
  26.             android:src="@drawable/d" />  
  27.           <ImageView  
  28.             android:id="@+id/imageView4"  
  29.             android:layout_width="fill_parent"  
  30.             android:layout_height="fill_parent"  
  31.             android:src="@drawable/f" />  
  32.          <ImageView  
  33.             android:id="@+id/imageView5"  
  34.             android:layout_width="fill_parent"  
  35.             android:layout_height="fill_parent"  
  36.             android:src="@drawable/g" />  
  37.     </ViewFlipper>  
  38.   
  39. </LinearLayout>  
ViewFlipper中包含了5张图片  用来手势切换。

[html]

  1. public class ViewFlipperActivity extends Activity implements OnTouchListener, android.view.GestureDetector.OnGestureListener {  
  2.     private ViewFlipper flipper;  
  3.     GestureDetector mGestureDetector;    
  4.     private int mCurrentLayoutState;    
  5.     private static final int FLING_MIN_DISTANCE = 80;    
  6.     private static final int FLING_MIN_VELOCITY = 150;    
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         // TODO Auto-generated method stub  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.viewflipper);  
  12.           
  13.         flipper=(ViewFlipper) this.findViewById(R.id.viewFlipper1);  
  14.          //注册一个用于手势识别的类    
  15.         mGestureDetector = new GestureDetector(this);    
  16.         //给mFlipper设置一个listener    
  17.         flipper.setOnTouchListener(this);    
  18.         mCurrentLayoutState = 0;    
  19.         //允许长按住ViewFlipper,这样才能识别拖动等手势    
  20.         flipper.setLongClickable(true);    
  21.           
  22.     }  
  23.     /**    
  24.      * 此方法在本例中未用到,可以指定跳转到某个页面    
  25.      * @param switchTo    
  26.      */    
  27.     public void switchLayoutStateTo(int switchTo) {    
  28.         while (mCurrentLayoutState != switchTo)   {    
  29.             if (mCurrentLayoutState > switchTo) {    
  30.                 mCurrentLayoutState--;    
  31.                 flipper.setInAnimation(inFromLeftAnimation());    
  32.                 flipper.setOutAnimation(outToRightAnimation());    
  33.                 flipper.showPrevious();    
  34.             } else {    
  35.                 mCurrentLayoutState++;    
  36.                 flipper.setInAnimation(inFromRightAnimation());    
  37.                 flipper.setOutAnimation(outToLeftAnimation());    
  38.                 flipper.showNext();    
  39.             }    
  40.      
  41.         }    
  42.    
  43.     }   
  44.     /**    
  45.      * 定义从右侧进入的动画效果    
  46.      * @return    
  47.      */    
  48.     protected Animation inFromRightAnimation() {    
  49.         Animation inFromRight = new TranslateAnimation(    
  50.                 Animation.RELATIVE_TO_PARENT, +1.0f,    
  51.                 Animation.RELATIVE_TO_PARENT, 0.0f,    
  52.                 Animation.RELATIVE_TO_PARENT, 0.0f,    
  53.                 Animation.RELATIVE_TO_PARENT, 0.0f);    
  54.         inFromRight.setDuration(200);    
  55.         inFromRight.setInterpolator(new AccelerateInterpolator());    
  56.         return inFromRight;    
  57.     }    
  58.      
  59.     /**    
  60.      * 定义从左侧退出的动画效果    
  61.      * @return    
  62.      */    
  63.     protected Animation outToLeftAnimation() {    
  64.         Animation outtoLeft = new TranslateAnimation(    
  65.                 Animation.RELATIVE_TO_PARENT, 0.0f,    
  66.                 Animation.RELATIVE_TO_PARENT, -1.0f,    
  67.                 Animation.RELATIVE_TO_PARENT, 0.0f,    
  68.                 Animation.RELATIVE_TO_PARENT, 0.0f);    
  69.         outtoLeft.setDuration(200);    
  70.         outtoLeft.setInterpolator(new AccelerateInterpolator());    
  71.         return outtoLeft;    
  72.     }    
  73.      
  74.     /**    
  75.      * 定义从左侧进入的动画效果    
  76.      * @return    
  77.      */    
  78.     protected Animation inFromLeftAnimation() {    
  79.         Animation inFromLeft = new TranslateAnimation(    
  80.                 Animation.RELATIVE_TO_PARENT, -1.0f,    
  81.                 Animation.RELATIVE_TO_PARENT, 0.0f,    
  82.                 Animation.RELATIVE_TO_PARENT, 0.0f,    
  83.                 Animation.RELATIVE_TO_PARENT, 0.0f);    
  84.         inFromLeft.setDuration(200);    
  85.         inFromLeft.setInterpolator(new AccelerateInterpolator());    
  86.         return inFromLeft;    
  87.     }    
  88.       
  89.     /**    
  90.      * 定义从右侧退出时的动画效果    
  91.      * @return    
  92.      */    
  93.     protected Animation outToRightAnimation() {    
  94.         Animation outtoRight = new TranslateAnimation(    
  95.                 Animation.RELATIVE_TO_PARENT, 0.0f,    
  96.                 Animation.RELATIVE_TO_PARENT, +1.0f,    
  97.                 Animation.RELATIVE_TO_PARENT, 0.0f,    
  98.                 Animation.RELATIVE_TO_PARENT, 0.0f);    
  99.         outtoRight.setDuration(200);    
  100.         outtoRight.setInterpolator(new AccelerateInterpolator());    
  101.         return outtoRight;    
  102.     }    
  103.       
  104.     public boolean onDown(MotionEvent e) {  
  105.         // TODO Auto-generated method stub  
  106.         return false;  
  107.     }  
  108.     /*    
  109.      * 用户按下触摸屏、快速移动后松开即触发这个事件    
  110.      * e1:第1个ACTION_DOWN MotionEvent    
  111.      * e2:最后一个ACTION_MOVE MotionEvent    
  112.      * velocityX:X轴上的移动速度,像素/秒    
  113.      * velocityY:Y轴上的移动速度,像素/秒    
  114.      * 触发条件 :    
  115.      * X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒    
  116.      */  
  117.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  118.             float velocityY) {  
  119.          if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE    
  120.                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    
  121.                 // 当像左侧滑动的时候    
  122.                 //设置View进入屏幕时候使用的动画    
  123.                 flipper.setInAnimation(inFromRightAnimation());    
  124.                 //设置View退出屏幕时候使用的动画    
  125.                 flipper.setOutAnimation(outToLeftAnimation());    
  126.                 flipper.showNext();    
  127.             } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE    
  128.                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    
  129.                 // 当像右侧滑动的时候    
  130.                 flipper.setInAnimation(inFromLeftAnimation());    
  131.                 flipper.setOutAnimation(outToRightAnimation());    
  132.                 flipper.showPrevious();    
  133.             }    
  134.             return false;    
  135.     }  
  136.     public void onLongPress(MotionEvent e) {  
  137.         // TODO Auto-generated method stub  
  138.           
  139.     }  
  140.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  141.             float distanceY) {  
  142.         // TODO Auto-generated method stub  
  143.         return false;  
  144.     }  
  145.     public void onShowPress(MotionEvent e) {  
  146.         // TODO Auto-generated method stub  
  147.           
  148.     }  
  149.     public boolean onSingleTapUp(MotionEvent e) {  
  150.         // TODO Auto-generated method stub  
  151.         return false;  
  152.     }  
  153.     public boolean onTouch(View v, MotionEvent event) {  
  154.         // TODO Auto-generated method stub  
  155.           // 一定要将触屏事件交给手势识别类去处理(自己处理会很麻烦的)    
  156.         return mGestureDetector.onTouchEvent(event);    
  157.     }  
  158.       
  159.       
  160. }  
这样就OK了 看下效果

因为是手势识别效果不太好演示,所以大家自己实现了以后就可以看到效果啦!

相关内容