我试图实现一个LinearLayout子类,它使用圆角绘制自己.从我的研究中,我设置setWillNotDraw(false)并覆盖onDraw()在画布中绘制一个圆角矩形:
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int sc = canvas.saveLayer(0,getWidth(),getHeight(),drawPaint,Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG); canvas.drawRoundRect(bounds,mCornerRadius,roundPaint); canvas.restoreToCount(sc); }
哪里:
drawPaint = new Paint(Paint.ANTI_ALIAS_FLAG); drawPaint.setColor(0xffffffff); drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); roundPaint.setColor(0xffffffff);
DST_IN在这里是正确的选择(根据APIDemos的例子),但是应该是透明的区域(圆角的)改为黑色背景,孩子的角落仍然可见.这是使用Android 4.2.2的Galaxy Nexus的结果:
任何提示?
编辑:这是我想要实现的,对于拍摄的粗鲁感到遗憾:)
编辑2:我向GitHub添加了一个可运行的示例项目:https://github.com/venator85/RoundClippingLayout
谢谢 原文链接:https://www.f2er.com/android/312424.html