自圆润的线性布局

自圆润的线性布局

本文介绍了自圆润的线性布局:添加边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了圆形的自定义的LinearLayout 我要添加1px的边框。

I have developed a custom rounded LinearLayout and I want to add 1px border.

下面我code:

public class MyLinearLayout extends LinearLayout {

private float radius;
private Path path = new Path();
private RectF rect = new RectF();


public MyLinearLayout(Context context)
{
    super(context);
    radius = 20;
    setWillNotDraw(false);
}

@Override
protected void onDraw(Canvas canvas) {
    path.reset();
    rect.set(0, 0, canvas.getWidth(), canvas.getHeight());
    path.addRoundRect(rect, radius, radius, Direction.CCW);
    // Add 1px border RED here ?
    path.close();
    canvas.clipPath(path);
}

}

感谢您的帮助。

推荐答案

如果您在圆角在您的自定义仔细一看的LinearLayout ,你可能会看到半径并不顺利。这是因为有是不支持路径抗锯齿系统限制

If you look closely at the rounded corners in your custom LinearLayout, you will probably see that the radius is not smooth. This is because there is a system limitation that does not support anti aliasing on Paths.

下面是埃里克·伯克一个伟大的如何正确地创建查看带圆角。基本上你将创建一个离屏位图,得出一个 RoundRectangle ,并使用alpha合成技术来合并关闭-screen 位图与自定义的LinearLayout 画布 位图

Here is a great tutorial from Erik Burke on how to properly create Views with rounded corners. Basically you will create an off-screen Bitmap, draw a RoundRectangle, and use an alpha compositing technique to merge the off-screen Bitmap with the custom LinearLayout Canvas' Bitmap.

至于边框,可以通过设置绘制 Paint.Style.FILL_AND_STROKE 油漆

As far as a border, you can can draw it by setting Paint.Style.FILL_AND_STROKE to the Paint.

这篇关于自圆润的线性布局:添加边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:54