手势识别[功能]
何为手势识别? 比如:你在屏幕上从左至右划出的一个动作 这就是手势 能够识别这个的就是 手势识别[思路]
1. android 有一个手势识别的类:OnGestureListener
2. 在 GestureDetector() 中使用上面的class 即可 系统就会把手势交由该类来处理
[代码]
1. 该类的定义
Java代码   1. public class SampleGuest implements OnGestureListener {  
   2.         Activity activity;  
   3.           
   4.         public SampleGuest(Activity a){  
   5.             activity = a;  
   6.         }  
   7.       
   8.         // called automatically, any screen action will Triggered it  
   9.         public boolean onTouchEvent(MotionEvent me){  
  10.             return gesture.onTouchEvent(me);  
  11.         }  
  12.   
  13.         @Override  
  14.         public boolean onDown(MotionEvent e) {  
  15.             // TODO Auto-generated method stub  
  16.             Log.d("TAG","[+++++++++++][onDown]");  
  17.             return true;  
  18.         }  
  19.   
  20.         @Override  
  21.         //e1, the begin of ACTION_DOWN MotionEvent  
  22.         //e2, the end of ACTION_DOWN MotionEvent  
  23.         // velocityX, the motion speed in X  
  24.         // velocityY:the motion speed in y  
  25.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  26.                 float velocityY) {  
  27.             // TODO Auto-generated method stub  
  28.             if ((e1.getX() - e2.getX() > VALUE_DISTANCE)  
  29.                     && Math.abs(velocityX) > VALUE_SPEED) {  
  30.                 Log.d("TAG","[+++++++++++][onFling][Fling left]");  
  31.             } else if ((e2.getX() - e1.getX() > VALUE_DISTANCE)  
  32.                     && Math.abs(velocityX) > VALUE_SPEED) {  
  33.                 Log.d("TAG","[+++++++++++][onDown][Fling right]");  
  34.   
  35.             }  
  36.             return true;  
  37.         }  
  38.   
  39.         @Override  
  40.         public void onLongPress(MotionEvent e) {  
  41.             // TODO Auto-generated method stub  
  42.             Log.d("TAG","[+++++++++++][onLongPress]");  
  43.         }  
  44.   
  45.         @Override  
  46.         public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  47.                 float distanceY) {  
  48.             // TODO Auto-generated method stub  
  49.             Log.d("TAG","[+++++++++++][onScroll]");  
  50.             return true;  
  51.         }  
  52.   
  53.         @Override  
  54.         public void onShowPress(MotionEvent e) {  
  55.             // TODO Auto-generated method stub  
  56.             Log.d("TAG","[+++++++++++][onShowPress]");  
  57.         }  
  58.   
  59.         @Override  
  60.         public boolean onSingleTapUp(MotionEvent e) {  
  61.             // TODO Auto-generated method stub  
  62.             Log.d("TAG","[+++++++++++][onSingleTapUp]");  
  63.             return true;  
  64.         }  
  65.           
  66.     }  public class SampleGuest implements OnGestureListener {
Activity activity;

public SampleGuest(Activity a){
activity = a;
}

    // called automatically, any screen action will Triggered it
    public boolean onTouchEvent(MotionEvent me){
     return gesture.onTouchEvent(me);
    } @Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("TAG","[+++++++++++][onDown]");
return true;
} @Override
//e1, the begin of ACTION_DOWN MotionEvent
//e2, the end of ACTION_DOWN MotionEvent
// velocityX, the motion speed in X
// velocityY:the motion speed in y
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
if ((e1.getX() - e2.getX() > VALUE_DISTANCE)
     && Math.abs(velocityX) > VALUE_SPEED) {
     Log.d("TAG","[+++++++++++][onFling][Fling left]");
     } else if ((e2.getX() - e1.getX() > VALUE_DISTANCE)
     && Math.abs(velocityX) > VALUE_SPEED) {
     Log.d("TAG","[+++++++++++][onDown][Fling right]");      }
return true;
} @Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("TAG","[+++++++++++][onLongPress]");
} @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
Log.d("TAG","[+++++++++++][onScroll]");
return true;
} @Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("TAG","[+++++++++++][onShowPress]");
} @Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("TAG","[+++++++++++][onSingleTapUp]");
return true;
}

}
(大家可以自己做一些手势 然后看 LogCat 就会知道是什么手势了)
2. 如何使用
Java代码   1. SampleGuest sg = new SampleGuest(this);  
   2. GestureDetector gesture = new GestureDetector(sg);     1. // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发  
   2. public boolean onDown(MotionEvent e){}  
   3.   
   4. // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN,   
   5. //多个ACTION_MOVE, 1个ACTION_UP触发  
   6. // e1:第1个ACTION_DOWN MotionEvent  
   7. // e2:最后一个ACTION_MOVE MotionEvent  
   8. // velocityX:X轴上的移动速度,像素/秒  
   9. // velocityY:Y轴上的移动速度,像素/秒  
  10. // 触发条件 :  
  11. // X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒  
  12. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY){}  
  13.   
  14. // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发  
  15. public void onLongPress(MotionEvent e)  
  16.   
  17. // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发  
  18. public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY)  
  19.   
  20. // 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发  
  21. // 注意和onDown()的区别,强调的是没有松开或者拖动的状态  
  22. public void onShowPress(MotionEvent e)  
  23.   
  24. // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发  
  25. public boolean onSingleTapUp(MotionEvent e)  

解决方案 »

  1.   

    1.Activity类Rotate.java文件
    Java代码   1. package cn.com;  
       2.   
       3. import android.app.Activity;  
       4. import android.os.Bundle;  
       5.   
       6. import android.view.GestureDetector;  
       7. import android.view.MotionEvent;  
       8. import android.widget.ImageView;  
       9.   
      10. public class Rotate extends Activity {  
      11.     GestureDetector gesture;  
      12.   
      13.     int mCenterX = 160;  
      14.     int mCenterY = 0;  
      15.   
      16.     ImageView mImageView1;  
      17.     ImageView mImageView2;  
      18.   
      19.     /** Called when the activity is first created. */  
      20.     @Override  
      21.     public void onCreate(Bundle savedInstanceState) {  
      22.         super.onCreate(savedInstanceState);  
      23.         setContentView(R.layout.main);  
      24.   
      25.         FlingGuest sg = new FlingGuest(this);  
      26.         gesture = new GestureDetector(sg);  
      27.   
      28.         mImageView1 = (ImageView) findViewById(R.id.image1);  
      29.         mImageView2 = (ImageView) findViewById(R.id.image2);  
      30.     }  
      31.   
      32.     public void leftMoveHandle() {  
      33.         Rotate3d leftAnimation = new Rotate3d(0, -90, 0, 0, mCenterX, mCenterY);  
      34.         Rotate3d rightAnimation = new Rotate3d(90, 0, 0.0f, 0.0f, mCenterX,  
      35.                 mCenterY);  
      36.   
      37.         leftAnimation.setFillAfter(true);  
      38.         leftAnimation.setDuration(1000);  
      39.         rightAnimation.setFillAfter(true);  
      40.         rightAnimation.setDuration(1000);  
      41.   
      42.         mImageView1.startAnimation(leftAnimation);  
      43.         mImageView2.startAnimation(rightAnimation);  
      44.     }  
      45.   
      46.     public void rightMoveHandle() {  
      47.         Rotate3d leftAnimation = new Rotate3d(0, 90, 0, 0, mCenterX, mCenterY);  
      48.         Rotate3d rightAnimation = new Rotate3d(-90, 0, 0.0f, 0.0f, mCenterX,  
      49.                 mCenterY);  
      50.   
      51.         leftAnimation.setFillAfter(true);  
      52.         leftAnimation.setDuration(1000);  
      53.         rightAnimation.setFillAfter(true);  
      54.         rightAnimation.setDuration(1000);  
      55.   
      56.         mImageView1.startAnimation(rightAnimation);  
      57.         mImageView2.startAnimation(leftAnimation);  
      58.     }  
      59.   
      60.     // called automatically, any screen action will Triggered it  
      61.     public boolean onTouchEvent(MotionEvent me) {  
      62.         return gesture.onTouchEvent(me);  
      63.     }  
      64. }  package cn.com;import android.app.Activity;
    import android.os.Bundle;import android.view.GestureDetector;
    import android.view.MotionEvent;
    import android.widget.ImageView;public class Rotate extends Activity {
    GestureDetector gesture; int mCenterX = 160;
    int mCenterY = 0; ImageView mImageView1;
    ImageView mImageView2; /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); FlingGuest sg = new FlingGuest(this);
    gesture = new GestureDetector(sg); mImageView1 = (ImageView) findViewById(R.id.image1);
    mImageView2 = (ImageView) findViewById(R.id.image2);
    } public void leftMoveHandle() {
    Rotate3d leftAnimation = new Rotate3d(0, -90, 0, 0, mCenterX, mCenterY);
    Rotate3d rightAnimation = new Rotate3d(90, 0, 0.0f, 0.0f, mCenterX,
    mCenterY); leftAnimation.setFillAfter(true);
    leftAnimation.setDuration(1000);
    rightAnimation.setFillAfter(true);
    rightAnimation.setDuration(1000); mImageView1.startAnimation(leftAnimation);
    mImageView2.startAnimation(rightAnimation);
    } public void rightMoveHandle() {
    Rotate3d leftAnimation = new Rotate3d(0, 90, 0, 0, mCenterX, mCenterY);
    Rotate3d rightAnimation = new Rotate3d(-90, 0, 0.0f, 0.0f, mCenterX,
    mCenterY); leftAnimation.setFillAfter(true);
    leftAnimation.setDuration(1000);
    rightAnimation.setFillAfter(true);
    rightAnimation.setDuration(1000); mImageView1.startAnimation(rightAnimation);
    mImageView2.startAnimation(leftAnimation);
    } // called automatically, any screen action will Triggered it
    public boolean onTouchEvent(MotionEvent me) {
    return gesture.onTouchEvent(me);
    }
    }2.手势监听类
    Java代码   1. package cn.com;  
       2.   
       3. import android.app.Activity;  
       4. import android.util.Log;  
       5. import android.view.MotionEvent;  
       6. import android.view.GestureDetector.OnGestureListener;  
       7.   
       8. public class FlingGuest implements OnGestureListener {  
       9.     Activity activity;  
      10.   
      11.     int VALUE_DISTANCE = 100;  
      12.     int VALUE_SPEED = 20;  
      13.   
      14.     public FlingGuest(Activity a) {  
      15.         activity = a;  
      16.     }  
      17.   
      18.     // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发  
      19.     public boolean onDown(MotionEvent e) {  
      20.         Log.d("TAG", "[+++++++++++][onDown]");  
      21.         return true;  
      22.     }  
      23.   
      24.     // e1, the begin of ACTION_DOWN MotionEvent  
      25.     // e2, the end of ACTION_DOWN MotionEvent  
      26.     // velocityX, the motion speed in X  
      27.     // velocityY:the motion speed in y  
      28.     // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN,  
      29.     // 多个ACTION_MOVE, 1个ACTION_UP触发  
      30.     // e1:第1个ACTION_DOWN MotionEvent  
      31.     // e2:最后一个ACTION_MOVE MotionEvent  
      32.     // velocityX:X轴上的移动速度,像素/秒  
      33.     // velocityY:Y轴上的移动速度,像素/秒  
      34.     // 触发条件 :  
      35.     // X轴的坐标位移大于VALUE_DISTANCE,且移动速度大于VALUE_SPEED个像素/秒  
      36.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
      37.             float velocityY) {  
      38.         if ((e1.getX() - e2.getX() > VALUE_DISTANCE)  
      39.                 && Math.abs(velocityX) > VALUE_SPEED) {  
      40.             Log.d("TAG", "[+++++++++++][onFling][Fling left]");  
      41.             ((Rotate)activity).leftMoveHandle();  
      42.               
      43.         } else if ((e2.getX() - e1.getX() > VALUE_DISTANCE)  
      44.                 && Math.abs(velocityX) > VALUE_SPEED) {  
      45.             Log.d("TAG", "[+++++++++++][onDown][Fling right]");  
      46.             ((Rotate)activity).rightMoveHandle();  
      47.         }  
      48.         return true;  
      49.     }  
      50.   
      51.     // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发  
      52.     public void onLongPress(MotionEvent e) {  
      53.         Log.d("TAG", "[+++++++++++][onLongPress]");  
      54.     }  
      55.   
      56.     // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发  
      57.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
      58.             float distanceY) {  
      59.         Log.d("TAG", "[+++++++++++][onScroll]");  
      60.         return true;  
      61.     }  
      62.   
      63.     // 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发  
      64.     // 注意和onDown()的区别,强调的是没有松开或者拖动的状态  
      65.     public void onShowPress(MotionEvent e) {  
      66.         Log.d("TAG", "[+++++++++++][onShowPress]");  
      67.     }  
      68.   
      69.     // 用户(轻触触摸屏后)松开,由一个MotionEvent ACTION_UP触发  
      70.     public boolean onSingleTapUp(MotionEvent e) {  
      71.         Log.d("TAG", "[+++++++++++][onSingleTapUp]");  
      72.         return true;  
      73.     }  
      74.   
      75. }  
      

  2.   

    3.Rotate3d.java动画辅助类
    Java代码   1. package cn.com;  
       2.   
       3. import android.graphics.Camera;  
       4. import android.graphics.Matrix;  
       5. import android.view.animation.Animation;  
       6. import android.view.animation.Transformation;  
       7.   
       8. public class Rotate3d extends Animation {  
       9.     private float mFromDegree;  
      10.     private float mToDegree;  
      11.     private float mCenterX;  
      12.     private float mCenterY;  
      13.     private float mLeft;  
      14.     private float mTop;  
      15.     private Camera mCamera;  
      16.     private static final String TAG = "Rotate3d";  
      17.   
      18.     public Rotate3d(float fromDegree, float toDegree, float left, float top,  
      19.             float centerX, float centerY) {  
      20.         this.mFromDegree = fromDegree;  
      21.         this.mToDegree = toDegree;  
      22.         this.mLeft = left;  
      23.         this.mTop = top;  
      24.         this.mCenterX = centerX;  
      25.         this.mCenterY = centerY;  
      26.   
      27.     }  
      28.   
      29.     @Override  
      30.     public void initialize(int width, int height, int parentWidth,  
      31.             int parentHeight) {  
      32.         super.initialize(width, height, parentWidth, parentHeight);  
      33.         mCamera = new Camera();  
      34.     }  
      35.   
      36.     @Override  
      37.     protected void applyTransformation(float interpolatedTime, Transformation t) {  
      38.         final float FromDegree = mFromDegree;  
      39.         float degrees = FromDegree + (mToDegree - mFromDegree)  
      40.                 * interpolatedTime;  
      41.         final float centerX = mCenterX;  
      42.         final float centerY = mCenterY;  
      43.         final Matrix matrix = t.getMatrix();  
      44.   
      45.         if (degrees <= -76.0f) {  
      46.             degrees = -90.0f;  
      47.             mCamera.save();  
      48.             mCamera.rotateY(degrees);  
      49.             mCamera.getMatrix(matrix);  
      50.             mCamera.restore();  
      51.         } else if (degrees >= 76.0f) {  
      52.             degrees = 90.0f;  
      53.             mCamera.save();  
      54.             mCamera.rotateY(degrees);  
      55.             mCamera.getMatrix(matrix);  
      56.             mCamera.restore();  
      57.         } else {  
      58.             mCamera.save();  
      59.             //  
      60.             mCamera.translate(0, 0, centerX);  
      61.             mCamera.rotateY(degrees);  
      62.             mCamera.translate(0, 0, -centerX);  
      63.             mCamera.getMatrix(matrix);  
      64.             mCamera.restore();  
      65.         }  
      66.   
      67.         matrix.preTranslate(-centerX, -centerY);  
      68.         matrix.postTranslate(centerX, centerY);  
      69.     }  
      70. }  package cn.com;import android.graphics.Camera;
    import android.graphics.Matrix;
    import android.view.animation.Animation;
    import android.view.animation.Transformation;public class Rotate3d extends Animation {
    private float mFromDegree;
    private float mToDegree;
    private float mCenterX;
    private float mCenterY;
    private float mLeft;
    private float mTop;
    private Camera mCamera;
    private static final String TAG = "Rotate3d"; public Rotate3d(float fromDegree, float toDegree, float left, float top,
    float centerX, float centerY) {
    this.mFromDegree = fromDegree;
    this.mToDegree = toDegree;
    this.mLeft = left;
    this.mTop = top;
    this.mCenterX = centerX;
    this.mCenterY = centerY; } @Override
    public void initialize(int width, int height, int parentWidth,
    int parentHeight) {
    super.initialize(width, height, parentWidth, parentHeight);
    mCamera = new Camera();
    } @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
    final float FromDegree = mFromDegree;
    float degrees = FromDegree + (mToDegree - mFromDegree)
    * interpolatedTime;
    final float centerX = mCenterX;
    final float centerY = mCenterY;
    final Matrix matrix = t.getMatrix(); if (degrees <= -76.0f) {
    degrees = -90.0f;
    mCamera.save();
    mCamera.rotateY(degrees);
    mCamera.getMatrix(matrix);
    mCamera.restore();
    } else if (degrees >= 76.0f) {
    degrees = 90.0f;
    mCamera.save();
    mCamera.rotateY(degrees);
    mCamera.getMatrix(matrix);
    mCamera.restore();
    } else {
    mCamera.save();
    //
    mCamera.translate(0, 0, centerX);
    mCamera.rotateY(degrees);
    mCamera.translate(0, 0, -centerX);
    mCamera.getMatrix(matrix);
    mCamera.restore();
    } matrix.preTranslate(-centerX, -centerY);
    matrix.postTranslate(centerX, centerY);
    }
    }4.main.xml
    Java代码   1. <?xml version="1.0" encoding="utf-8"?>  
       2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       3.     android:orientation="vertical" android:layout_width="fill_parent"  
       4.     android:layout_height="wrap_content">  
       5.     <ImageView android:id="@+id/image2" android:layout_width="fill_parent"  
       6.         android:layout_height="fill_parent" android:background="@drawable/two" />  
       7.     <ImageView android:id="@+id/image1" android:layout_width="fill_parent"  
       8.         android:layout_height="fill_parent" android:background="@drawable/one" />  
       9. </RelativeLayout>