使用什么 Android Api 来在 Android 上的开始屏幕上向左或向右滚动?
最简单的方法是检测“Fling”手势。 Android API 有一个内置的检测器,用于基本手势,如快速滑动、滚动、长按、双击、捏合缩放等。
该文档位于http://developer.android.com/reference/android/view/GestureDetector.html http://developer.android.com/reference/android/view/GestureDetector.html.
您要做的就是创建 GestureDetector 的实例,重写您有兴趣检测手势的视图的 onTouchEvent 方法,并将 MotionEvent 传递给 GestureDetector。
您还必须向 GestureDetector 提供 OnGestureListener 实现(最容易扩展 SimpleOnGestureListener),它将处理所有手势事件。
Example:
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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)