【问题标题】:UnsupportedOperationException in GLES20Canvas.clipPath with hardware acceleration disabled on viewGLES20Canvas.clipPath 中的 UnsupportedOperationException 在视图中禁用了硬件加速
【发布时间】:2012-04-16 20:36:55
【问题描述】:

我在我的应用程序中启用了硬件加速,但我为我的一个视图禁用了它,因为我遇到了笔画上限和其他问题。

现在我在 Google Play 崩溃错误控制台中获取此堆栈跟踪:

java.lang.UnsupportedOperationException
at android.view.GLES20Canvas.clipPath(GLES20Canvas.java:287)
at com.myapp.MyCustomView.onDraw(SourceFile:288)
at android.view.View.draw(View.java:9310)
at android.view.View.getDisplayList(View.java:8773)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2298)
...
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:609)
at android.view.ViewRoot.draw(ViewRoot.java:1634)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1450)
at android.view.ViewRoot.handleMessage(ViewRoot.java:2094)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
...

我在 AndroidManifest.xml 中指定了android:hardwareAccelerated="true"。但我在自定义视图的构造函数中专门禁用了硬件加速:

    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);

            // ... code omitted

            // disable acceleration because Paint.setStrokeCap(Cap.ROUND) is not working otherwise
            Compatibility.disableHardwareAcceleration(this);
    }

兼容性是这样的:

public class Compatibility {
        // View.setLayerType() was introduced in Honeycomb
        private static Method setLayerTypeMethod = getMethod(View.class, "setLayerType", int.class,
                        Paint.class);

        private static Method getMethod(Class<?> clazz, String name, Class<?>... parameterTypes) {
                try {
                        return clazz.getMethod(name, parameterTypes);
                }
                catch (NoSuchMethodException e) {
                        return null;
                }
        }

        private Compatibility() {
        }

        public static void disableHardwareAcceleration(View view) {
                try {
                        if (setLayerTypeMethod != null) {
                                int layerType = 1; // View.LAYER_TYPE_SOFTWARE
                                setLayerTypeMethod.invoke(view, layerType, null);
                        }
                }
                catch (Exception ignored) {
                }
        }
}

很遗憾,崩溃错误控制台没有显示有关 Android 操作系统版本或设备的信息。

有什么想法吗?

【问题讨论】:

    标签: android hardware-acceleration


    【解决方案1】:

    存在一个已知问题,即即使设置了 LAYER_TYPE_SOFTWARE,仍然使用硬件加速绘制视图。详情here

    作为一种解决方法,您可以做两件事

    1. 使用Canvas.isHardwareAccelerated() 跳过有问题的代码。
    2. 将有问题的东西绘制到位图中,然后使用 canvas.drawBitmap() 将其绘制到硬件加速视图上。

    【讨论】:

      猜你喜欢
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多