Android 4.0 compatibility issues with Canvas.clipP

2020-07-23 05:07发布

问题:

Recently my application received quite a lot comments that "it's not working" on Android Ice Cream Sandwich with CM9. I'm not able to reproduce the error on the emulator running Android 4.0 and thanks to the way android market works there's no way I can't contact those people to ask about the details.

Luckily, one crash error report caught my eye. I'm using Canvas.clipPath to draw rounded corners... and looks like some phones are throwing UnsupportedOperationException when trying to use that function. Quick look at Google reveals that it seems to be a problem when using hardware acceleration in Android 4.0 - so that's the potential reason of received comments.

The question is - what's going on? Weren't applications hardware accelerated in previous android versions? Why such common function isn't supported? What's the workaround?

Example usage of Canvas.clipPath can be found in my other post. Check the accepted answer here: Android rounded corners in ListView

Thanks in advance

回答1:

In ICS, hardware acceleration was turned on by default. Until 4.0, the default was that hardware acceleration was off. Hardware acceleration does not support clipPath (and a few other operations, see more here http://android-developers.blogspot.com/2011/03/android-30-hardware-acceleration.html) I am facing the same issue. As a quick fix I will turn off acceleration for the entire application, and later I will rewrite the code not to use clipPath



回答2:

You can turn off hardware acceleration only on the specific view that is causing problems. Check out my answer here: https://stackoverflow.com/a/14054331/596708