如何实现"双指拖"姿态在Android?如何实现、姿态、双指拖、QUOT

2023-09-13 00:28:34 作者:人间惆怅客

我是新的Andr​​oid开发和正在工作的一个辅助研究项目为盲人(果冻豆API级别17项目)。我一直在尝试用一些手势和在二指拖的姿态已经真的很难实现。下面的图像捕捉什么,我实际上需要相当好。

I am new to Android development and am working on an accessibility research project for blind people (Jelly Bean API level 17 project). I have been experimenting with some gestures and the Two-Finger-Drag gesture has been really tough to implement. The following image captures what I actually require quite well.

我要盲人用户可以将两个手指在水平方向(在屏幕任意位置),他会获得他/她的EditText上键入的文本的音频输出。还根据这两个手指行进的同时拖动的距离,我们分别输出每个单词。(下文实施例)

I want the blind user to drag two fingers across horizontally (anywhere on the screen) and he would obtain an Audio output of the text he/she typed in the EditText. Also according to the distance the two fingers travel while dragging, we output each word separately.(Example below)

例如:如果用户输入的今天是个好日子的并拖动他的手指   (X值)(说)10个单位到左,我们输出的好日子的,但如果他   拖动它说的20个单位到左,我们输出的美好的一天的,为30个单位   到左的是个好日子的等等等等。

Example: If the user types "Today is a good day" and drags his finger (x value) by (say) 10 units to Left we output "good day" but if he drags it say 20 units to Left we output "a good day", for 30 units to Left "is a good day" etc etc.

我偶然发现这似乎发现两指触控:

I stumbled across which seems to detect two-finger touch:

TouchEvent(MotionEvent event)

另外这个教程检测多个触摸的似乎是有前途的,但我需要它来工作的接触和拖这我不知道可以这样来实现。

Also this tutorial on detecting multiple touches seems promising, but I need it to work for touch and drag which I am not sure can be implemented like this.

任何新的建议,以实现这个或指针教程,可以帮助将是巨大的!

Any new suggestions to implement this or pointers to tutorials that can help would be great!

在此先感谢, 平硐

推荐答案

好了,感谢加布在这里,并在此众多的博客,我已经找到了解决我的问题!

Okay So thanks to Gabe here and numerous blogs on this, I have found a solution to my question!

首先,我在活动类初始化我的变量

int GLOBAL_TOUCH_POSITION_X = 0;
int GLOBAL_TOUCH_CURRENT_POSITION_X = 0;

下一步,里面的onCreate():

//Two-Finger Drag Gesture detection
    RelativeLayout TextLoggerLayout = (RelativeLayout)findViewById(R.id.ActivityrView);
    TextLoggerLayout.setOnTouchListener(
            new RelativeLayout.OnTouchListener(){

                @Override
                public boolean onTouch(View v, MotionEvent m) {
                    handleTouch(m); 
                    return true;
                }

    });

现在定义功能handleTouch(M)如下:,它输出了双指触控随着触摸的初始位置的当前位置:

Now define the function handleTouch(m) as follows, it outputs the current position of the "Two-finger-touch" along with the initial position of the touch:

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

你有它! 两手指拖动的姿态终于实现。 看起来,就像我可以在其上wrte一个微小的博客文章以及! :)

There you have it! The "Two-finger-drag" gesture finally implemented. Looks, like I can wrte a tiny blog post on it as well!! :)