android-Canvas中的java.lang.UnsupportedOperationException
作者:互联网
我在应用程序中使用了自定义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)
解决方法:
使用此代码:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
Gma_CustomImage.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
标签:android-imageview,android-canvas,android 来源: https://codeday.me/bug/20191029/1958681.html