使用 path.lineTo() 时对角线比直线粗
Diagonal line appears thicker than straight line when using path.lineTo()
我创建了一个扩展 Shape
的 class,以便为我的按钮背景绘制自定义形状(带切角的矩形)。我遇到的问题是画成对角线的线看起来比直线粗得多:
如何使边框始终保持一致的宽度?我尝试将抗锯齿设置为 true/false 但没有任何区别
public class ShapeMaker extends Shape {
private float STROKE_WIDTH = 20.0f;
private final Paint border = new Paint();
private final Path path;
private float tl = 0; //Top Left
private float tr = 0; //Top Right
private float bl = 0; //Bottom Left
private float br = 0; //Bottom Right
public ShapeMaker(float tl, float tr, float bl, float br, int fillColor) {
path = new Path();
this.tl = tl;
this.tr = tr;
this.bl = bl;
this.br = br;
border.setColor(fillColor);
border.setStyle(Paint.Style.STROKE);
border.setStrokeWidth(STROKE_WIDTH);
border.setAntiAlias(true);
border.setDither(true);
border.setStrokeJoin(Paint.Join.ROUND);
border.setStrokeCap(Paint.Cap.ROUND);
}
@Override
protected void onResize(float width, float height) {
super.onResize(width, height);
path.reset();
path.moveTo(0 + tl, 0);
path.lineTo(width - tr, 0);
path.lineTo(width, tr);
path.lineTo(width, height-br);
path.lineTo(width-br, height);
path.lineTo(0+bl, height);
path.lineTo(0, height-bl);
path.lineTo(0, 0+tl);
path.lineTo(0 + tl, 0);
path.close();
}
@Override
public void draw(Canvas canvas, Paint paint) {
canvas.drawPath(path, border);
}
}
用法:
Button button = findViewById(R.id.buttom);
float den = getResources().getDisplayMetrics().density;
button.setBackground(new ShapeDrawable(new ShapeMaker(den * 15, 0, 0, 0, Color.RED)));
简短的回答是"move"你的线向内(远离视图的边缘)等于笔划宽度的一半。
你的路径精确地描绘了你视野的边缘。实际绘制笔划时,它以这条路径为中心。这意味着一半的笔画在视图的边界之外,因此被剪掉了。
但是,当您绘制路径的对角线部分时,您完全在视图的边界内绘制,因此不会发生裁剪。这使得对角线笔划看起来是其余笔划的两倍。
这是我编写的代码的等价物:
int inset = (int) border.getStrokeWidth() / 2;
path.reset();
path.moveTo(notch + inset, inset);
path.lineTo(width - inset, inset);
path.lineTo(width - inset, height - inset);
path.lineTo(inset, height - inset);
path.lineTo(inset, notch + inset);
path.close();
我创建了一个扩展 Shape
的 class,以便为我的按钮背景绘制自定义形状(带切角的矩形)。我遇到的问题是画成对角线的线看起来比直线粗得多:
如何使边框始终保持一致的宽度?我尝试将抗锯齿设置为 true/false 但没有任何区别
public class ShapeMaker extends Shape {
private float STROKE_WIDTH = 20.0f;
private final Paint border = new Paint();
private final Path path;
private float tl = 0; //Top Left
private float tr = 0; //Top Right
private float bl = 0; //Bottom Left
private float br = 0; //Bottom Right
public ShapeMaker(float tl, float tr, float bl, float br, int fillColor) {
path = new Path();
this.tl = tl;
this.tr = tr;
this.bl = bl;
this.br = br;
border.setColor(fillColor);
border.setStyle(Paint.Style.STROKE);
border.setStrokeWidth(STROKE_WIDTH);
border.setAntiAlias(true);
border.setDither(true);
border.setStrokeJoin(Paint.Join.ROUND);
border.setStrokeCap(Paint.Cap.ROUND);
}
@Override
protected void onResize(float width, float height) {
super.onResize(width, height);
path.reset();
path.moveTo(0 + tl, 0);
path.lineTo(width - tr, 0);
path.lineTo(width, tr);
path.lineTo(width, height-br);
path.lineTo(width-br, height);
path.lineTo(0+bl, height);
path.lineTo(0, height-bl);
path.lineTo(0, 0+tl);
path.lineTo(0 + tl, 0);
path.close();
}
@Override
public void draw(Canvas canvas, Paint paint) {
canvas.drawPath(path, border);
}
}
用法:
Button button = findViewById(R.id.buttom);
float den = getResources().getDisplayMetrics().density;
button.setBackground(new ShapeDrawable(new ShapeMaker(den * 15, 0, 0, 0, Color.RED)));
简短的回答是"move"你的线向内(远离视图的边缘)等于笔划宽度的一半。
你的路径精确地描绘了你视野的边缘。实际绘制笔划时,它以这条路径为中心。这意味着一半的笔画在视图的边界之外,因此被剪掉了。
但是,当您绘制路径的对角线部分时,您完全在视图的边界内绘制,因此不会发生裁剪。这使得对角线笔划看起来是其余笔划的两倍。
这是我编写的代码的等价物:
int inset = (int) border.getStrokeWidth() / 2;
path.reset();
path.moveTo(notch + inset, inset);
path.lineTo(width - inset, inset);
path.lineTo(width - inset, height - inset);
path.lineTo(inset, height - inset);
path.lineTo(inset, notch + inset);
path.close();