android - 运动事件问题

标签 android

我想知道如何为 MotionEvent 获取准确的 get(x) 和 get(y) 值?发生的事情是,当我触摸屏幕上的特定区域时,我会告诉一个 Action 发生。 问题是,一旦我触摸屏幕并移开手指,它仍然认为我的手指在同一位置(因为那是我最后触摸的位置)。因此,当我有多个 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.

如果你能想到什么,请告诉我。我尝试了你们解释的方法,看起来好像有用,但没有用。

最佳答案

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

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

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

这是例子。 coordsXcoordsY 将跟踪指针坐标,如果位置 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;
    }
}

关于android - 运动事件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7545591/

相关文章:

android - REACT NATIVE 的 PayPal 支付集成与最新的 lib android + ios

android - 改造和 Proguard 错误

android - 从 ViewPager fragment 隐藏 Android 应用程序中的软键盘

android - 我可以在 Android 上模拟指南针吗

android - 为什么发布构建抛出错误,但在 buildTypes 相同时不进行调试? (Android gradle 构建类型)

android - 获取另一个 Activity 的值(value)放在另一个 Activity 的 ListView 的编辑文本中

android - 我可以使 ListView 项目不可选吗?

java - 比较Android按钮的文本

java - 转换日期: d-M-yyyy to dd-MM-yyyy

java - 比较字符串出错