Work around Canvas.clipPath() that is not supported in android any more

clipPath with hardware acceleration is only supported in API level 18 and higher, on API levels from 11 to 17 it needs to be turned off.

The article you’ve mentioned contains a clue:

If your application is affected by any of these missing features or limitations, you can turn off hardware acceleration for just the affected portion of your application by calling setLayerType(View.LAYER_TYPE_SOFTWARE, null). This way, you can still take advantage of hardware acceleratin everywhere else. See Controlling Hardware Acceleration for more information on how to enable and disable hardware acceleration at different levels in your application.

The main idea here is to disable hardware acceleration in the part of the application where you need to use the unsupported methods on devices with the API level lower than 18. You can do it for a particular view, there’s no need to turn it off completely for the whole application.

If you don’t want to turn off hardware acceleration, then I would suggest using Porter-Duff modes. You can create a bitmap with a circle in it, then draw your image onto the canvas using such a mode that would clip your image to the original content.

Leave a Comment