在Android中为触摸屏实现死区

时间:2012-08-16 08:01:03

标签: java android touch processing multi-touch

我需要在我的APK中为触摸屏实现一种死区,以便活动不会从这些区域接收任何触摸事件。 一旦多点触摸事件与第一次触摸相关联,这似乎并不是微不足道的。

假设我有一个带有两个并排视图的布局,并且想要忽略右视图中的所有触摸活动(“死”视图是不可触摸的)。

单次触摸它是微不足道的。

但是对于多点触控,第一次触摸会决定一切:

  1. 如果第一个处于“死”视图中,我根本没有任何事件。

  2. 如果第一个处于“生命”视图中而第二个处于“死”视图中,则会收到多点触控事件ACTION_POINTER_DOWN_2。

  3. 目前,我必须接收原始流中的所有事件,并根据我的“死区”规则将其转换为另一个事件流。

    但问题是:我们是否有任何有用的API将触摸屏事件处理限制在我们只需要的区域?

    P.S。在玩游戏时,我需要所有这些来过滤屏幕两侧附近的意外触摸。

    感谢。

1 个答案:

答案 0 :(得分:1)

如果你想要两个布局,一个是实时布局,一个布局布局,你应该为它们各自添加View.OnTouchListener

我做了一个例子,这是活动:

public class MainActivity extends ActionBarActivity {
TextView text;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    LinearLayout linearLayoutDead = (LinearLayout) findViewById(R.id.dead);
    linearLayoutDead.setOnTouchListener(onTouchDeadListener);
    LinearLayout linearLayoutLive = (LinearLayout) findViewById(R.id.live);
    linearLayoutLive.setOnTouchListener(onTouchLiveListener);

    text = (TextView) findViewById(R.id.textView);
}

private View.OnTouchListener onTouchDeadListener = new View.OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        text.setText(motionEvent.toString());
        return false;
    }

};

private View.OnTouchListener onTouchLiveListener = new View.OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        text.setText(motionEvent.toString());
        return true;
    }

};

}

布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="1">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/live"
    android:layout_weight="1"
    android:background="@color/background_floating_material_dark">
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/dead"
    android:layout_weight="1">
</LinearLayout>

我们覆盖OnTouch方法,当我们在&#34;死区&#34;上进行单次或多次触摸时,您会看到这种情况。我们得到了初步的回应,但没有进一步。 但是,当我们在&#34; live zone&#34;中进行第一次触摸时的情况是什么?还有第二个死人?

对于这种情况,您应该为OnTouch方法添加一个新条件,如下所示:

private View.OnTouchListener onTouchLiveListener = new View.OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if(motionEvent.getPointerCount()>1){
            if(motionEvent.getY(1) > someNumber){
                //Case when we touch the dead zone, TODO do some ...
            }
        }
        text.setText(motionEvent.toString());
        return true;
    }

};

因此,在这种特殊情况下,我们可以检查触摸何时发生在&#34; live zone&#34;并且完全忽略它或实现我们想要的任何行为。

希望这可能有所帮助。 问候 何