Android register touch events on visible portion of image?

蹲街弑〆低调 提交于 2019-12-11 11:49:33

问题


I've currently been thinking about how I could register only touch events on the visible (non-transparent) parts of a .PNG image.

-I've been testing around with AndEngine and it seems they have multiple options: I've tried any I could find to no avail.

-I could very possibly create my own method of checking a given touched area's transparency I suppose, but not sure how much work/overhead that might create with 15-20 objects on screen being able to be touched..

Any help is much appreciated!


回答1:


One simple way to do it would be to grab the the pixel color at the touch location. Then you can check if the pixel is transparent:

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);

Note: Depending on how you implement your touch listener will might need to convert the x,y location of the touch event to the x,y coordinates of the image view.



来源:https://stackoverflow.com/questions/7547890/android-register-touch-events-on-visible-portion-of-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!