gpt4 book ai didi

android - 在带有旋转的android中使用 Canvas 创建一个有4个线段的圆

转载 作者:行者123 更新时间:2023-11-30 05:05:05 25 4
gpt4 key购买 nike

我需要像这样用四种随机颜色创建一个分段圆:

http://vectips.com/wp-content/uploads/2015/04/39.png . “4 段围成一圈”

我在引用有关使用 canvas.drawArc() 的 android 文档时编写了以下代码,但它没有按照我的预期工作,因为它只显示一个 fragment 。

           Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

bitMap = bitMap.copy(bitMap.getConfig(), true);
// Construct a canvas with the specified bitmap to draw into
Canvas canvas = new Canvas(bitMap);
// Create a new paint with default settings.
Paint paint = new Paint();
// smooths out the edges of what is being drawn
paint.setAntiAlias(true);
// set color
paint.setColor(Color.BLUE);

paint.setStrokeWidth(4.5f);


RectF oval = new RectF(200F, 200F, 450F, 450F);

paint.setStyle(Paint.Style.FILL);
canvas.drawArc(oval, 0F, 90F, true, paint);
paint.setColor(Color.GREEN);
canvas.drawArc(oval, 90F, 90F, true, paint);
paint.setColor(Color.RED);

canvas.drawArc(oval, 180F, 90F, true, paint);
paint.setColor(Color.BLACK);

canvas.drawArc(oval, 270F, 90F, true, paint);

imageView.setImageBitmap(bitMap);

最佳答案

你的圆弧角度是错误的。参见 drawArc Canvas reference ,角度参数是这样的:

  • startAngle - 圆弧开始的起始角度(以度为单位)

  • sweepAngle - 顺时针测量的扫掠角(以度为单位)

您的最后一条弧线覆盖了之前的所有弧线。你应该做类似的事情(下面的 Kotlin 代码):

private val oval = RectF(200F, 200F, 450F, 450F)

private val paint = Paint()

override fun onDraw(canvas: Canvas) {
paint.style = Paint.Style.FILL

paint.color = Color.BLACK
canvas.drawArc(oval, 0F, 90F, true, paint)
paint.color = Color.BLUE
canvas.drawArc(oval, 90F, 90F, true, paint)
paint.color = Color.RED
canvas.drawArc(oval, 180F, 90F, true, paint)
paint.color = Color.GREEN
canvas.drawArc(oval, 270F, 90F, true, paint)
}

会画出类似的东西:

Sample output

Java 版本:

  private RectF oval = new RectF(0F,0F,100F,100F);

private Paint paint = new Paint();

@Override
protected void onDraw(Canvas canvas) {
paint.setStyle(Paint.Style.FILL);

paint.setColor(Color.BLACK);
canvas.drawArc(oval, 0F, 90F, true, paint);
paint.setColor(Color.BLUE);
canvas.drawArc(oval, 90F, 1800F, true, paint)
paint.setColor(Color.RED);
canvas.drawArc(oval, 0F, 190F, true, paint)
paint.setColor(Color.GREEN);
canvas.drawArc(oval, 270F, 360F, true, paint)
}

关于android - 在带有旋转的android中使用 Canvas 创建一个有4个线段的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54714611/

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