Android 0nTouch 事件问题 [Swipe DOWN and UP]

标签 android

我有一个应用程序需要像 Iphone 中的 onTouch 滑动事件。他们是

  1. 向上滑动。
  2. 向下滑动。
  3. 向左滑动。
  4. 向右滑动。

我按如下方式实现了 onTouch 事件。我得到了正确的向左和向右滑动操作。但这是实现向下和向上滑动操作的正确方法。

我的代码:

    float downXValue,downYValue;

    @Override
    public boolean onTouchEvent(MotionEvent arg1) {

         // Get the action that was done on this touch event
        switch (arg1.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                // store the X value when the user's finger was pressed down
                downXValue = arg1.getX();
                downYValue = arg1.getY();
                break;
            }

            case MotionEvent.ACTION_UP:
            {
                // Get the X value when the user released his/her finger
                float currentX = arg1.getX();   
                float currentY=arg1.getY();

                // going backwards: pushing stuff to the right
                if (downXValue < currentX)
                {
                    Log.d(DEBUG_TAG, "Right");
                }

                // going forwards: pushing stuff to the left
                if (downXValue > currentX)
                {

                    Log.d(DEBUG_TAG, "Left");
                }
                break;
            }
        }

         //GestureListener is called here... 

        //return gestures.onTouchEvent(event);


        return true;
    }

谢谢。

最佳答案

那...呢

if (downYValue < currentY)
{
    Log.d(DEBUG_TAG, "Down");
}

你确定上面的代码是你写的吗?

编辑

好吧,我相信你。你要做的基本上是:

double sizeInX = Math.abs(downXValue - currentX);
double sizeInY = Math.abs(downYValue - currentY);
if( sizeInX > sizeInY ){
   // you better swipe horizontally
} else {
   // you better swipe vertically
}

关于Android 0nTouch 事件问题 [Swipe DOWN and UP],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5698839/

相关文章:

android - 自定义 Spinner TextView 被切断

python - Android SL4A (Python) 强制停止包失败。

android - 从特定域的 webview 获取 cookie?

android - 如何使用 Gradle 将 Android 配置注入(inject)每个子项目?

java - 更新到 7 后 Butterknife @InjectView 问题

Android获取设备方向

android - 可以重新创建 Android keystore

android - 在 kotlin 中使用 when 循环

android - 从本地服务向 Activity 发送消息的最简单方法是什么

java - 我在 dalvik.system.PathClassLoader.findClass 中的启动器主题 java.lang.ClassNotFoundException 上不断收到此错误