使用 android 中的 canvas 旋转创建具有 4 段的圆
Creating a circle with 4 segments using canvas in android with rotation
我需要像这样用四种随机颜色创建一个分段圆:
http://vectips.com/wp-content/uploads/2015/04/39.png。 “4 段围成一圈”
我参考 android 关于使用 canvas.drawArc() 的文档编写了以下代码,但它没有按照我的预期工作,因为它只显示一个片段。
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)
}
会画出类似的东西:
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)
}
我需要像这样用四种随机颜色创建一个分段圆:
http://vectips.com/wp-content/uploads/2015/04/39.png。 “4 段围成一圈”
我参考 android 关于使用 canvas.drawArc() 的文档编写了以下代码,但它没有按照我的预期工作,因为它只显示一个片段。
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)
}
会画出类似的东西:
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)
}