Android启动屏幕上使用的手势

3

在Android上,用于获取启动屏幕向左或向右滚动的Android Api是什么?


也许如果您解释一下您想要实现什么,您的悬赏就会得到满足。我猜您所说的“启动屏幕”是指Launcher,其源代码是免费提供的。如果这不能提供您所寻找的答案,请考虑解释一下您真正需要的是什么。 - mbafford
3个回答

5
最简单的方法是通过检测“Fling”手势来实现。Android API内置了基本手势检测器,例如fling、scroll、long press、double tap、pinch zoom等等。
文档可以在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;
    }
}

2

1

这意味着我基本上必须自己创建手势,并且没有固定的左右滑动手势可用?这个手势甚至可以在我的 Android 1.5 手机的启动屏幕上使用,而你提供的类是在 Android 1.6 中引入的。 - Christian
@Christian,像快速滑动这样简单的操作,并不需要你去构建复杂的手势。系统内置了方便的类来处理这种常见情况。 - CodeFusionMobile

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接