Java.lang.UnsupportedOperationException on canvas

I used custom ImageView

in my application. But he shows

java.lang.UnsupportedOperationException

in the code canvas.clipPath(clipPath)

.

I don't know why I got this error. It works great on some phones.

public class Gma_CustomImage2 extends ImageView {

    public static float radius = 10.0f;  

    public Gma_CustomImage2(Context context) {
        super(context);
    }

    public Gma_CustomImage2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

    @Override
    protected void onDraw(Canvas canvas) {
        Path clipPath = new Path();
        RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight()+13.0f);
        clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.onDraw(canvas);
    }
}

      

LogCat

08-08 12:04:47.963: E/AndroidRuntime(5710): FATAL EXCEPTION: main
08-08 12:04:47.963: E/AndroidRuntime(5710): java.lang.UnsupportedOperationException
08-08 12:04:47.963: E/AndroidRuntime(5710):     at    android.view.GLES20Canvas.clipPath(GLES20Canvas.java:408)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at com.example.meetap.Gma_CustomImage2.onDraw(Gma_CustomImage2.java:34)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.View.draw(View.java:10982)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.View.getDisplayList(View.java:10421)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.View.getDisplayList(View.java:10384)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.View.getDisplayList(View.java:10384)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.View.getDisplayList(View.java:10384)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewGroup.drawChild(ViewGroup.java:2850)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.View.draw(View.java:10985)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.widget.FrameLayout.draw(FrameLayout.java:450)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.widget.ScrollView.draw(ScrollView.java:1524)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.View.getHardwareLayer(View.java:10220)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewGroup.drawChild(ViewGroup.java:2863)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at com.example.meetap.Gma_CustomViewBehind.dispatchDraw(Gma_CustomViewBehind.java:121)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.View.getDisplayList(View.java:10419)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewGroup.drawChild(ViewGroup.java:2850)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.View.getDisplayList(View.java:10419)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.View.getDisplayList(View.java:10384)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.View.getDisplayList(View.java:10384)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.View.getDisplayList(View.java:10384)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:842)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewRootImpl.draw(ViewRootImpl.java:1935)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1659)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2467)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.os.Looper.loop(Looper.java:137)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at android.app.ActivityThread.main(ActivityThread.java:4424)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at java.lang.reflect.Method.invokeNative(Native Method)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at java.lang.reflect.Method.invoke(Method.java:511)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-08 12:04:47.963: E/AndroidRuntime(5710):     at dalvik.system.NativeStart.main(Native Method)

      

+3


source to share


3 answers


use this code:



if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
   Gma_CustomImage.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

      

+2


source


You must turn off hardware acceleration for clipPath()

. More specifically, call

this.setLayerType(View.LAYER_TYPE_SOFTWARE, null)

(as stated on the page here ). Basically you want to leave hardware acceleration for the rest of your application, but disable it on purpose in this situation when clipPath()

not supported with it.



Also look at the table on the page.

0


source


Work with Canvas.clipPath () which is no longer supported in Android app -

clipPath

supported only when hardware acceleration is disabled.

The main idea here is to disable hardware acceleration in the part of the application where you need to use unsupported methods. You can do this for a specific view, there is no need to completely disable it for the entire application.

0


source







All Articles