Android——OnTouchListener(单点/多点触碰)和 GestureDetector.GestureListener(手势)
介绍
事件:重写,生成,添加
监听器:View.OnTouchListener , 事件:MotionEvent内的事件
OnTouchListener是安卓内多点触碰监听器。可以监听多手指的触碰事件。
方法
(View)
view.setOnTouchListener(OnTouchListener otl)
如果是图片的滑动,缩放扩大。需要让图片模式为martix。而且大小大于图片内容
重写实现 View.OnTouchListener.onTouch(View v , MotionEvent me)
使用
public class TouchListenerPra extends AppCompatActivity { private Matrix matrix = new Matrix(); private PointF point = new PointF(); private ImageView img_head; private static final String TAG = "TouchListenerPra"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_touch_listener_pra); img_head = this.findViewById(R.id.head); img_head.setOnTouchListener(new MyTouchListener()); } class MyTouchListener implements View.OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent event) { switch ( event.getAction() ){ case MotionEvent.ACTION_DOWN: matrix.set(img_head.getImageMatrix()); point.set(event.getX(),event.getY()); Log.i(TAG, "onTouch: 触发down"); break; case MotionEvent.ACTION_MOVE: matrix.postTranslate(event.getX()-point.x,event.getY()-point.y); img_head.setImageMatrix(matrix); Log.i(TAG, "onTouch: 触发"); break; } return true; } } }
介绍
GestureDetetor.GestureListener :手势监听器。在Activity中监听手势变化做出响应
重写,生成,添加
监听器:GestureDetetor.GestureListener 事件:MotionEvent内的事件
方法
(Activity)
onTouchEvent(MotionEvent event)
return GestureDetetor.onTouchEvent(event)
因为GestureListener是监听Activity的变化,所以需要在Activity内重写响应方法添加事件
(GestureListener)
public boolean onDown(MotionEvent e) {
Log.i(TAG, "onDown: 按下");
return false;
}
// 手指按下一定时间,不过没长按
@Override
public void onShowPress(MotionEvent e) {
Log.i(TAG, "onShowPress:中按下");
}
// 手指离开屏幕一瞬间
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
// 手指在屏幕上滑动
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
// 长按并且没有松开
@Override
public void onLongPress(MotionEvent e) {
Log.i(TAG, "onLongPress: 长按下");
}
// 迅速滑动,并松开
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.i(TAG, "onScroll: x: "+velocityX+" y:"+velocityY);
return false;
}
使用:
public class MainActivity extends AppCompatActivity { private GestureDetector myGestureDetector ; private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyGestureListener myGestureListener = new MyGestureListener(); myGestureDetector = new GestureDetector(this,myGestureListener); } @Override public boolean onTouchEvent(MotionEvent event) { return myGestureDetector.onTouchEvent(event); } private class MyGestureListener implements GestureDetector.OnGestureListener{ @Override public boolean onDown(MotionEvent e) { Log.i(TAG, "onDown: 按下"); return false; } // 手指按下一定时间,不过没长按 @Override public void onShowPress(MotionEvent e) { Log.i(TAG, "onShowPress:中按下"); } // 手指离开屏幕一瞬间 @Override public boolean onSingleTapUp(MotionEvent e) { return false; } // 手指在屏幕上滑动 @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } // 长按并且没有松开 @Override public void onLongPress(MotionEvent e) { Log.i(TAG, "onLongPress: 长按下"); } // 迅速滑动,并松开 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.i(TAG, "onScroll: x: "+velocityX+" y:"+velocityY); return false; } } }

浙公网安备 33010602011771号