各地Canvas.clipPath(工作)未在Android的支持更多更多、工作、Canvas、clipPath

2023-09-12 22:04:44 作者:Fickle 薄情

从安卓3.0的clipPath()方法是不再支持在设备硬件加速开启。(阅读文章更多详细信息)。

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.

回答: TNX @Malcolm你的答案。我发现了一个很好的例子,展示this技术,它基本上是一个面具。

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

推荐答案

Canvas.clipPath()支持硬件加速,因为已经重新 API 18

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

要解决此问题最好的方法是调用 setLayerType(View.LAYER_TYPE_SOFTWARE,空)只有当你在API从11运行到17:

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);
}

我还包围了 clipPath()用try-catch块调用,以避免联合国predicted应用程序崩溃:

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;
    }
}

总之,UnsupportedOperationException异常不应该在API抛出> = 18。

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

请参阅的不支持的绘图操作