【问题标题】:Android: detect if area is being pressed with any of the fingersAndroid:检测是否用任何手指按下区域
【发布时间】:2011-07-22 20:23:28
【问题描述】:

我有一个 SurfaceView,我正在画一个圆圈。我知道圆的位置和半径。

我需要知道用户是否按下了圆圈。用户可能正在用一根或两根手指轻敲屏幕。如果有任何手指在圆圈区域上方,则必须按下它。

当用户将手指从屏幕上抬起,而圆圈上不再有手指时,必须停止按下圆圈。

当用户只有一根手指在屏幕上时我没有问题,但当他使用两根手指时我无法解决问题。

我遇到的问题是当我收到一个 ACTION_UP 或 ACTION_POINTER_UP 动作时,我不知道哪个指针不再出现在屏幕上,所以我不必查看它们的坐标是否在圆上。

我尝试了几次都没有成功,最后一个是:

protected boolean checkPressed(MotionEvent event) {

    ColourTouchWorld w = (ColourTouchWorld)gameWorld;

    int actionMasked = event.getActionMasked();

    for (int i = 0; i < event.getPointerCount(); i++) {
        if (i == 0 && (actionMasked == MotionEvent.ACTION_UP || actionMasked == MotionEvent.ACTION_POINTER_UP)) {
            // the pointer with index 0 is no longer on screen, 
            // so the circle is not pressed by this pointer, even if
            // it's coordinates are over the area of the circle

            continue;
        }

        if (isPointInCicle(event.getX(i)), event.getY(i))) {
           return true;
        }
    }

    return false;
}

有什么想法吗?谢谢。

【问题讨论】:

    标签: android pointers touch multi-touch


    【解决方案1】:

    在问题中写的方法中,我假设收到的操作引用索引为 0 的指针。我错了,但我需要使用 ACTION_POINTER_INDEX_MASK

    方法的正确实现是:

    protected boolean checkPressed(MotionEvent event) {
    
    ColourTouchWorld w = (ColourTouchWorld)gameWorld;
    
    int actionMasked = event.getActionMasked();
    int pointerIndex = ((event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT);
    
    for (int i = 0; i < event.getPointerCount(); i++) {
        if (i == pointerIndex  && (actionMasked == MotionEvent.ACTION_UP || actionMasked == MotionEvent.ACTION_POINTER_UP)) {
            // the pointer with index 0 is no longer on screen, 
            // so the circle is not pressed by this pointer, even if
            // it's coordinates are over the area of the circle
    
            continue;
        }
    
        if (isPointInCicle(event.getX(i)), event.getY(i))) {
           return true;
        }
    }
    
    return false;
    }
    

    【讨论】:

      【解决方案2】:

      您需要使用ACTION_POINTER_INDEX_MASK 常量。我从来没有实现过这个,所以我不知道代码会是什么样子。但我认为您将需要使用它。

      【讨论】:

      • 是的,很好!!我假设收到的操作引用索引为 0 的指针。我错了,我需要使用 ACTION_POINTER_INDEX_MASK。
      猜你喜欢
      • 1970-01-01
      • 2011-09-18
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      相关资源
      最近更新 更多