Android屏幕手势检测的原理是由于触摸事件的检测与识别比较繁琐,因此Android提供了手势检测器帮助开发者识别手势。利用手势检测器可以自动辨别常用的几个手势事件,如点击、长按、滑动等,从而使开发者专注于业务逻辑,不必在手势的行为判断上绞尽脑汁。
- layout/activity_gesture_detector.xml界面布局代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="10dp">
<TextView
android:id="@+id/tv_gesture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="这里查看手势结果"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
- GestureDetectorActivity.java逻辑代码如下:
package com.fukaimei.gesturedetector;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.TextView;
import com.fukaimei.gesturedetector.util.DateUtil;
public class GestureDetectorActivity extends AppCompatActivity {
private TextView tv_gesture;
private GestureDetector mGesture;
private String desc = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture_detector);
tv_gesture = (TextView) findViewById(R.id.tv_gesture);
mGesture = new GestureDetector(this, new MyGestureListener());
}
public boolean dispatchTouchEvent(MotionEvent event) {
mGesture.onTouchEvent(event);
return true;
}
final class MyGestureListener implements GestureDetector.OnGestureListener {
@Override
public final boolean onDown(MotionEvent event) {
// desc = String.format("%s%s 您按下来了\n", desc, DateUtil.getNowTime());
// tv_gesture.setText(desc);
//onDown的返回值没有作用,不影响其它手势的处理
return true;
}
@Override
public final boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float offsetX = e1.getX() - e2.getX();
float offsetY = e1.getY() - e2.getY();
if (Math.abs(offsetX) > Math.abs(offsetY)) {
if (offsetX > 0) {
desc = String.format("%s%s 您向左滑动了一下\n", desc, DateUtil.getNowTime());
} else {
desc = String.format("%s%s 您向右滑动了一下\n", desc, DateUtil.getNowTime());
}
} else {
if (offsetY > 0) {
desc = String.format("%s%s 您向上滑动了一下\n", desc, DateUtil.getNowTime());
} else {
desc = String.format("%s%s 您向下滑动了一下\n", desc, DateUtil.getNowTime());
}
}
tv_gesture.setText(desc);
return true;
}
@Override
public final void onLongPress(MotionEvent event) {
desc = String.format("%s%s 您长按了一下下\n", desc, DateUtil.getNowTime());
tv_gesture.setText(desc);
}
@Override
public final boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public final void onShowPress(MotionEvent event) {
}
@Override
public boolean onSingleTapUp(MotionEvent event) {
desc = String.format("%s%s 您轻轻点了一下\n", desc, DateUtil.getNowTime());
tv_gesture.setText(desc);
//返回true表示我已经处理了,别处不要再处理这个手势
return true;
}
}
}
- Demo程序运行效果界面截图如下: