简体   繁体   English

Android中具有多点触控的OnTouch

[英]OnTouch with multitouch in android

I am havig troubles counting how much touches are being made in each "half" of the screen. 我很难计算在屏幕的每个“一半”中进行了多少触摸。 When I touch in the upper side it counts correctly, and the same with the lower side. 当我触摸上侧时,它的计数正确,下侧也一样。 BUT, when I touch the upper side, dont release it, and touch the lower side, then it counts the wrong side, what is wrong in my implementation? 但是,当我触摸上侧时,不要松开它,然后触摸下侧,那么它算错了,在我的实现中有什么问题?

     myView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int action = event.getAction() & MotionEvent.ACTION_MASK;
                if (action == MotionEvent.ACTION_DOWN) {
                    if(event.getY()<400)
                    {


                    }
                    else
                    {

                    }
                }

                if (action == MotionEvent.ACTION_UP) {

                    if(event.getY()<400)
                    {

                        zenbat=zenbat+1;
                        tv.setText(String.valueOf(zenbat));

                    }
                    if(event.getY()>400)
                    {
                        zenbat1=zenbat1+1;
                        tv1.setText(String.valueOf(zenbat1));

                    }
                }

                if (action == MotionEvent.ACTION_POINTER_DOWN) {
                    if(event.getY()<400)
                    {                           
                    }
                    else
                    {
                    }
                }

                if (action == MotionEvent.ACTION_POINTER_UP) {
                    if(event.getY()<400)
                    {
                        zenbat=zenbat+1;
                        tv.setText(String.valueOf(zenbat));

                    }
                    if(event.getY()>400)
                    {
                        zenbat1=zenbat1+1;
                        tv1.setText(String.valueOf(zenbat1));

                    }
                } 
                return true;
            }
        });

If you want to correctly handle multitouch events you need to use the pointer index to correctly identify the finger generating the event. 如果要正确处理多点触摸事件,则需要使用指针索引正确识别生成事件的手指。

I've answered a similar question in Android multi-touch interference where I posted a code example how to do it. 我已经在Android多点触控干扰中回答了一个类似的问题,在该问题中,我发布了代码示例该如何做。

To correctly identify the finger, you should refer to the fingerId in the code posted. 要正确识别手指,您应该在发布的代码中引用fingerId

Regards. 问候。

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

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