Android 在视图中禁用硬件加速的情况下,GLES20Canvas.clipPath中的UnsupportedOperationException
我在我的应用程序中启用了硬件加速,但我在一个视图中禁用了它,因为我在笔划上限和其他方面有问题 现在,我在Google Play崩溃错误控制台中获得了以下堆栈跟踪:Android 在视图中禁用硬件加速的情况下,GLES20Canvas.clipPath中的UnsupportedOperationException,android,hardware-acceleration,Android,Hardware Acceleration,我在我的应用程序中启用了硬件加速,但我在一个视图中禁用了它,因为我在笔划上限和其他方面有问题 现在,我在Google Play崩溃错误控制台中获得了以下堆栈跟踪: java.lang.UnsupportedOperationException at android.view.GLES20Canvas.clipPath(GLES20Canvas.java:287) at com.myapp.MyCustomView.onDraw(SourceFile:288) at android.view.Vi
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) {
}
}
}
公共类兼容性{
//View.setLayerType()是在Honeycomb中引入的
私有静态方法setLayerTypeMethod=getMethod(View.class,“setLayerType”,int.class,
油漆类);
私有静态方法getMethod(类clazz、字符串名称、类…参数类型){
试一试{
返回clazz.getMethod(名称、参数类型);
}
捕获(无此方法例外){
返回null;
}
}
私有兼容性(){
}
公共静态无效禁用硬件加速(视图){
试一试{
if(setLayerTypeMethod!=null){
int layerType=1;//View.LAYER\u TYPE\u软件
调用(视图,layerType,null);
}
}
捕获(忽略异常){
}
}
}
不幸的是,崩溃错误控制台没有透露有关Android操作系统版本或设备的信息
有什么想法吗?有一个已知问题,即即使设置了LAYER_TYPE_软件,视图仍然使用硬件加速绘制。详情如下 作为解决方法,您可以做两件事