Problems with MotionEvent

I was wondering how to get the exact get (x) and get (y) values ​​for a MotionEvent? It happens that when I touch a certain area on the screen, I say that the action should happen. The problem is that as soon as I touch the screen and distract my finger, he still thinks that my finger is in one place (since this was the last place I touched). Therefore, when I have more than one Down event (for multi-touch), it drops everything. Is there a way to reset the values ​​of X and Y, so when I turn off the screen, they return to 0 or zero (or something else)?

Here is the video I just uploaded to explain it better, because it scares

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

And here is the exact code that I use

    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.

, , - . , , , , .

+1
2

, . , , , , . . startes ( , , - /). , .

: 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 . , multitouch, ; -)

. 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;
    }
}
+5

'motionEvent.getPoinerCount()' .

+1

All Articles