Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

want to create a semi doughnut shape button clickable in a particular region

I want to create a semi doughnut shaped button which is only clickable in the region where it is visible and not in the the whole rectangular region.

https://i.sstatic.net/MKD45.png

I want clicking to affect only this blue region.

like image 427
Aman Rawat Avatar asked Dec 31 '25 04:12

Aman Rawat


1 Answers

You can do this by grabbing the Bitmap representation of the Button, then testing the x/y pixel's alpha value.

To get the bitmap for a button:

Bitmap buttonBmp;
button.setDrawingCacheEnabled(true);
buttonBmp = Bitmap.createBitmap(button.getDrawingCache());
button.setDrawingCacheEnabled(false);

I'd recommend only doing this once, and saving the results, that way you're not creating a new bitmap every time you touch the button.

Then you override the Button's onTouchEvent so you have the local x/y where the user tapped. If the alpha in that spot is 0, you have a non-clickable area. It's not as simple as an onClickListener, but it should do the job.

This way you can use any arbitrary shape, not just a doughnut. Colors, textures, whatever.

like image 104
Geobits Avatar answered Jan 02 '26 19:01

Geobits



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!