简体   繁体   中英

Android Drawable getTransparentRegion() not working

I'm trying to get the dimensions of the transparent region of a PNG.

Region transparentRegion = drawable.getTransparentRegion();

My Drawables are 100% valid as far as I can tell, all of their functions are working as desired.

The getTransparentRegion call doesn't fail, but any attempt to do anything with the returned region is a null pointer exception and causes my app to crash.

05-01 20:54:14.207: E/AndroidRuntime(19159): java.lang.NullPointerException 05-01 20:54:14.207: E/AndroidRuntime(19159): at android.graphics.Region.setPath(Region.java:105)

Tested on both 2.3.7 and 4.2

Any ideas on what could be causing this?

What I really want to do is fine the non-transparent area of my images (always rectangular), if there is an even more direct way to achieving this my ears are open.

I think you are misunderstanding the function getTransparentRegion() .

That function, in the default Android implementation ALWAYS returns null. Here is the quote from the documentation:

The default implementation returns null, indicating no transparent region; subclasses can optionally override this to return an actual Region if they want to supply this optimization information, but it is not required that they do so.

It also says you can make your own custom class and override this function and program some clever pixel checker that looks for transparent pixels and regions and return something intelligible. (so basically, if you want this to be useful, you have to use your own logic to do so).

This call has been in since API 1. Now if you are like me, you might think "why in the world would they make a method like that and always return null?" The only answer I can come up with for that is "at the time it seemed the logical thing to do."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM