Pages

20 May 2014

Android Canvas's clipping functions doensn't work?

I was working on an Android's custom view to show a volume bar with an overlay gradient, it looks somewhat like to this:


My approach was calculate small volume bars's RectF then use Canvas's clipRect to clip the canvas, then draw the gradient and gray part on it. Everything worked well with Nexus 5 and Galaxy S2 LTE, but not with a NTTdocomo Android phone (all of them were 4.0 up).

At first I thought the divider between volume bars was too small but after double checked, it wasn't the reason. Then it took me 2 hours for many checks (Android API Samples) and searches, it's only because of something called Hardware Acceleration, as written in the developer page:

Hardware acceleration is enabled by default if your Target API level is >=14

 So, Hardware Acceleration must be enabled by default on all of my devices, then I tried to turn it off by putting this line into the Activity which contains my custom view:

android:hardwareAccelerated="false"

And it worked!!!

So all the blames should go to the Hardware Acceleration, but why did it still work with Hardware Acceleration ON on two other devices ?!
It's a question that I haven't but need to figure out later!

P/S: The more I work with Android, the more I see this sentence of a random guy on StackOverflow is very true:

Everything is so random with Android!

Hope this help someone out there to not waste 2 hours at work like I did lol, happy coding guys!

UPDATE: I just found this today (20140728) http://developer.android.com/guide/topics/graphics/hardware-accel.html. There is a table about Unsupported Drawing Operations, so you guys can take a look then enable/disable hardware acceleration to match your needs.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.