Canvas中的java.lang.UnsupportedOperationException

时间:2014-08-08 06:32:39

标签: android android-canvas android-imageview

我在自己的应用中使用了自定义ImageView。但它显示

  

java.lang.UnsupportedOperationException

代码canvas.clipPath(clipPath)中的

我不知道为什么会出现这个错误。它适用于某些手机。

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 个答案:

答案 0 :(得分:3)

使用此代码:

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

答案 1 :(得分:1)

您必须关闭clipPath()的硬件加速才能正常工作。更具体地说,请致电

this.setLayerType(View.LAYER_TYPE_SOFTWARE, null)(由提到的here页面指定)。基本上,您希望在应用程序的其余部分保持硬件加速,但在这种情况下,特别是在不支持clipPath()的情况下将其关闭。

另请查看页面上的表格。

答案 2 :(得分:0)

Work around Canvas.clipPath() that is not supported in android any more -

仅在关闭硬件加速时才支持

clipPath

这里的主要思想是在需要使用不支持的方法的应用程序部分禁用硬件加速。您可以针对特定视图执行此操作,无需为整个应用程序完全关闭它。