【问题标题】:Implementing Dead Zone for the touch screen in Android在 Android 中为触摸屏实现死区
【发布时间】:2012-08-16 08:01:03
【问题描述】:

我需要在我的 APK 中为触摸屏实现一种死区,这样 Activity 就不会从这些区域接收任何触摸事件。 一旦多点触控事件与第一次触地相关联,这似乎并非易事。

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

对于单次触摸来说,这是微不足道的。

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

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

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

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

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

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

谢谢。

【问题讨论】:

    标签: java android touch processing multi-touch


    【解决方案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 方法,您会看到,当我们在“死区”上进行单次或多次触摸时,我们会得到初步响应,但没有进一步的反应。 但是,当我们第一次触摸“活区”而第二次触摸死区时,情况会怎样呢?

    对于这种情况,您应该在 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;
        }
    
    };
    

    所以在这种特殊情况下,我们可以检查触摸何时发生在“活动区域”下,然后简单地忽略它或实现我们想要的任何行为。

    希望这会有所帮助。 问候 何塞

    【讨论】:

    • 对不起,这恰好是我对 Android 的早期尝试。我不再与那个项目有任何关系(实际上将近两年)。我记得我实施了一种相同的解决方案。我的触摸屏处理过滤了区域外的点击和移动,移除了额外的坐标。在 ent 我成功过滤了他们想要编辑的内容,但项目本身早就完成了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2016-08-13
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多