android 位图/Canvas 的分形生成器
Fractal generator for android Bitmap / Canvas
我想为 Android 创建一个分形生成器。
我的列表中唯一不清楚的是:我应该使用哪个 class 才能在屏幕上显示结果?我需要像位图这样的东西,以便能够获得当前的宽度/高度并能够设置一些像素(RGB),而不需要每个像素都是不同的对象。
我看到我可以使用 Bitmap class,但我不确定这是否正确。
另外,我还希望能够将生成的屏幕设置为背景并将结果保存到文件中。
我应该使用哪个 class?
您可以使用 canvas.drawPoint()
:
To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).
我将其用于分形树:
private fun drawTree(canvas: Canvas, x1: Float, y1: Float, angle: Double, depth: Int) {
if (depth == 0) return
val x2 = x1 + (cos(Math.toRadians(angle)) * depth * 10.0).toInt()
val y2 = y1 + (sin(Math.toRadians(angle)) * depth * 10.0).toInt()
canvas.drawLine(x1, y1, x2, y2, panoramictextpaint)
drawTree(canvas, x2, y2, angle - 20, depth - 1)
drawTree(canvas, x2, y2, angle + 20, depth - 1)
}
我想为 Android 创建一个分形生成器。
我的列表中唯一不清楚的是:我应该使用哪个 class 才能在屏幕上显示结果?我需要像位图这样的东西,以便能够获得当前的宽度/高度并能够设置一些像素(RGB),而不需要每个像素都是不同的对象。
我看到我可以使用 Bitmap class,但我不确定这是否正确。
另外,我还希望能够将生成的屏幕设置为背景并将结果保存到文件中。
我应该使用哪个 class?
您可以使用 canvas.drawPoint()
:
To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).
我将其用于分形树:
private fun drawTree(canvas: Canvas, x1: Float, y1: Float, angle: Double, depth: Int) {
if (depth == 0) return
val x2 = x1 + (cos(Math.toRadians(angle)) * depth * 10.0).toInt()
val y2 = y1 + (sin(Math.toRadians(angle)) * depth * 10.0).toInt()
canvas.drawLine(x1, y1, x2, y2, panoramictextpaint)
drawTree(canvas, x2, y2, angle - 20, depth - 1)
drawTree(canvas, x2, y2, angle + 20, depth - 1)
}