GLES20Canvas.clipPath中的UnsupportedOperationException,在视图上禁用了硬件加速

时间:2012-04-16 12:21:56

标签: android hardware-acceleration

我在我的应用程序中启用了硬件加速功能,但我已将其禁用,因为我遇到了中风帽等问题。

现在我在Google Play Crash Errors控制台中收到了这个堆栈跟踪:

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

不幸的是,Crash Errors控制台不会显示有关Android OS版本或设备的信息。

有什么想法可能会发生什么?

1 个答案:

答案 0 :(得分:6)

即使设置了LAYER_TYPE_SOFTWARE,仍然存在使用硬件加速仍然绘制视图的已知问题。详细信息为here

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

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