不规则图形按钮非透明区的点击
/**
* 不规则"图形按钮控件"
*
*/
public class TrapezoidImageButton extends ImageButton {
public TrapezoidImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TrapezoidImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TrapezoidImageButton(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (isTouchPointInView(event.getX(),event.getY())||
event.getAction() != MotionEvent.ACTION_DOWN){
return super.onTouchEvent(event);
}else{
return false;
}
}
protected boolean isTouchPointInView(float localX, float localY){
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
draw(canvas);
int x = (int)localX;
int y = (int)localY;
if (x < 0 || x >= getWidth())
return false;
if (y < 0 || y >= getHeight())
return false;
int pixel = bitmap.getPixel(x,y);
if ((pixel&0xff000000) != 0){ //点在非透明区
return true;
}else{
return false;
}
}
} 相关推荐
大地飞鸿 2020-11-12
星星有所不知 2020-10-12
jinxiutong 2020-07-26
MIKUScallion 2020-07-05
songfens 2020-07-05
songfens 2020-06-11
songfens 2020-06-08
northwindx 2020-05-31
northwindx 2020-05-31
northwindx 2020-05-27
northwindx 2020-05-25
MIKUScallion 2020-05-25
jinxiutong 2020-05-10
xdyangxiaoromg 2020-05-10
大地飞鸿 2020-05-06
northwindx 2020-04-25