解决 Android 不再支持的 Canvas.clipPath() [英] Work around Canvas.clipPath() that is not supported in android any more

查看:32
本文介绍了解决 Android 不再支持的 Canvas.clipPath()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 android 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 为您解答.我找到了一个很好的例子来演示这种技术,基本上就是一个面具.

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.

解决此问题的最佳方法是仅在您从 11 到 17 运行 API 时调用 setLayerType(View.LAYER_TYPE_SOFTWARE, null):

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

我还用 try-catch 块包围了 clipPath() 调用,以避免意外的应用程序崩溃:

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

无论如何,不​​应该在 API >= 18 上抛出 UnsupportedOperationException.

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

参见 不支持的绘图操作

这篇关于解决 Android 不再支持的 Canvas.clipPath()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆