简体   繁体   English

Android MultiTouch不起作用

[英]Android MultiTouch does not work

 @Override 
public boolean onTouchEvent(MotionEvent event)
{ 
    synchronized (getHolder())
    {   
        int aktion = event.getAction();
        if (aktion == MotionEvent.ACTION_DOWN)
        {
            touched = true;
            bar.shareTouch(event.getX(), event.getY(), touched);
        }
        else if (aktion == MotionEvent.ACTION_UP)
        {
            touched = false;
            bar.shareTouch(event.getX(), event.getY(), touched);
        }
            if (aktion == MotionEvent.ACTION_POINTER_DOWN)
        {
            touched2 = true;
            bar.shareTouch2(event.getX(), event.getY(), touched2);
        }
        else if (aktion == MotionEvent.ACTION_POINTER_UP) 
        {
            touched2 = false;
            bar.shareTouch2(event.getX(), event.getY(), touched2);
        }
    }
    return true;
}

This is a Code to chech if first Finger is going onto the screen or leaving it. 如果第一个手指进入屏幕或离开屏幕,这是一个检查代码。 The same for another finger. 另一只手指也一样。

public void shareTouch2(float xTouch2, float yTouch2, boolean touched2)
{
    if (xTouch2 <= gameViewWidth/2) {
        if (touched2 == true) {
            touchedLeft2 = true;
        }
        else if(touched2 == false) {
            touchedLeft2 = false;
        }
    }
    else if (xTouch2 > gameViewWidth/2) {
        if (touched2 == true) {
            touchedRight2 = true;
        }
        else if(touched2 == false) {
            touchedRight2 = false;
        }
    }
}

public void shareTouch(float xTouch, float yTouch, boolean touched)
{
    if (xTouch <= gameViewWidth/2) {
        if (touched == true) {
        touchedLeft = true;
        }
        else if(touched == false) {
            touchedLeft = false;
        }
    }
    else if (xTouch > gameViewWidth/2) {
        if (touched == true) {
            touchedRight = true;
        }
        else if(touched == false) {
            touchedRight = false;
        }
    }

}

private void moveRight()
{
    x += 3;
}

private void moveLeft()
{
    x -= 3;
}

private void checkTouch() {
    if ((touchedLeft == true && touchedRight2 == false) || (touchedLeft2 == true && touchedRight == false)) {
        moveLeft();
    }
    else if ((touchedLeft == false && touchedRight2 == true) || (touchedLeft2 == false && touchedRight == true)) {
        moveRight();
    }
    else if ((touchedLeft == true && touchedRight2 == true) || (touchedLeft2 == true && touchedRight == true)) {

    }
}

The checkTouch() is called in the onDraw() Method. onDraw()方法中调用checkTouch() Now if I place a finger on the right side of the screen it moves right. 现在,如果我将手指放在屏幕的右侧,它将向右移动。 Same for left. 左边也一样。 But if I touch left and the right without removing the left finger the Object still moves left although it should stop. 但是,如果我在不移开左手指的情况下左右触摸对象,尽管该对象应该停止,但它仍然向左移动。 Now when I leave the left finger it still moves left although it should move right. 现在,当我离开左手手指时,尽管它应该向右移动,但它仍然向左移动。

I hope you understand my problem. 希望你能理解我的问题。

Hope you can help 希望你能帮忙

You can't just assume the ACTION_POINTER_UP Event will only be sent for the same finger ACTION_POINTER_DOWN was sent. 您不能仅仅假设仅在发送ACTION_POINTER_DOWN的同一手指上发送ACTION_POINTER_UP事件。

Just as the ACTION_DOWN event is always fired for the first finger to touch down, the ACTION_UP event is only fired for the last finger to lift (ie when there are no more fingers on the screen). 就像始终要触发第一个手指ACTION_UPACTION_DOWN事件一样,仅要ACTION_UP 最后一根手指(即,当屏幕上没有手指时)才触发ACTION_UP事件。 All others will get an ACTION_POINTER_UP event, even if it's the finger that started the gesture. 所有其他人都将获得ACTION_POINTER_UP事件,即使是开始手势的手指也是如此。

However, the Android documentation does specify 但是,Android文档确实指定了

Each pointer has a unique id that is assigned when it first goes down [and] remains valid until the pointer eventually goes up. 每个指针都有一个唯一的ID,该ID在它第一次下降时分配[并且]保持有效,直到指针最终上升。

So in theory, your first and second finger should still have the same ID, regardless whether the Event that reports them. 因此,从理论上讲,无论是否报告事件,您的第一根和第二根手指仍应具有相同的ID。

The solution is simple, then: Use getPointerCount() and getPointerID() to keep track of your fingers. 然后,解决方案很简单:使用getPointerCount()getPointerID()来跟踪您的手指。

This may be easier if you refactor your code to account for more than two fingers by replacing the Booleans touchedLeft and touchedLeft2 with a single counter, eg fingersOnLeftSide - which has the neat side effect of only needing one shareTouch() method and otherwise reducing redundancy in your code. 如果您通过用单个计数器(例如, fingersOnLeftSide替换布尔值touchedLefttouchedLeft2来重构代码以touchedLeft两个以上的手指,这可能会更容易,例如,它具有仅需要一个shareTouch()方法的整洁副作用,否则会减少冗余您的代码。

EDIT: 编辑:

Disclaimer: This is just off the top of my head, untested and written without knowledge of any of your code other than what you posted. 免责声明:这就是我的头上问题,未经测试,没有编写任何其他代码(而不是发布的内容)。 Not necessarily the best way to solve your problem, but the shortest I could think of. 解决问题的最佳方法不一定,但我能想到的最短。

This is the Event handler: 这是事件处理程序:

public boolean onTouchEvent(MotionEvent event)
{
    synchronized (getHolder())
    {   
        int aktion = event.getAction();
        if (aktion == MotionEvent.ACTION_DOWN || aktion == MotionEvent.ACTION_POINTER_DOWN
        || aktion == MotionEvent.ACTION_UP || aktion == MotionEvent.ACTION_POINTER_UP)
        {
            bar.clearTouches();
            for (int i = 0; i < event.getPointerCount(); i++) {
                bar.shareTouch(event.getX(i)); // we don't need the y coordinate anyway
            }
        }
    }
    return true;
}

The rewritten shareTouch() to go along with it, as well as clearTouches() I introduced to make this simpler: 改写的shareTouch()使用,以及我引入的clearTouches()使之更简单:

private void shareTouch(float x) 
{
    if (x < gameViewWidth/2) {
        fingersLeft++;
    } else {
        fingersRight++;
    }
}

private void clearTouches() 
{
    fingersLeft = 0;
    fingersRight = 0;
}

And finally, the new checkTouch() : 最后,新的checkTouch()

public void checkTouch()
{
    if (fingersLeft > fingersRight) {
        moveLeft();
    } else if (fingersRight > fingersLeft) {
        moveRight();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM