Android手势识别
程序员文章站
2024-02-11 23:36:58
...
Android手势识别
package com.example.gesturelistener0810;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
private TextView textView;
private GestureDetector gestureDetector;
public static final String TAG ="hello";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//步骤1:创建手势检测器实例
gestureDetector = new GestureDetector(this,new GestureDetectorListener());
textView = findViewById(R.id.text_view);
//让某个View检测手势,重写View的onTouch函数,将触屏事件交给GestureDetector进行处理
textView.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
private class GestureDetectorListener implements GestureDetector.OnGestureListener {
@Override
public boolean onDown(MotionEvent motionEvent) {
Log.d(TAG, "onDown: ");
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
Log.d(TAG, "onShowPress: ");
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
Log.d(TAG, "onSingleTapUp: ");
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
Log.d(TAG, "onScroll: ");
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
Log.d(TAG, "onLongPress: ");
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
Log.d(TAG, "onFling: ");
return false;
}
}
}
他人编写
TextView mTextView;
GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 步骤1:创建手势检测器实例 & 传入OnGestureListener接口(需要复写对应方法)
mGestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
// 1. 用户轻触触摸屏
public boolean onDown(MotionEvent e) {
Log.i("MyGesture1", "onDown");
return false;
}
// 2. 用户轻触触摸屏,尚未松开或拖动
// 与onDown()的区别:无松开 / 拖动
// 即:当用户点击的时,onDown()就会执行,在按下的瞬间没有松开 / 拖动时onShowPress就会执行
public void onShowPress(MotionEvent e) {
Log.i("MyGesture", "onShowPress");
}
// 3. 用户长按触摸屏
public void onLongPress(MotionEvent e) {
Log.i("MyGesture", "onLongPress");
}
// 4. 用户轻击屏幕后抬起
public boolean onSingleTapUp(MotionEvent e) {
Log.i("MyGesture", "onSingleTapUp");
return true;
}
// 5. 用户按下触摸屏 & 拖动
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.i("MyGesture", "onScroll:");
return true;
}
// 6. 用户按下触摸屏、快速移动后松开
// 参数:
// e1:第1个ACTION_DOWN MotionEvent
// e2:最后一个ACTION_MOVE MotionEvent
// velocityX:X轴上的移动速度,像素/秒
// velocityY:Y轴上的移动速度,像素/秒
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.i("MyGesture", "onFling");
return true;
}
});
// 步骤2:创建 & 设置OnDoubleTapListener接口实现类
mGestureDetector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
// 1. 单击事件
// 关于OnDoubleTapListener.onSingleTapConfirmed()和 OnGestureListener.onSingleTapUp()的区别
// onSingleTapConfirmed:再次点击(即双击),则不会执行
// onSingleTapUp:手抬起就会执行
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.i("MyGesture", "onSingleTapConfirmed");
return false;
}
// 2. 双击事件
public boolean onDoubleTap(MotionEvent e) {
Log.i("MyGesture", "onDoubleTap");
return false;
}
// 3. 双击间隔中发生的动作
// 指触发onDoubleTap后,在双击之间发生的其它动作,包含down、up和move事件;
public boolean onDoubleTapEvent(MotionEvent e) {
Log.i("MyGesture", "onDoubleTapEvent");
return false;
}
});
// 步骤3:重写View的onTouch函数,将触屏事件交给GestureDetector处理,从而对用户手势作出响应
mTextView = (TextView) findViewById(R.id.textView);
mTextView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return true;
}
});
}
}