首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何仅获取最大被触摸的指针数

如何仅获取最大被触摸的指针数
EN

Stack Overflow用户
提问于 2019-08-21 02:00:42
回答 1查看 108关注 0票数 0

我正在尝试构建我的第一个android游戏,我只想让手指一次触摸的最大数量。

在屏幕上,我显示了一个介于1-5之间的数字,我希望用户用这个数量的手指触摸屏幕。

当我设置onTouchListener,并且用户放置(例如)3个手指时,我还会触发1个和2个手指指示。

有没有办法只获取最大的手指数量,或者在使用getPointerCount()方法计算指针之前等待几毫秒?

我在ACTION_DOWN内部和外部都尝试过使用这种方法。

我使用ifswitch尝试解决这个问题,但无法使其正常工作。

代码语言:javascript
运行
复制
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextView tv = (TextView)findViewById(R.id.textView9);
    ConstraintLayout my = (ConstraintLayout)findViewById(R.id.activity_motion_event);

    my.setOnTouchListener(new ConstraintLayout.OnTouchListener(){

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            pointerCount =  motionEvent.getPointerCount();

            if (pointerCount > maxCount)
                maxCount = pointerCount;

            handleTouch(maxCount);

            switch(motionEvent.getAction()) {

                case MotionEvent.ACTION_UP:
                    findViewById(R.id.textView1).setBackgroundColor(Color.BLUE);
                    findViewById(R.id.textView2).setBackgroundColor(Color.BLUE);
                    findViewById(R.id.textView3).setBackgroundColor(Color.BLUE);
                    findViewById(R.id.textView4).setBackgroundColor(Color.BLUE);
                    findViewById(R.id.textView5).setBackgroundColor(Color.BLUE);

                    int val = (Integer.valueOf(tv.getText().toString()));
                    if (Integer.valueOf(tv.getText().toString()) == pointerCount) {
                        tv.setBackgroundColor(Color.WHITE);
                    } else {
                        tv.setBackgroundColor(Color.YELLOW);
                    }

                    maxCount = 0;
                    tv.setText(Integer.toString((int)(1+Math.random()*((5-1)+1))));
                    break;
            }

            return true;
        }
    });

    private void handleTouch(int pointerCount) {
        switch(pointerCount) {
            case 5:
                findViewById(R.id.textView5).setBackgroundColor(Color.WHITE);
                break;
            case 4:
                findViewById(R.id.textView4).setBackgroundColor(Color.WHITE);
                break;
            case 3:
                findViewById(R.id.textView3).setBackgroundColor(Color.WHITE);
                break;
            case 2:
                findViewById(R.id.textView2).setBackgroundColor(Color.WHITE);
                break;
            case 1:
                findViewById(R.id.textView1).setBackgroundColor(Color.WHITE);
                break;
        }
    }
}

我让屏幕识别所有的指针触摸值(就像我说的,如果我触摸3个手指,我会得到1,2和3),但我只想得到3,所以相关的textView会亮起。

EN

回答 1

Stack Overflow用户

发布于 2019-08-21 04:41:39

当ACTION_DOWN事件发生时(即屏幕上的第一次触摸),发布一个具有小延迟的runnable,当Runnable在延迟后执行时,在间隔内一次注册的最大触摸点将在` in`maxCount变量中可用。

请尝试以下代码

代码语言:javascript
运行
复制
private Runnable runnable;
private Handler myHandler = new Handler();
private boolean isRunning = false;
private int pointerCount = 0;
private int maxCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextView tv = (TextView)findViewById(R.id.textView9);
    ConstraintLayout my = (ConstraintLayout)findViewById(R.id.activity_motion_event);


    my.setOnTouchListener(new ConstraintLayout.OnTouchListener(){

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            pointerCount =  motionEvent.getPointerCount();

            if (pointerCount>maxCount)
                maxCount=pointerCount;

              if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                 if(!isRunning) setRunnable();
            }else if (motionEvent.getAction() == MotionEvent.ACTION_UP){
                if (!isRunning){
                    //set to initial state();
                }
            }

            return true;


        }
    });

}



private void setRunnable() {
         isRunning = true;
         runnable = new Runnable() {

        @Override
        public void run() {
            isRunning = false;

             if (pointerCount != 0){
               handleTouch(maxCount);
               pointerCount = 0;
             }
            maxCount = 0;

    }
};

myHandler.postDelayed(runnable, 1000);


}

}

也是onDestroy中处理程序的removeCallbacks,也就是-添加到下面的行,以避免活动被销毁后的回调

代码语言:javascript
运行
复制
 @Override
protected void onDestroy() {
    myHandler.removeCallbacks(runnable);
    super.onDestroy();
}

更详细地说,我觉得你遇到的问题是

有没有办法只获取最大的手指数量,或者在使用getPointerCount()方法计算指针之前等待几毫秒?

这就是Runnables和Handler发挥作用的地方。如果您希望在不阻塞(暂停线程执行)的情况下将一段代码的执行延迟一段时间,则可以使用Handler.postDelayed()方法进行延迟,因为parameters.Here Runnable可用于发送延迟的任务。

我建议你研究一下android中的线程以获得更深层次的知识,https://medium.com/@ankit.sinhal/handler-in-android-d138c1f4980e https://en.proft.me/2017/04/15/understanding-handler-android/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57579254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档