Während Virendra Singhs Antwort richtig ist, habe ich das Gefühl, dass Pfade in einem allgemeinen Fall zu schwierig sein könnten.
Um alle Fälle zu behandeln, benötigen Sie einen Test pro Pixel. Pro-Pixel-Test zeichnet die Ansicht und testet das berührte Pixel, wenn es anklickbar ist. Der Code ist sehr kurz:
public class BitmapButton extends Button {
private final Bitmap bitmap;
private final Canvas canvas;
public BitmapButton(Context context, AttributeSet attrs) {
super(context, attrs);
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
}
public boolean onTouchEvent(MotionEvent event) {
bitmap.setPixel(0, 0, 0); // reset pixel
canvas.save();
canvas.translate(-event.getX(), -event.getY());
draw(canvas); // draw only the touched pixel
canvas.restore();
// pass touch events when the pixel is clickable
return Color.alpha(bitmap.getPixel(0, 0)) > 0 && super.onTouchEvent(event);
}
}