gpt4 book ai didi

android - 如何在Android中绘制贝塞尔曲线

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:09 24 4
gpt4 key购买 nike

我需要在我的项目中创建贝塞尔曲线。为此,我正在用油漆绘制 View ,但问题是我没有得到下图中提到的我需要的确切形状。因此,请帮助我解决您的解决方案以及对我的代码进行更改或修改。提前致谢。

我用来创建贝塞尔曲线的代码:

public class DrawView extends View {

public DrawView (Context context) {
super (context);
}

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

Paint pLine = new Paint () {{
setStyle (Paint.Style.STROKE);
setAntiAlias (true);
setStrokeWidth (1.5f);
setColor (Color.RED); // Line color
}};

Paint pLineBorder = new Paint () {{
setStyle (Paint.Style.STROKE);
setAntiAlias (true);
setStrokeWidth (3.0f);
setStrokeCap (Cap.ROUND);
setColor (Color.RED); // Darker version of the color
}};
Path p = new Path ();
Point mid = new Point ();
// ...
Point start =new Point (30,90);
Point end =new Point (canvas.getWidth ()-30,140);
mid.set ((start.x + end.x) / 2, (start.y + end.y) / 2);

// Draw line connecting the two points:
p.reset ();
p.moveTo (start.x, start.y);
p.quadTo ((start.x + mid.x) / 2, start.y, mid.x, mid.y);
p.quadTo ((mid.x + end.x) / 2, end.y, end.x, end.y);

canvas.drawPath (p, pLineBorder);
canvas.drawPath (p, pLine);
}
}

主 Activity

public class MainActivity extends Activity {

private DrawView drawView;

@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
drawView = new DrawView (this);
setContentView (drawView);

}
}

我的实际需要:

enter image description here

我得到的输出:

enter image description here

最佳答案

经过长时间的斗争,我从头开始找到了问题的根本原因。感谢 tool这帮助我生成了坐标以及 blog 这向我展示了我需要的确切样本。最后我的代码如下:

public class DrawView extends View {

Paint paint;
Path path;

public DrawView(Context context) {
super(context);
init();
}

public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init(){
paint = new Paint();

paint.setStyle(Paint.Style.STROKE);

}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
path = new Path();
paint.setColor(Color.RED);
paint.setStrokeWidth(3);
path.moveTo(34, 259);
path.cubicTo(68, 151, 286, 350, 336, 252);
canvas.drawPath(path, paint);

}

关于android - 如何在Android中绘制贝塞尔曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30073682/

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