Goo*_*ofy 3 android rounded-corners android-shapedrawable
看看下面的代码.
ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape());
shapeDrawable.getPaint().setColor(Color.parseColor("#5a2705"));
shapeDrawable.getPaint().setStyle(Style.STROKE);
shapeDrawable.getPaint().setAntiAlias(true);
shapeDrawable.getPaint().setStrokeWidth(2);
shapeDrawable.getPaint().setPathEffect(new CornerPathEffect(10));
Run Code Online (Sandbox Code Playgroud)
我将此作为背景应用于我LinearLayout,但边缘不平滑.我怎样才能解决这个问题?
以下是它的外观截图.
使用以编程方式创建的形状可绘制为视图背景会导致笔划宽度的外半部分被裁剪掉(原因我不知道).仔细观察你的图像,你会发现你的笔划只有1像素宽,即使你要求2.这就是为什么角落看起来很难看.如果您尝试更大的笔划和半径(例如分别为10和40),则此效果将更加明显.
要么使用XML drawable,这似乎没有这个问题,比如Harshit Jain的回答,或者如果你必须(或者更喜欢)使用程序化解决方案,请执行以下操作.
解决方案:使用图层列表按要剪裁的量(笔画宽度的一半)插入矩形,如下所示:
float strokeWidth = 2;
ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape());
shapeDrawable.getPaint().setColor(Color.parseColor("#5a2705"));
shapeDrawable.getPaint().setStyle(Style.STROKE);
shapeDrawable.getPaint().setAntiAlias(true);
shapeDrawable.getPaint().setStrokeWidth(strokeWidth);
shapeDrawable.getPaint().setPathEffect(new CornerPathEffect(10));
Drawable[] layers = {shapeDrawable};
LayerDrawable layerDrawable = new LayerDrawable(layers);
int halfStrokeWidth = (int)(strokeWidth/2);
layerDrawable.setLayerInset(0, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth);
Run Code Online (Sandbox Code Playgroud)
然后使用它layerDrawable作为你的背景.以下是上述代码结果的屏幕截图:

| 归档时间: |
|
| 查看次数: |
1800 次 |
| 最近记录: |