UnsupportedOperationException on clipPath

2019-06-27 02:09发布

问题:

I am getting an UnsupportedOperationException when using Canvas clipPath.

This is happening on an HTC Sensation XE (4.0.3). It works perfectly on other 4.0.3 devices.

This is the code:

    @Override
protected void dispatchDraw(Canvas canvas) {
    canvas.save();
    canvas.clipPath(this._mClip);
    super.dispatchDraw(canvas);
    canvas.restore();
}

It fails on the "clipPath" with the following stack:

11-06 12:05:53.650: E/AndroidRuntime(25021): FATAL EXCEPTION: main

11-06 12:05:53.650: E/AndroidRuntime(25021): java.lang.UnsupportedOperationException
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.GLES20Canvas.clipPath(GLES20Canvas.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at com.applicat.meuchedet.views.RoundedListView.dispatchDraw(RoundedListView.java:76)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.View.draw(View.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.widget.AbsListView.draw(AbsListView.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.widget.ListView.draw(ListView.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.View.getDisplayList(View.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.ViewGroup.drawChild(ViewGroup.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.View.getDisplayList(View.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.ViewGroup.drawChild(ViewGroup.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.View.draw(View.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.View.getDisplayList(View.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.ViewGroup.drawChild(ViewGroup.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.View.getDisplayList(View.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.ViewGroup.drawChild(ViewGroup.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.View.getDisplayList(View.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.ViewGroup.drawChild(ViewGroup.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.View.draw(View.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.widget.FrameLayout.draw(FrameLayout.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.View.getDisplayList(View.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.ViewRootImpl.draw(ViewRootImpl.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.os.Handler.dispatchMessage(Handler.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.os.Looper.loop(Looper.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at android.app.ActivityThread.main(ActivityThread.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at java.lang.reflect.Method.invokeNative(Native Method)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at java.lang.reflect.Method.invoke(Method.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
11-06 12:05:53.650: E/AndroidRuntime(25021):    at dalvik.system.NativeStart.main(Native Method)

回答1:

There is an answer here. In short: Its no longer supported but you can still use it if you disable hardware acceleration. See post for more info.



回答2:

you can control hardware accelerations for individual activities by editing manifest file as either

<application android:hardwareAccelerated="true">
<activity ... />
<activity android:hardwareAccelerated="false" />



回答3:

Even I got the same error here is answer which helped me to run the application Android UnsupportedOperationException at Canvas.clipPath



回答4:

I ran into the same UnsupportedOperationException problem- when I had a view that tried to use Canvas clipPath(), it would crash only on devices running 4.0.3. You can see how I solved it here.



回答5:

As newer versions of Android do support clipPath() with hardware acceleration turned, this is probably a better solution:

try {
    canvas.clipPath(path);
} catch (UnsupportedOperationException exception) {
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    try {
        canvas.clipPath(path);
    } catch (UnsupportedOperationException exception2) {
        // shouldn't happen, but just in case 
    }
}