android手势:GestureDetector
android.view.GestureDetector
Detects various gestures and events using the supplied MotionEvent
s. The OnGestureListener
callback will notify users when a particular motion event has occurred. This class should only be used with MotionEvent
s reported via touch (don't use for trackball events). To use this class:
Create an instance of the
GestureDetector
for yourView
In the
View.onTouchEvent(MotionEvent)
method ensure you callonTouchEvent(MotionEvent)
. The methods defined in your callback will be executed when the events occur.
boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
@ OverrideNotified of a fling event when it occurs with the initial on down MotionEvent
and the matching up MotionEvent
. The calculated velocity is supplied along the x and y axis in pixels per second.
- Parameters :
- e1 The first down motion event that started the fling.
- e2 The move motion event that triggered the current onFling.
- velocityX The velocity of this fling measured in pixels per second along the x axis.
- velocityY The velocity of this fling measured in pixels per second along the y axis.
- Returns :
- true if the event is consumed, else false
创建GestureDetector时,实现OnGestureListener接口中的回调方法。
GestureDetector gestureDetector = new GestureDetector(getActivity(), new GestureDetector.OnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent e) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Logger.i(TAG, "++onFling++"); Logger.i(TAG, "e1.getAction():"+e1.getAction()+", e1.getX():"+e1.getX()+", e1.getY():"+e1.getY()); Logger.i(TAG, "e2.getAction():"+e2.getAction()+", e2.getX():"+e2.getX()+", e2.getY():"+e2.getY()); Logger.i(TAG, "velocityX:"+velocityX+", velocityY:"+velocityY); return true; // 需要返回true } @Override public boolean onDown(MotionEvent e) { return false; }});
在onTouchEvent方法中调用GestureDetector.onTouchEvent方法。
class OnProgramTouchListener implements View.OnTouchListener { float xDown; float xLast; float xCurrent; float xUp; float xDistance; int newLeftMargin; FrameLayout.LayoutParams params = null; @Override public boolean onTouch(View v, MotionEvent event) { Logger.i(TAG, "++programTouchListener.onTouch++"); gestureDetector.onTouchEvent(event);
创建OnProgramTouchListener的实例对象并且在onActivityCreated方法中使用。
@Overridepublic void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); programListFragment.getView().setOnTouchListener(programTouchListener);}View.OnTouchListener programTouchListener = new OnProgramTouchListener();
在onFling方法中打印出来的日志可以发现:onFling方法在getAction()的值等于MotionEvent.ACTION_UP时被调用。
11-01 14:02:25.057: D/WatchTvFragment(20176): ++programTouchListener.onTouch++11-01 14:02:25.057: D/WatchTvFragment(20176): event.getAction():211-01 14:02:25.057: D/WatchTvFragment(20176): leftMargin:33711-01 14:02:25.057: D/WatchTvFragment(20176): ++programTouchListener.onTouch++11-01 14:02:25.057: D/WatchTvFragment(20176): ++onFling++11-01 14:02:25.057: D/WatchTvFragment(20176): e1.getAction():0, e1.getX():155.7273, e1.getY():503.87511-01 14:02:25.057: D/WatchTvFragment(20176): e2.getAction():1, e2.getX():206.13757, e2.getY():487.0196511-01 14:02:25.067: D/WatchTvFragment(20176): velocityX:604.2696, velocityY:-86.6334511-01 14:02:25.067: D/WatchTvFragment(20176): event.getAction():1
日志中getAction()的可选值有:ACTION_UP(1), ACTION_MOVE(2), ACTION_DOWN(0).
更多相关文章
- Android(安卓)之美 从0到1 之Android(安卓)进阶(二)
- 饭后Android(安卓)第六餐-Bmob云后端(Bmob介绍,Android使用方法-增
- android流媒体播放器
- Android调用系统自带的文件管理器进行文件选择并获得路径,android
- Android与IOS异同点对比(1)------ 显示
- Android启动过程——init,Zygote,SystemServer
- Android之——史上最简单最酷炫的3D图片浏览效果的实现
- 在Android中创建和使用数据库
- Android工程手动增加插件包方法