本文介绍了Android的0nTouch事件问题[向下滑动和UP]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IM具有这就需要像iPhone onTouch刷卡事件的应用程序。他们是

im having an application which needs onTouch swipe events like in Iphone. they are


  1. 向上滑动。

  2. 向下滑动。

  3. 请向左滑动。

  4. 向右滑动即可。

我实现onTouch事件如下。我得到了左右滑动即可行动correctly.but这是贯彻落实刷卡正确的方式向下和向上的动作。

i implemented onTouch event as following . and i got Swipe Left and Right actions correctly.but which is the correct way to implement the swipe Down and Up actions.

我的code:

    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");
}

您确定您写的code以上?

Are you sure you wrote the code above?

修改

好吧,我相信你。你所要做的主要是:

OK, I believe you. What you have to do is basically:

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事件问题[向下滑动和UP]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 11:13