Round button in android.. avoid presses “outside” the button?

后端 未结 2 820
感动是毒
感动是毒 2020-12-16 04:26

I have created/tried to create, a round button for android by using the ImageButton \"widget\". But since that type of button is treated as a square and my png-image also is

2条回答
  •  再見小時候
    2020-12-16 04:51

    Try Pythagorean theorem and onTouch, simple and easy way to do it.

    public boolean inCircle(MotionEvent e, int radius, int x, int y) {
        int dx = e.x - x;
        int dy = e.y - y;
        double d = Math.sqrt((dx * dx) + (dy * dy));
        if(d < radius)
            return true;
        return false;
    }
    

    the x, y is the posision of the circle, the radius is the radius, and the e is the TouchEvent you have.

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        if(arg1.getAction() == MotionEvent.ACTION_DOWN){
               if(inCircle(arg1, radius, xCircle, yCircle){
                      //do whatever you wanna do here
                      }
                }
        return false;
    }
    

提交回复
热议问题