如何使按钮可点击或仅在图像可见区域上可触摸?

时间:2011-12-02 08:10:01

标签: android

我正在使用自定义RadioButton,我只需要在图像可见区域上进行点击。 正如您在下图所示,我只需要在图像部分上进行点击,即仅白色和绿色部分,黑色区域将保持透明且无法点击。

enter image description here

谢谢, 任何帮助将非常感激。

2 个答案:

答案 0 :(得分:1)

我认为检测图像的“可见”内容是否被点击的最简单方法是连接OnTouchListener,获取触摸坐标,然后使用Bitmap.getPixel(int x, int y)获取这些坐标的颜色。由于这将返回ARBG颜色,因此使用Alpha通道的图像应该没什么问题。任何“透明”(或在这种情况下为黑色)都将无效,其他一切都将意味着实际内容被点击。

答案 1 :(得分:0)

一种简单的方法是抓住触摸位置的像素颜色。然后你可以检查像素是否透明:

int color = Bitmap.getPixel(x,y); // x and y are the location of the touch event in Bitmap space
int alpha = Color.getAlpha(color);
boolean isTransparent = (alpha==0);

更多详情here