参考:http://www.jianshu.com/p/7b47be38f64a
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//手势识别检测器
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//隐藏系统标题
//requestWindowFeature(Window.FEATURE_NO_TITLE);//继承自Activity
if (getSupportActionBar() != null) {//继承自AppCompatActivity
getSupportActionBar().hide();
}
setContentView(R.layout.activity_main);
mGestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
@Override //按下pressed
public boolean onDown(MotionEvent motionEvent) {
Log.d("WENXIN", String.format("gesture onDown, %d, point(%f,%f)", motionEvent.getAction(),
motionEvent.getAxisValue(MotionEvent.AXIS_X), motionEvent.getAxisValue(MotionEvent.AXIS_Y)));
return false;
}
@Override //
public void onShowPress(MotionEvent motionEvent) {
Log.d("WENXIN", String.format("gesture onShowPress, %d, point(%f,%f)", motionEvent.getAction(),
motionEvent.getAxisValue(MotionEvent.AXIS_X), motionEvent.getAxisValue(MotionEvent.AXIS_Y)));
}
@Override //释放released
public boolean onSingleTapUp(MotionEvent motionEvent) {
Log.d("WENXIN", String.format("gesture onSingleTapUp, %d, point(%f,%f)", motionEvent.getAction(),
motionEvent.getAxisValue(MotionEvent.AXIS_X), motionEvent.getAxisValue(MotionEvent.AXIS_Y)));
return false;
}
@Override //长按
public void onLongPress(MotionEvent motionEvent) {
Log.d("WENXIN", String.format("gesture onLongPress, %d, point(%f,%f)", motionEvent.getAction(),
motionEvent.getAxisValue(MotionEvent.AXIS_X), motionEvent.getAxisValue(MotionEvent.AXIS_Y)));
}
@Override //垂直滚动
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
Log.d("WENXIN", String.format("gesture onFling, %d, point(%f,%f)", motionEvent.getAction(),
motionEvent.getAxisValue(MotionEvent.AXIS_X), motionEvent.getAxisValue(MotionEvent.AXIS_Y)));
return false;
}
@Override //左右滑动
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
Log.d("WENXIN", String.format("gesture onScroll, %d, point(%f,%f)", motionEvent.getAction(),
motionEvent.getAxisValue(MotionEvent.AXIS_X), motionEvent.getAxisValue(MotionEvent.AXIS_Y)));
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);//将该Activity上的触碰事件交给GesturDetector处理
}
@Override
public void onClick(View v) {
}
}
测试结果:
11-15 14:54:15.941 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(376.523438,1291.875000)
11-15 14:54:16.040 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(376.523438,1291.875000)
11-15 14:54:16.085 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(376.523438,1291.875000)
11-15 14:54:16.609 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(371.250000,1316.250000)
11-15 14:54:16.708 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(371.250000,1316.250000)
11-15 14:54:16.793 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(371.250000,1316.250000)
11-15 14:54:17.588 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(381.796875,1237.500000)
11-15 14:54:17.687 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(381.796875,1237.500000)
11-15 14:54:17.739 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(381.796875,1237.500000)
11-15 14:54:18.324 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(406.054688,1260.000000)
11-15 14:54:18.422 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(406.054688,1260.000000)
11-15 14:54:18.484 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(406.054688,1260.000000)
11-15 14:54:19.838 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(459.843750,1260.000000)
11-15 14:54:19.937 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(459.843750,1260.000000)
11-15 14:54:19.982 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(459.843750,1260.000000)
11-15 14:54:20.549 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(459.843750,1239.375000)
11-15 14:54:20.648 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(459.843750,1239.375000)
11-15 14:54:20.684 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(459.843750,1239.375000)
11-15 14:54:21.836 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(495.703125,1243.125000)
11-15 14:54:21.935 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(495.703125,1243.125000)
11-15 14:54:21.995 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(495.703125,1243.125000)
11-15 14:54:22.648 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(497.812500,1243.125000)
11-15 14:54:22.746 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(497.812500,1243.125000)
11-15 14:54:22.822 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(497.812500,1243.125000)
11-15 14:54:24.056 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(505.195312,1248.750000)
11-15 14:54:24.156 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(505.195312,1248.750000)
11-15 14:54:24.232 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(505.195312,1248.750000)
11-15 14:54:24.789 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(519.960938,1265.625000)
11-15 14:54:24.888 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(519.960938,1265.625000)
11-15 14:54:24.965 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(519.960938,1265.625000)
11-15 14:54:25.726 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(514.687500,1265.625000)
11-15 14:54:25.825 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(514.687500,1265.625000)
11-15 14:54:25.910 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(514.687500,1265.625000)
11-15 14:54:26.439 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(513.632812,1282.500000)
11-15 14:54:26.538 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(513.632812,1282.500000)
11-15 14:54:26.609 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(513.632812,1282.500000)
11-15 14:54:27.369 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(556.875000,1320.000000)
11-15 14:54:27.468 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(556.875000,1320.000000)
11-15 14:54:27.505 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(556.875000,1320.000000)
11-15 14:54:28.080 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(565.312500,1327.500000)
11-15 14:54:28.179 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(565.312500,1327.500000)
11-15 14:54:28.231 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(565.312500,1327.500000)
11-15 14:54:28.993 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(555.820312,1325.625000)
11-15 14:54:29.092 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(555.820312,1325.625000)
11-15 14:54:29.144 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(555.820312,1325.625000)
11-15 14:54:29.822 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(553.710938,1331.250000)
11-15 14:54:29.921 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(553.710938,1331.250000)
11-15 14:54:29.949 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onSingleTapUp,1,point(553.710938,1331.250000)
11-15 14:54:30.795 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(555.820312,1325.625000)
11-15 14:54:30.894 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(555.820312,1325.625000)
11-15 14:54:31.394 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onLongPress,0,point(555.820312,1325.625000)
11-15 14:54:32.319 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(552.656250,1323.750000)
11-15 14:54:32.419 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onShowPress,0,point(552.656250,1323.750000)
11-15 14:54:32.919 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onLongPress,0,point(552.656250,1323.750000)
11-15 14:54:34.117 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(548.437500,1301.250000)
11-15 14:54:34.185 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(548.437500,1301.250000)
11-15 14:54:34.202 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(548.437500,1301.250000)
11-15 14:54:34.218 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(548.437500,1301.250000)
11-15 14:54:34.235 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(548.437500,1301.250000)
11-15 14:54:34.252 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(548.437500,1301.250000)
11-15 14:54:34.269 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(548.437500,1301.250000)
11-15 14:54:34.285 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(548.437500,1301.250000)
11-15 14:54:34.302 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(548.437500,1301.250000)
11-15 14:54:34.319 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(548.437500,1301.250000)
11-15 14:54:34.325 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onFling,0,point(548.437500,1301.250000)
11-15 14:54:36.281 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(841.640625,1278.750000)
11-15 14:54:36.337 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(841.640625,1278.750000)
11-15 14:54:36.354 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(841.640625,1278.750000)
11-15 14:54:36.370 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(841.640625,1278.750000)
11-15 14:54:36.387 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(841.640625,1278.750000)
11-15 14:54:36.404 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(841.640625,1278.750000)
11-15 14:54:36.420 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(841.640625,1278.750000)
11-15 14:54:36.437 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(841.640625,1278.750000)
11-15 14:54:36.454 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(841.640625,1278.750000)
11-15 14:54:36.470 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(841.640625,1278.750000)
11-15 14:54:36.488 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(841.640625,1278.750000)
11-15 14:54:36.490 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onFling,0,point(841.640625,1278.750000)
11-15 14:54:37.396 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(583.242188,1545.000000)
11-15 14:54:37.471 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(583.242188,1545.000000)
11-15 14:54:37.488 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(583.242188,1545.000000)
11-15 14:54:37.505 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(583.242188,1545.000000)
11-15 14:54:37.521 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(583.242188,1545.000000)
11-15 14:54:37.538 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(583.242188,1545.000000)
11-15 14:54:37.555 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(583.242188,1545.000000)
11-15 14:54:37.571 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(583.242188,1545.000000)
11-15 14:54:37.588 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(583.242188,1545.000000)
11-15 14:54:37.605 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(583.242188,1545.000000)
11-15 14:54:37.621 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(583.242188,1545.000000)
11-15 14:54:37.638 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(583.242188,1545.000000)
11-15 14:54:37.639 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onFling,0,point(583.242188,1545.000000)
11-15 14:54:38.432 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onDown,0,point(395.507812,671.250000)
11-15 14:54:38.505 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(395.507812,671.250000)
11-15 14:54:38.522 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(395.507812,671.250000)
11-15 14:54:38.539 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(395.507812,671.250000)
11-15 14:54:38.555 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(395.507812,671.250000)
11-15 14:54:38.572 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(395.507812,671.250000)
11-15 14:54:38.589 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onScroll,0,point(395.507812,671.250000)
11-15 14:54:38.595 5013-5013/app.hwb.com.testfragment D/WENXIN:gesture onFling,0,point(395.507812,671.250000)
进阶:
http://www.gcssloop.com/customview/motionevent
http://blog.csdn.net/a772890398/article/details/50547693