“双指轻扫”的姿态? [英] “Two Finger Swipe” gesture?

查看:202
本文介绍了“双指轻扫”的姿态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现我的Andr​​oid应用两指滑动手势。我用下面的code:

 无效handleTouch(MotionEvent米){
//触及数
INT pointerCount = m.getPointerCount();
如果(pointerCount == 2){
    INT行动= m.getActionMasked();
    INT的actionIndex = m.getActionIndex();
    串actionString;
    TextView的电视=(的TextView)findViewById(R.id.testDiffText);
    开关(动作)
    {
        案例MotionEvent.ACTION_DOWN:
            GLOBAL_TOUCH_POSITION_X =(int)的m.getX(1);
            actionString =DOWN+当前+ GLOBAL_TOUCH_CURRENT_POSITION_X +preV+ GLOBAL_TOUCH_POSITION_X;
            tv.setText(actionString);
            打破;
        案例MotionEvent.ACTION_UP:
            GLOBAL_TOUCH_CURRENT_POSITION_X = 0;
            actionString =UP+当前+ GLOBAL_TOUCH_CURRENT_POSITION_X +preV+ GLOBAL_TOUCH_POSITION_X;
            tv.setText(actionString);
            打破;
        案例MotionEvent.ACTION_MOVE:
            GLOBAL_TOUCH_CURRENT_POSITION_X =(int)的m.getX(1);
            INT差异= GLOBAL_TOUCH_POSITION_X,GLOBAL_TOUCH_CURRENT_POSITION_X;
            actionString =差别+ DIFF +当前+ GLOBAL_TOUCH_CURRENT_POSITION_X +preV+ GLOBAL_TOUCH_POSITION_X;
            tv.setText(actionString);
            打破;
        案例MotionEvent.ACTION_POINTER_DOWN:
            GLOBAL_TOUCH_POSITION_X =(int)的m.getX(1);
            actionString =DOWN+当前+ GLOBAL_TOUCH_CURRENT_POSITION_X +preV+ GLOBAL_TOUCH_POSITION_X;
            tv.setText(actionString);
            打破;
        默认:
            actionString =;
    }    pointerCount = 0;
}
其他{
    GLOBAL_TOUCH_POSITION_X = 0;
    GLOBAL_TOUCH_CURRENT_POSITION_X = 0;
}

}

我得到在向上或向下轻扫执行相同的操作。它仅执行Action_up任务。任何人都可以请帮我实现这两个由上至下滑动和自下而上刷卡。


解决方案

 私有静态最终诠释NONE = 0;
私有静态最终诠释SWIPE = 1;
私人INT模式=无;
私人浮动startY;
私人浮动stopY;
//我们将只检测一个滑动如果该差值为至少100个像素
//将此值更改为您的需求
私有静态最终诠释TRESHOLD = 100;公共布尔onTouch(视图V,MotionEvent事件)
{
    开关(event.getAction()及MotionEvent.ACTION_MASK)
    {
        案例MotionEvent.ACTION_POINTER_DOWN:
            //这发生在你触摸屏幕用两个手指
            模式= SWIPE;
            //也可以使用event.getY(1)或两者的平均
            startY = event.getY(0);
            打破;        案例MotionEvent.ACTION_POINTER_UP:
            //出现这种情况,当你松开第二根手指
            模式=无;
            如果(Math.abs(startY - stopY)GT; TRESHOLD)
            {
                如果(startY> stopY)
                {
                    //向上滑动
                }
                其他
                {
                    //向下滑动
                }
            }
            this.mode =无;
            打破;        案例MotionEvent.ACTION_MOVE:
            如果(==模式SWIPE)
            {
                stopY = event.getY(0);
            }
            打破;
    }    返回true;
}

我希望这有助于。

i want to implement two finger swipe gesture in my android app. I have used the following code:

void handleTouch(MotionEvent m){
//Number of touches
int pointerCount = m.getPointerCount();
if(pointerCount == 2){
    int action = m.getActionMasked();
    int actionIndex = m.getActionIndex();
    String actionString;
    TextView tv = (TextView) findViewById(R.id.testDiffText);
    switch (action)
    {
        case MotionEvent.ACTION_DOWN:                   
            GLOBAL_TOUCH_POSITION_X = (int) m.getX(1);
            actionString = "DOWN"+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X;
            tv.setText(actionString);
            break;
        case MotionEvent.ACTION_UP:                 
            GLOBAL_TOUCH_CURRENT_POSITION_X = 0;
            actionString = "UP"+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X;
            tv.setText(actionString);  
            break;  
        case MotionEvent.ACTION_MOVE:
            GLOBAL_TOUCH_CURRENT_POSITION_X = (int) m.getX(1);
            int diff = GLOBAL_TOUCH_POSITION_X-GLOBAL_TOUCH_CURRENT_POSITION_X;
            actionString = "Diff "+diff+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X;
            tv.setText(actionString);                   
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            GLOBAL_TOUCH_POSITION_X = (int) m.getX(1);
            actionString = "DOWN"+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X;
            tv.setText(actionString);
            break;
        default:
            actionString = "";
    }

    pointerCount = 0;
}
else {
    GLOBAL_TOUCH_POSITION_X = 0;
    GLOBAL_TOUCH_CURRENT_POSITION_X = 0;
}

}

I'm getting same action performed in up or down swipe . it perform only Action_up task. can anyone please help me to implement both up to bottom swipe and bottom to up swipe.

解决方案

private static final int NONE = 0;
private static final int SWIPE = 1;
private int mode = NONE;
private float startY;
private float stopY;
// We will only detect a swipe if the difference is at least 100 pixels
// Change this value to your needs
private static final int TRESHOLD = 100;

public boolean onTouch(View v, MotionEvent event)
{
    switch (event.getAction() & MotionEvent.ACTION_MASK)
    {
        case MotionEvent.ACTION_POINTER_DOWN:
            // This happens when you touch the screen with two fingers
            mode = SWIPE;
            // You can also use event.getY(1) or the average of the two
            startY = event.getY(0);
            break;

        case MotionEvent.ACTION_POINTER_UP:
            // This happens when you release the second finger
            mode = NONE;
            if(Math.abs(startY - stopY) > TRESHOLD)
            {
                if(startY > stopY)
                {
                    // Swipe up
                }
                else
                {
                    //Swipe down
                }
            }
            this.mode = NONE;
            break;

        case MotionEvent.ACTION_MOVE:
            if(mode == SWIPE)
            {
                stopY = event.getY(0);
            }
            break;
    }

    return true;
}

I hope this helped.

这篇关于“双指轻扫”的姿态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆