I have a set of imageButtons
placed within a relative layout, and each imageButton
has a shape within it that is visible while the rest of it is set to alpha. I have currently set these buttons to slightly overlap, and I am trying to code it so that when the alpha part of one button is pressed, it ignores that button and checks the button underneath it.
I am currently using onTouch()
with an OnTouchListener
to get the x
and y
coordinates of the touch on the screen, but that is calculated based on the whole screen from what I can tell. Is there a way to use the position found from event.getX()
and event.getY()
to look at where the button is on the screen and see if that spot clicked on the button is transparent or not?
Asked
Active
Viewed 226 times
0

Vyacheslav
- 26,359
- 19
- 112
- 194

user3686124
- 55
- 1
- 11
1 Answers
0
Use View.getLocationOnScreen()
and/or getLocationInWindow()
.
https://stackoverflow.com/a/2226048/1979882
In order to check if alpha-channel exists, I would use:
public static Bitmap loadBitmapFromView(View v) {
Bitmap bitmap;
v.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
return bitmap;
}
and than detect the ARGB value to a particular pixel.
int pixel = bitmap.getPixel(x,y);
Now you can get each channel with:
int alphaValue = Color.alpha(pixel);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);

Community
- 1
- 1

Vyacheslav
- 26,359
- 19
- 112
- 194
-
So using this with the coordinates found with getx() and gety() would give me the absolute coordinates within the view where the user touched the screen, is that correct? With that though, is it possible to see if that point on the button located at that spot in the view is transparent or not? – user3686124 Jan 28 '16 at 12:00
-
After trying this method, it seems to be checking for alpha coloration in the background of the layout instead of in the spot on the button. is the button not included in the bitmap when it is created from the view? Also, should looking for alphaValue use Color.alpha(pixel)? – user3686124 Jan 28 '16 at 12:35
-
@user3686124 , yes. try the other method to get screenshot (the other answers in the link) of the imagebutton. Or do not use the imagebutton view. – Vyacheslav Jan 28 '16 at 12:56