25

From android 3.0 the clipPath() method is no longer supported in devices with hardware acceleration turned on.(Read this article for more details).

I am working with canvas and I need to draw rounded image. Any ideas about how can I do that?

*I can't turn the hardware acceleration off, I am looking for other solution.

Answered: Tnx @Malcolm for your answer. I found a good example that demonstrate this technique, it's basically a mask.

Community
  • 1
  • 1
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216

2 Answers2

42

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.

Malcolm
  • 41,014
  • 11
  • 68
  • 91
  • 1
    Yeah but I only got one view, (this is usually what you got when working with canvas...) So I can't turn it off, this is why I am looking for work around solution. – Ilya Gazman Jan 17 '12 at 14:21
  • "It is not no longer supported, it is only not available with hardware acceleration turned on." The double-negatives here make this sentence difficult to read. Maybe rephrase to something like: "`clipPath` is only available with hardware acceleration turned off." – Leo Accend May 24 '14 at 00:35
  • @LeoAccend Why don't you just [suggest an edit](http://stackoverflow.com/help/editing) yourself? – Malcolm May 24 '14 at 01:43
  • Thanks, @Malcolm, I didn't realize I could do that! – Leo Accend May 28 '14 at 18:01
29

Canvas.clipPath() support with hardware acceleration has been reintroduced since API 18.

The best way to work around the problem is calling setLayerType(View.LAYER_TYPE_SOFTWARE, null) only when you are running on API from 11 to 17:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2
        && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    setLayerType(LAYER_TYPE_SOFTWARE, null);
}

I also surrounded the clipPath() call with a try-catch block to avoid unpredicted app crashes:

if (doClip) {
    try {
        canvas.clipPath(clipPath);
    } catch (UnsupportedOperationException e) {
        Log.e(TAG, "clipPath() not supported");
        doClip = false;
    }
}

Anyway, UnsupportedOperationException should never be thrown on API >= 18.

See Unsupported Drawing Operations

araks
  • 40,738
  • 8
  • 34
  • 39
  • Isn't this exactly the same as in the other answer, except for the mention of the fact that the API 18 introduced the hardware support for it? The latter should be added, of course. – Malcolm May 21 '15 at 19:00
  • Yes, it is... But I added the API level 18 info, explanations and code examples... All in a brief answer. – araks May 21 '15 at 19:43