简体   繁体   中英

Android layout touch issue

I am building a test lock screen app. I have the design working, and it works, but wherever you press, it registers as it is at the number 1 position, and then makes the design relative to that. Here is the main design:

主图

Now If I click on the first position, it registers one, if I drag to 2 or 3, etc. it registers those numbers in the order that I drag. But If I want to start at position 2, it still registers as position 1 and so everything "shifts positon" (not the design, only the points of the rectangle).

How can I fix this? I am getting this, wherever I click (say I click position 2):

正在显示位置1

But I need this:

在此处输入图片说明

here is my Java Code that makes the rectangles and when they are touched:

public void onWindowFocusChanged (boolean hasFocus){
    super.onWindowFocusChanged(hasFocus);       
        b1R = new Rect(b1.getLeft(), b1.getTop(), b1.getRight(), b1.getBottom());
        b2R = new Rect(b2.getLeft(), b2.getTop(), b2.getRight(), b2.getBottom());
        b3R = new Rect(b3.getLeft(), b3.getTop(), b3.getRight(), b3.getBottom());
        b4R = new Rect(b4.getLeft(), b4.getTop()+120, b4.getRight(), b4.getBottom()+120);
        b5R = new Rect(b5.getLeft(), b5.getTop()+120, b5.getRight(), b5.getBottom()+120);
        b6R = new Rect(b6.getLeft(), b6.getTop()+120, b6.getRight(), b6.getBottom()+120);
        b7R = new Rect(b7.getLeft(), b7.getTop()+240, b7.getRight(), b7.getBottom()+240);
        b8R = new Rect(b8.getLeft(), b8.getTop()+240, b8.getRight(), b8.getBottom()+240);
        b9R = new Rect(b9.getLeft(), b9.getTop()+240, b9.getRight(), b9.getBottom()+240);

}
public boolean onTouch(View arg0, MotionEvent arg1) {
    // TODO Auto-generated method stub
    try {
        Thread.sleep(50);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    x = (int) arg1.getX();
    y = (int) arg1.getY();

    if(b1R.contains(x, y)){
        arr.add("1");
        Log.d("block", "1");
    }
    if(b4R.contains(x,y)){
        arr.add("4");
        Log.d("block", "4");
    }
    if(b7R.contains(x,y)){
        arr.add("7");
        Log.d("block", "7");
    }

    if(b2R.contains(x,y)){
        arr.add("2");
        Log.d("block", "2");
    }
    if(b8R.contains(x,y)){
        arr.add("8");
        Log.d("block", "8");
    }
    if(b5R.contains(x,y)){
        arr.add("5");
        Log.d("block", "5");
    }

    if(b3R.contains(x,y)){
        arr.add("3");
        Log.d("block", "3");
    }
    if(b6R.contains(x,y)){
        arr.add("6");
        Log.d("block", "6");
    }
    if(b9R.contains(x,y)){
        arr.add("9");
        Log.d("block", "9");
    }

    return false;
}

The reason the bXr.contains is in that order is because if it was 0-9 order, it would not register correctly, it would freak out and register 1, 2 and 3 (then 4, 5 and 6, and then 7, 8 and 9) all in the same block for some reason.

Any ideas?

I do not know what exacly you use, but I bet you have nine (Image)Views in a Layout and registered all their touchevents to one listener. The effect you describe would happen if you do so, because touch events are in the subcoordinate system of the touched view.

You can lay a transparent View above everything and use the touch events arriving there. Then it would work. Or you draw everything in one view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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