android中的圆形按钮..避免按下"外部"按钮?

Inx*_*Inx 7 android

我已经创建/尝试使用ImageButton"小部件"为Android创建一个圆形按钮.但由于这种类型的按钮被视为正方形而我的png图像也被视为具有透明背景的正方形,那么如何避免用户在圆形按钮外按压?...原因至于现在..他们可以按下按钮的"角落",这仍然会触发点击事件..是否有任何特殊的映射层可以在photoshop中完成或以任何方式更改图像按钮的半径,以便它适合我的形象的"圆度"......或任何想法?

提前谢谢!...对不起英语不好..

Dan*_*iel 5

尝试Pythagorean定理和onTouch,简单易行的方法.

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;
}
Run Code Online (Sandbox Code Playgroud)

x,y是圆的位置,半径是半径,e是你拥有的TouchEvent.

@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;
}
Run Code Online (Sandbox Code Playgroud)