Get button coordinates and detect if finger is over them - Android

前端 未结 4 1360
迷失自我
迷失自我 2020-11-30 07:58

I have 4 buttons in my android app activity. This is what it looks like:

As yo

4条回答
  •  离开以前
    2020-11-30 09:05

    private boolean checkInterSection(View view, int rawX, int raxY) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int x = location[0];
        int y = location[1];
        int width = view.getWidth();
        int height = view.getHeight();
        //Check the intersection of point with rectangle achieved 
        return (!(rawX < x || rawY > x + width || rawY < y || rawY > y + height)); 
    }
    
    for(int i = 0; i < touchview.getChildCount(); i++){
        if(checkInterSection(touchview.getChildAt(i), event.getRawX(), event.getRawY())){
            if(checkInterSection(touchview.getChildAt(i), event.getRawX(), event.getRawY())){
                ((Button)touchview.getChildAt(i)).setBackgroundColor(Color.BLUE);// Type casting may not be required 
            }else{
                ((Button)touchview.getChildAt(i)).setBackgroundColor(Color.WHITE);
            }
            break;
        }
    }
    

提交回复
热议问题