【问题标题】:MotionEvent Issues运动事件问题
【发布时间】:2011-10-27 17:41:24
【问题描述】:

我想知道如何为 MotionEvent 获取准确的 get(x) 和 get(y) 值?正在发生的事情是,当我触摸屏幕上的特定区域时,我会告诉一个动作发生。 问题是,一旦我触摸屏幕并松开手指,它仍然认为我的手指在同一个位置(因为那是我最后触摸的位置)。因此,当我有多个 Down 事件(用于多点触控)时,它会抛出所有问题。有没有办法重置 X 和 Y 值,所以当我离开屏幕时,它们会回到 0 或 null(或其他)?

这是我刚刚上传的一个视频,以便更好地解释它,因为它有点令人困惑

http://www.youtube.com/watch?v=OHGj2z5SwQs

这是我正在使用的确切代码

    int x = (int) e.getX();
    int y = (int) e.getY();
    int x2 = (int) e.getX(1);
    int y2 = (int) e.getY(1);


    boolean a1 = y > 0 && y < 200....etc        

    boolean a5 = etc... 

    switch (e.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        x =  0;
        y = 0;          
        x2 = 0;
        y2 = 0;
                    ////I'm setting the x and y values to 0 as suggested

        text.setText("x:" + String.valueOf(x) + "y:" + String.valueOf(y));
                    //// This is so I can see the values on the screen
        if (a1 && a5){
            viewA1.setBackgroundColor(Color.GREEN);
            viewA5.setBackgroundColor(Color.GREEN);
        }
        if (a1) {

            viewA1.setBackgroundColor(Color.GREEN);
        }



        else if (a5) {
            viewA5.setBackgroundColor(Color.GREEN);

        }           



        break;

    case MotionEvent.ACTION_POINTER_1_DOWN:
        // /A Strummer
        x =  0;
        y = 0;

        x2 = 0;
        y2 = 0;

        text1.setText("x:" + String.valueOf(x2) + "y:" + String.valueOf(y2));
        if (a1 && a5){

            viewA1.setBackgroundColor(Color.GREEN);
            viewA5.setBackgroundColor(Color.GREEN);

        }
        if (a1) {

            viewA1.setBackgroundColor(Color.GREEN);
        }



        else if (a5) {

            viewA1.setBackgroundColor(Color.GREEN);

        }       
     /////I have pretty much the same method for ACTION_UP & ACTION_POINTER_UP; I set x & y to 0.

如果你能想到什么,请告诉我。我尝试了你们解释的方法,似乎有帮助,但没有。

【问题讨论】:

    标签: android


    【解决方案1】:

    我没有提示,但有一个可行的解决方案和一些针对多点触控初学者的提示。我以前从来没有这样做过,所以在看到你的问题后,我很想知道多点触控是如何工作的。实际上,要正确完成它非常棘手。这是开始的一个很好的例子(当一个指针触摸它时,四个角会被着色 - 最多可以使用五个指针/手指)。因此,我将根据下面进一步发布的工作解决方案进行解释。

    基本上它是这样工作的:getPointerCount() 为您提供当前触摸指针的数量。 getX(i) 和 getY(i) 给你相应的坐标。但这部分很棘手:指针 0、1、2 正在接触,你抬起 1(第二根手指),现在你有 0、1。这就是 getPointerId(1) 将返回 2 的点,因为这仍然是你的指针 2很感人,但它现在位于位置 1 而不是 2。您有一次机会对该更改做出反应,这就是 ACTION_POINTER_UP 被解雇的时候。在这个动作和ACTION_POINTER_DOWN getActionIndex() 将返回从指针事件列表中添加或删除的指针的动作指针位置。否则它总是返回 0,这是没有人告诉你的。

    结论是您实际上无法跟踪哪个指针正在移动(这就是getActionIndex() 在移动时返回0 的原因)。这似乎合乎逻辑,因为为每个手指触发 5 个单独的事件是愚蠢的。但是,从多点触控开始,这是一个非常重要的事实 ;-)

    这是一个例子。 coordsX 和 coordsY 将跟踪指针坐标,如果位置 2 包含有效坐标,则意味着这是您的无名指,无论您是否抬起其他手指,它仍在触摸屏幕。在这种情况下,这可能无关紧要,但它在其他多点触控实现中仍然有用。

    public class MultitouchView extends LinearLayout {
    
        // we have a maximum of 5 pointer
        // we will set Integer.MIN_VALUE if the pointer is not present
        private int [] coordsX = new int [5];
        private int [] coordsY = new int [5];
    
        // add 4 views with a certain gravity so they are placed in corners
        public MultitouchView(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            // initialize as not present
            Arrays.fill(coordsX, Integer.MIN_VALUE);
            Arrays.fill(coordsY, Integer.MIN_VALUE);
    
    
            /* the layout is inflated from a XML file and is basically this one:
             * all subviews are matching / filling parent and have a weight of 1
             * <LinearLayout vertical>
             *   <LinearLayout horizontal>
             *     <View /><View />
             *   </LinearLayout>
             *   <LinearLayout horizontal>
             *     <View /><View />
             *   </LinearLayout>
             * </LinearLayout>
             */
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent e) {
            if (e.getAction() == MotionEvent.ACTION_CANCEL) {
                // every touch is going to be canceled, clear everything
                Arrays.fill(coordsX, Integer.MIN_VALUE);
                Arrays.fill(coordsY, Integer.MIN_VALUE);
            } else {
                // track all touches
                for (int i = 0; i < e.getPointerCount(); i++) {
                    int id = e.getPointerId(i);
    
                    if (e.getActionIndex() == i
                            && (e.getActionMasked() == MotionEvent.ACTION_POINTER_UP
                            || e.getActionMasked() == MotionEvent.ACTION_UP)) {
                        // pointer with this id is about to leave, clear it
                        coordsX[id] = coordsY[id] = Integer.MIN_VALUE;
                    } else {
                        // update all other pointer positions
                        coordsX[id] = (int) e.getX(i);
                        coordsY[id] = (int) e.getY(i);
                    }
                }
            }
    
            int hw = getWidth() / 2;
            int hh = getHeight() / 2;
    
            // first no corner is touched
            boolean topLeft = false, topRight = false, bottomRight = false, bottomLeft = false;
    
            // check if any of the given pointer is touching a certain corner
            for (int i = 0; i < coordsX.length; i++) {
                // pointer is not active (anymore)
                if (coordsX[i] == Integer.MIN_VALUE || coordsY[i] == Integer.MIN_VALUE) {
                    continue;
                }
    
                topLeft = topLeft || coordsX[i] < hw && coordsY[i] < hh;
                topRight = topRight || coordsX[i] > hw && coordsY[i] < hh;
                bottomRight = bottomRight || coordsX[i] > hw && coordsY[i] > hh;
                bottomLeft = bottomLeft || coordsX[i] < hw && coordsY[i] > hh;
            }
    
            // set the result we have now to the views represnting the corners
            ((ViewGroup) getChildAt(0)).getChildAt(0).setBackgroundColor(topLeft ? 0xffffff00 : 0x0);
            ((ViewGroup) getChildAt(0)).getChildAt(1).setBackgroundColor(topRight ? 0xffff0000 : 0x0);
            ((ViewGroup) getChildAt(1)).getChildAt(0).setBackgroundColor(bottomLeft ? 0xff00ff00 : 0x0);
            ((ViewGroup) getChildAt(1)).getChildAt(1).setBackgroundColor(bottomRight ? 0xff0000ff : 0x0);
    
            return true;
        }
    }
    

    【讨论】:

    • @Knickedi 我希望我可以这么说,但无论如何,对我来说,这只会让事情变得复杂。我不明白为什么我要实现的目标会像您建议的那样繁重,尽管我很可能是错的,因为我尝试使用的方法显然不起作用。
    • 有一个问题,我是否必须扩展 View 并使用 OnTouchEvent,或者我可以扩展 Activity 并在 OnTouch 上使用?我一直在使用后者,因为前者对我来说似乎更复杂。不过我不知道,我学习 Java/Android 才大约 6 周,在我遇到这个之前它似乎进展顺利,现在就像我碰到了一堵砖墙。我想我需要回到基础... D-:
    • 另外,我不确定你是否知道这个问题的答案,但为什么不是像第一根手指使用 ACTION_DOWN 和第二根手指使用 ACTION_POINTER_DOWN 一样简单,依此类推?
    • 最后,非常感谢您为提供帮助所付出的努力,非常感谢
    • 再次发帖很痛(这是评论区,不是聊天)。 Here a inflate article。你可以在底部的背景部分看到我所做的here。当开始使用 android 时,我认为基本的 Java 知识是一个很大的优势。如果您希望我们可以在聊天中见面。
    【解决方案2】:

    您应该使用“motionEvent.getPoinerCount()”来检查触摸点的数量。

    【讨论】:

    • @Peter Knego 我刚刚这样做了,PointerCount 是准确的。还有其他想法吗?
    猜你喜欢
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多