我使用clipPath从画布进行剪辑,然后驱动:
@Override
protected void onDraw(Canvas canvas)
{
float radius = ((float) getWidth()) / 2;
clipPath.reset();
clipPath.addCircle(radius, radius, radius, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
当然,我在构造函数和 XML 中为此视图设置了 View.LAYER_TYPE_SOFTWARE。
但在某些设备上(Android 3):
LENOVO K1
acer A501
samsung GT-P6200
ZTE V9S
当我尝试绘制裁剪的画布时抛出异常:
java.lang.UnsupportedOperationException
at android.view.GLES20Canvas.clipPath(GLES20Canvas.java:303)
at com.myapp.ui.RoundedCornerImageView.onDraw(RoundedCornerImageView.java:35)
at android.view.View.draw(View.java:9292)
at android.view.View.getDisplayList(View.java:8755)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2296)
at android.view.View.getDisplayList(View.java:8719)
at android.view.ViewGroup.drawChild(ViewGroup.java:2554)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
at android.view.View.getDisplayList(View.java:8753)
at android.view.ViewGroup.drawChild(ViewGroup.java:2554)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
at android.widget.AbsListView.dispatchDraw(AbsListView.java:2029)
at android.view.View.draw(View.java:9399)
at android.widget.AbsListView.draw(AbsListView.java:3315)
at android.view.View.getDisplayList(View.java:8755)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2296)
at android.view.View.getDisplayList(View.java:8719)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2296)
at android.view.View.getDisplayList(View.java:8719)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2296)
at android.view.View.getDisplayList(View.java:8719)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2296)
at android.view.View.getDisplayList(View.java:8719)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2296)
at android.view.View.getDisplayList(View.java:8719)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2296)
at android.view.View.getDisplayList(View.java:8719)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2296)
at android.view.View.getDisplayList(View.java:8719)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2296)
at android.view.View.getDisplayList(View.java:8719)
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:691)
at android.view.ViewRoot.draw(ViewRoot.java:1624)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1440)
at android.view.ViewRoot.handleMessage(ViewRoot.java:2070)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4143)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
有趣的是,该应用程序不会立即崩溃,而是会在一段时间后崩溃。堆栈上面的开始时间是 2013-03-31 16:04:04,crashe 时间是 2013-03-31 16:18:47。虽然 RoundedCornerImageView 显示在 MainActivity!