Android canvas 画不同颜色的圆
Android canvas draw circle with different colors
我只用canvas画了这个圆和线。
但是我想把每一块都做成不同的颜色。有没有办法用 3-4-5-6..(每块都相等)不同的颜色绘制 300,300 个圆。
例如第一块是红色,第二块是蓝色,第三块是橙色。
编辑:我回答了下面的问题。
我在方形图像上做过类似的事情,将图像切成小块并在每块上使用 PorterDuffXferMode
滤镜很容易。这可能对你不起作用,原因是你的饼形切片不均匀而不是 square/Rect。但是我是怎么做到的呢?见:
创建一个 Canvas 对象将您的图像作为位图传递:
Canvas canvas = new Canvas(bitmap1);
创建一个Paint对象并设置XferMode为SRC_ATOP
:
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
确定图像切片的大小并用它初始化一个 RectF
对象,这是饼形大小的问题,如果你能找到解决这个问题的方法,你就可以做到.
现在,在所有切片之间循环并在其上放置滤色器。
paint.setColor("Your color here");
//Move the rect to next piece using offset
//i and j are loop variables as a 2D Matrix
//dx is width of a slice and dy is the height of a slice
rect.offsetTo(i * dx, j * dy);
//Draw the colored Rect on the image
canvas.drawRect(rect, paint);
最后根据需要使用位图
PS:这不是什么新鲜事,但我是在大约 4 年前投入大量时间并阅读了许多 android 文档后自己发现的。看到这个答案好像很简单,但是找到整个过程却不是。
这是 5 件。您可以更改半径、度数和其他参数以使其动态变化。
private fun drawCircle300(): Bitmap? {
var radius = 150f
val bitmap = Bitmap.createBitmap(
(radius * 2).toInt(),
(radius * 2).toInt(),
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
val paint = Paint().apply {
strokeWidth = 3f
style = Paint.Style.FILL
isAntiAlias = true
}
val rect = RectF(0f, 0f, 300f, 300f)
var degree = 72f
var currentAngle = 0f
for (i in 0 until 5) {
if (i == 0)
paint.color = Color.BLACK
if (i == 1)
paint.color = Color.BLUE
if (i == 2)
paint.color = Color.RED
if (i == 3)
paint.color = Color.YELLOW
if (i == 4)
paint.color = Color.GREEN
canvas.drawArc(rect, currentAngle, degree, true, paint)
currentAngle += degree
}
return bitmap
}
我只用canvas画了这个圆和线。
但是我想把每一块都做成不同的颜色。有没有办法用 3-4-5-6..(每块都相等)不同的颜色绘制 300,300 个圆。 例如第一块是红色,第二块是蓝色,第三块是橙色。
编辑:我回答了下面的问题。
我在方形图像上做过类似的事情,将图像切成小块并在每块上使用 PorterDuffXferMode
滤镜很容易。这可能对你不起作用,原因是你的饼形切片不均匀而不是 square/Rect。但是我是怎么做到的呢?见:
创建一个 Canvas 对象将您的图像作为位图传递:
Canvas canvas = new Canvas(bitmap1);
创建一个Paint对象并设置XferMode为
SRC_ATOP
:Paint paint = new Paint(); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
确定图像切片的大小并用它初始化一个
RectF
对象,这是饼形大小的问题,如果你能找到解决这个问题的方法,你就可以做到.现在,在所有切片之间循环并在其上放置滤色器。
paint.setColor("Your color here"); //Move the rect to next piece using offset //i and j are loop variables as a 2D Matrix //dx is width of a slice and dy is the height of a slice rect.offsetTo(i * dx, j * dy); //Draw the colored Rect on the image canvas.drawRect(rect, paint);
最后根据需要使用位图
PS:这不是什么新鲜事,但我是在大约 4 年前投入大量时间并阅读了许多 android 文档后自己发现的。看到这个答案好像很简单,但是找到整个过程却不是。
这是 5 件。您可以更改半径、度数和其他参数以使其动态变化。
private fun drawCircle300(): Bitmap? {
var radius = 150f
val bitmap = Bitmap.createBitmap(
(radius * 2).toInt(),
(radius * 2).toInt(),
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
val paint = Paint().apply {
strokeWidth = 3f
style = Paint.Style.FILL
isAntiAlias = true
}
val rect = RectF(0f, 0f, 300f, 300f)
var degree = 72f
var currentAngle = 0f
for (i in 0 until 5) {
if (i == 0)
paint.color = Color.BLACK
if (i == 1)
paint.color = Color.BLUE
if (i == 2)
paint.color = Color.RED
if (i == 3)
paint.color = Color.YELLOW
if (i == 4)
paint.color = Color.GREEN
canvas.drawArc(rect, currentAngle, degree, true, paint)
currentAngle += degree
}
return bitmap
}