我使用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!
答案 0 :(得分:1)
我发现了这个错误:http://code.google.com/p/android/issues/detail?id=20474
仅在4.1(API 16)中修复。
我决定仅针对API 16及更高版本启用硬件加速
在清单中:
android:hardwareAccelerated="@bool/hw_acceleration"
在values / constants.xml中:
<bool name="hw_acceleration">false</bool>
在values-16 / constants.xml中:
<bool name="hw_acceleration">true</bool>
我希望,这将有效!