Android手势识别和双击事件
安卓触摸屏的手势识别在很多时候会使用户操作更加方便:
实现原理主要是将该界面的onTouchEvent设置为GestureDetector的onTouchEvent:
具体实现代码:
1.在Activity中响应事件:
MainActivity.java
package com.hu.gesturedemo; import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.widget.Toast; public class MainActivity extends Activity { GestureDetector gestureDetector = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gestureDetector = new GestureDetector(this, new SimpleOnGestureListener(){ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, //滑动事件 float velocityX, float velocityY) { if(Math.abs(velocityX) > Math.abs(velocityY)){ //如果X偏移量大于Y偏移量 if(velocityX > 0){ Toast.makeText(MainActivity.this, "Right Fling", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "Left Fling", Toast.LENGTH_SHORT).show(); } }else{ if(velocityY > 0){ Toast.makeText(MainActivity.this, "Down Fling", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "Up Fling", Toast.LENGTH_SHORT).show(); } } return super.onFling(e1, e2, velocityX, velocityY); } @Override public boolean onDoubleTap(MotionEvent e) { //双击事件 Toast.makeText(MainActivity.this, "On double Tap", Toast.LENGTH_SHORT).show(); return super.onDoubleTap(e); } }); } @Override public boolean onTouchEvent(MotionEvent event) { return gestureDetector.onTouchEvent(event); } }
2.在View中响应事件
MyView.java
package com.hu.gesturedemo; import android.content.Context; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.View; import android.widget.Toast; public class MyView extends View{ GestureDetector gestureDetector = null; public MyView(Context context) { super(context); gestureDetector = new GestureDetector(context, new SimpleOnGestureListener(){ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, //滑动事件 float velocityX, float velocityY) { if(Math.abs(velocityX) > Math.abs(velocityY)){ //如果X偏移量大于Y偏移量 if(velocityX > 0){ Toast.makeText(getContext(), "Right Fling", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getContext(), "Left Fling", Toast.LENGTH_SHORT).show(); } }else{ if(velocityY > 0){ Toast.makeText(getContext(), "Down Fling", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getContext(), "Up Fling", Toast.LENGTH_SHORT).show(); } } return super.onFling(e1, e2, velocityX, velocityY); } @Override public boolean onDoubleTap(MotionEvent e) { //双击事件 Toast.makeText(getContext(), "On double Tap", Toast.LENGTH_SHORT).show(); return super.onDoubleTap(e); } }); this.setClickable(true); this.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } }); } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28