简体   繁体   English

Android多点触控:ACTION_UP并不总是被调用?

[英]Android multitouch: ACTION_UP not always called?

I have developed an Android application that is handling multitouch in a view. 我开发了一个在视图中处理多点触控的Android应用程序。 I basically track the several MotionEvents that can occur like ACTION_UP, ACTION_MOVE, ... My overwritten onTouch method in the View class looks like this: 我基本上跟踪可能出现的几个MotionEvent,如ACTION_UP,ACTION_MOVE,...我在View类中覆盖的onTouch方法如下所示:

public boolean onTouch(View view, MotionEvent event) 
{
    int action = event.getAction() & MotionEvent.ACTION_MASK;

    if (action == MotionEvent.ACTION_DOWN) {
        float x = event.getX();
        handleTouchStart(view, x);
    } else if (action == MotionEvent.ACTION_UP) {
        float x = event.getX();
        handleTouchEnded(view, x);
    } else if (action == MotionEvent.ACTION_POINTER_DOWN) {
        int pointer = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
        float x = event.getX(pointer);
        handleTouchStart(view, x);
    } else if (action == MotionEvent.ACTION_POINTER_UP) {
        int pointer = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
        float x = event.getX(pointer);
        handleTouchEnded(view, x);
    } else if (action == MotionEvent.ACTION_MOVE) {
        int pointer = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
        float x = event.getX(pointer);
        handleTouchMoved(view, x);
    }

    return true;
}

On my phone (Samsung Galaxy S), this works flawlessly. 在我的手机(三星Galaxy S)上,这完美无瑕。 For every handleTouchStart(), the appropriate handleTouchEnded() is called. 对于每个handleTouchStart(),都会调用相应的handleTouchEnded()。 This is crucial for my app. 这对我的应用至关重要。 It's a 2D sidescroller with a steering area in the left lower portion of the screen. 它是一个2D侧面滚轮,在屏幕的左下部有一个转向区域。 If - for some reason - the handleTouchEnded() method is not called when the user lifts a finger from the touchscreen, the player's character continues running. 如果 - 由于某种原因 - 当用户从触摸屏抬起手指时不调用handleTouchEnded()方法,则播放器的角色继续运行。

I've received reports from players on other devices (like the HTC Desire) that in rare occasions, the character does indeed continue running in a direction even if they lifted their finger from the steering area on the screen. 我收到了来自其他设备(如HTC Desire)的玩家的报告,在极少数情况下,即使他们的手指从屏幕上的转向区域抬起,角色也确实继续向某个方向跑。 I concluded that this can only happen if handleTouchEnded() is not called which in turn means that no ACTION_UP (or ACTION_POINTER_UP) event is generated. 我的结论是,只有在未调用handleTouchEnded()时才会发生这种情况,这反过来意味着不会生成ACTION_UP(或ACTION_POINTER_UP)事件。

Are there device specific implementations of multitouch support? 是否有针对多点触控支持的设备特定实现? Is there no guarantee that for each ACTION_DOWN (or ACTION_POINTER_DOWN) MotionEvent, an appropriate ACTION_UP (or ACTION_POINTER_UP) MotionEvent is called? 是否无法保证每个ACTION_DOWN(或ACTION_POINTER_DOWN)MotionEvent都调用适当的ACTION_UP(或ACTION_POINTER_UP)MotionEvent? Or am I missing something? 或者我错过了什么? Is my onTouch implementation even correct? 我的onTouch实现是否正确?

My broader question would be: are there better ways to handle multitouch touchscreen input for "action"-games? 我更广泛的问题是:是否有更好的方法来处理“动作”游戏的多点触控触摸屏输入? I'm basically mimicking the left and right controls of a gamepad's steering cross. 我基本上模仿了游戏手柄转向十字架的左右控制。 I've found that Android's UI buttons don't offer enough flexibility because they can't track onPress and onRelease events... or do they? 我发现Android的UI按钮没有提供足够的灵活性,因为它们无法跟踪onPress和onRelease事件......或者它们呢?

You're missing something. 你错过了什么。 ;) There is one more action type that you should handle: ACTION_CANCEL . ;)还有一个你应该处理的动作类型: ACTION_CANCEL ACTION_CANCEL is sent when the gesture in progress is aborted at some higher level - some higher-level component may have intercepted the gesture and taken it over, etc. 当正在进行的手势在某个更高级别中止时发送ACTION_CANCEL - 某些更高级别的组件可能截获了手势并将其取消等等。

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

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