gpt4 book ai didi

android - 使用 path.lineTo() 时对角线看起来比直线粗

转载 作者:行者123 更新时间:2023-11-29 16:35:40 24 4
gpt4 key购买 nike

我创建了一个扩展 Shape 的类,以便为我的按钮背景绘制自定义形状(带切角的矩形)。我遇到的问题是画成对角线的线看起来比直线粗得多:

enter image description here

如何使边框始终保持一致的宽度?我尝试将抗锯齿设置为 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)));

最佳答案

简短的回答是将您的线条向内“移动”(远离 View 的边缘),移动量等于笔划宽度的一半。

您的路径精确追踪您视野的边缘。实际绘制笔划时,它以这条路径为中心。这意味着一半的笔画在 View 的边界之外,因此被剪掉了。

但是,当您绘制路径的对角线部分时,您完全在 View 的边界内绘制,因此不会发生裁剪。这使得对角笔划看起来是其余笔划的两倍粗。

这是我编写的代码的等价物:

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();

关于android - 使用 path.lineTo() 时对角线看起来比直线粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52557822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com