gpt4 book ai didi

android - 如何在 Canvas 上绘制三角形、星形、正方形或心形?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:49:05 27 4
gpt4 key购买 nike

我可以使用
在 Canvas 上画一个圆和一个长方形path.addCircle()

path.addRect()

现在我想知道如何画三角形、星形、正方形或心形?

最佳答案

为了以后直接求答案,我用canvas画了一个几乎对称的星星,如图:

Star Image

主要工具是使用 Paths。

假设您已设置:

Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);

Path path = new Path();

然后在你的 onDraw 中,你可以像我下面那样使用路径。它将适本地缩放到任何尺寸的 Canvas

@Override
protected void onDraw(Canvas canvas) {

float mid = getWidth() / 2;
float min = Math.min(getWidth(), getHeight());
float fat = min / 17;
float half = min / 2;
float rad = half - fat;
mid = mid - half;

paint.setStrokeWidth(fat);
paint.setStyle(Paint.Style.STROKE);

canvas.drawCircle(mid + half, half, rad, paint);

path.reset();

paint.setStyle(Paint.Style.FILL);


// top left
path.moveTo(mid + half * 0.5f, half * 0.84f);
// top right
path.lineTo(mid + half * 1.5f, half * 0.84f);
// bottom left
path.lineTo(mid + half * 0.68f, half * 1.45f);
// top tip
path.lineTo(mid + half * 1.0f, half * 0.5f);
// bottom right
path.lineTo(mid + half * 1.32f, half * 1.45f);
// top left
path.lineTo(mid + half * 0.5f, half * 0.84f);

path.close();
canvas.drawPath(path, paint);

super.onDraw(canvas);

}

关于android - 如何在 Canvas 上绘制三角形、星形、正方形或心形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7007429/

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