绘制的LinearLayout带有圆角 [英] Drawing LinearLayout with rounded corners

查看:2303
本文介绍了绘制的LinearLayout带有圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的LinearLayout子类绘制自身带有圆角。从我的研究,我设置 setWillNotDraw(假)和覆盖的OnDraw()绘制在画布上一个圆角矩形:

I'm trying to implement a LinearLayout subclass that draws itself with rounded corners. From my research, I set setWillNotDraw(false) and overridden onDraw() to draw a rounded rectangle in the canvas:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int sc = canvas.saveLayer(0, 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, 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例子),但面积应该是透明的(舍入1)具有代替黑色背景,并且孩子的角仍然可见。这是一个Galaxy Nexus的采用Android 4.2.2的结果:

DST_IN seems the correct option here (according to the APIDemos example), but the area that should be transparent (the rounded one) has instead a black background, and the corners of the children are still visible. This is the result on a Galaxy Nexus with Android 4.2.2:

任何提示?

编辑:这就是我想实现的,对不起PS技术的粗陋:)

Here is what I'd like to achieve, sorry for the crudeness of photoshopping :)

编辑2:我加入到GitHub上的例子可运行的项目:<一href="https://github.com/venator85/RoundClippingLayout">https://github.com/venator85/RoundClippingLayout

EDIT 2: I added to GitHub an example runnable project: https://github.com/venator85/RoundClippingLayout

感谢;)

推荐答案

不太一样: 罗曼盖伊做了关于使用位图的着色器的图像裁剪角落的博客帖子..不知道如果你想延长同样的事情。

Not quite the same : Romain Guy did a blog post about cropping corners on images using bitmap shaders.. Not sure if you would want to extend the same thing.

http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

这篇关于绘制的LinearLayout带有圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆