您的位置:首页 > 移动开发 > Android开发

Android实现手势控制

2016-05-06 23:56 423 查看
可以使用在许多View上
手势交互原理:
1.触屏一刹那,触发MotionEvent事件
2.被OnTouchListen监听,在onTouch()中获得MotionEvent对象
3.GestureDetector转发MotionEvent对象至OnGestureListen
4.OnGestureListen获得该对象,根据该对象封装的信息做出合适的反馈。

MotionEvent:
用于封装手势、触摸笔等动作事件
内部封装用于记录横轴和纵轴坐标的属性X和Y
GestureDetector:
识别各种手势
工作原理:
1.当接收到用户触摸消息时,将消息交给GestureDetector加工。
2.通过设置监听器获得GestureDetector处理后的手势。
OnGestureListener:处理单击类消息
onDown(MotionEvent e)单击
onSingleTapUp(MotionEvent e)抬起
onShowPress(MotionEvent  e)短按
onLongPress(MotionEvent e)长按
onScroll(MotionEvent e1,MotionEvent  e2,float x,float y)
onFling(MotionEvent  e1MotionEvent e2,float x,float y

OnDoubleTapListener:处理双击类消息
onDoubleTap(MotionEvent  e)双击
onDoubleTapEvent(MotionEvent  e)双击按下和抬起触发一次
onSingleTapConfirmed(MotionEvent  e)单击确认

OnGestureListener:
手势交互的监听接口,其提供多个抽象方法
根据GestureDetector的手势识别结果调用相对应的方法

使用SimpleOnGestureListener
     继承SimpleOnGestureListener
     重载感兴趣的手势

example:-------------------------------------------------------------------------------------------------
 private ImageView iv;

 GestureDetector detetor;

 class myGestureListenner extends SimpleOnGestureListener{

  @Override

  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

   // TODO Auto-generated method stub

   if(e1.getX()-e2.getX()>50){

    Toast.makeText(MainActivity.this, "从右往左边划", Toast.LENGTH_SHORT).show();

   }

   if(e2.getX()-e1.getX()>50){

    Toast.makeText(MainActivity.this, "从左往右边划", Toast.LENGTH_SHORT).show();

   }

   return super.onFling(e1, e2, velocityX, velocityY);

  }

 }

 

 @SuppressWarnings("deprecation")

 @Override

 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  iv = (ImageView) findViewById(R.id.imageView1);

  detetor =new GestureDetector(new myGestureListenner());

  iv.setOnTouchListener(new OnTouchListener() {

   public boolean onTouch(View v, MotionEvent event) {

    // TODO Auto-generated method stub

    detetor.onTouchEvent(event);

    return false;

   }

  });

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android