最简单的方法是检测"Fling"手势.android API有一个内置的检测器,用于基本手势,如投掷,滚动,长按,双击,捏缩放等.
该文档可从http://developer.android.com/reference/android/view/GestureDetector.html获得.
你要做的是创建一个GestureDetector实例,覆盖你有兴趣检测手势的视图的onTouchEvent方法,并将MotionEvent传递给GestureDetector.
您还必须提供OnGestureListener实现(最容易将SimpleOnGestureListener扩展)到GestureDetector,它将处理您的所有手势事件.
例:
class MyView extends View
{
GestureDetector mGestureDetect;
public MyView(Context context)
{
super(context);
mGestureDetect = new GestureDetector(new SimpleOnGestureListener()
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
//check if the fling was in the direction you were interested in
if(e1.getX() - e2.getX() > 0)
{
//Do something here
}
//fast enough?
if(velocityX > 50)
{
//etc etc
}
return true;
}
}
}
public boolean onTouchEvent(MotionEvent ev)
{
mGestureDetector.onTocuhEvent(ev);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1475 次 |
| 最近记录: |