如何在onDraw中从A点到B点绘制"path from circles"
How to draw "path from circles" from A point to B point in onDraw
我需要在 canvas 的 onDraw 路径上绘制从 A 到 B 的许多圆圈(气泡)。像那样:
如何操作?
我已经有了通过点击图像绘制的图钉(点从数组中获取)
@SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
invertedPinsCoordinates.forEach {
val marker = getPinForCoordinates(it)
val matrixMarker = Matrix()
matrixMarker.setTranslate(it.x, it.y)
matrixMarker.postConcat(mMatrix)
canvas.drawBitmap(marker, matrixMarker, null)
}
}
但是不明白如何从圆圈中画出路径。请帮忙!
我创建了解决方案。这是获得点数的算法:
private fun getPointsToDraw(a: CoordinatesEntity, b: CoordinatesEntity): List<CoordinatesEntity> {
val points = ArrayList<CoordinatesEntity>()
val numberOfPoints = 5
val stepX = (b.x - a.x) / numberOfPoints
val stepY = (b.y - a.y) / numberOfPoints
points.add(a)
for (i in 0 until numberOfPoints) {
val lastPoint = points.last()
points.add(CoordinatesEntity(lastPoint.x + stepX, lastPoint.y + stepY))
}
points.add(b)
return points
}
数据class:
data class CoordinatesEntity(var x: Float, var y: Float)
当然我们只能得到之间的固定点,因为有不确定的计数。
下面是如何写它们。
我需要在 canvas 的 onDraw 路径上绘制从 A 到 B 的许多圆圈(气泡)。像那样:
如何操作?
我已经有了通过点击图像绘制的图钉(点从数组中获取)
@SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
invertedPinsCoordinates.forEach {
val marker = getPinForCoordinates(it)
val matrixMarker = Matrix()
matrixMarker.setTranslate(it.x, it.y)
matrixMarker.postConcat(mMatrix)
canvas.drawBitmap(marker, matrixMarker, null)
}
}
但是不明白如何从圆圈中画出路径。请帮忙!
我创建了解决方案。这是获得点数的算法:
private fun getPointsToDraw(a: CoordinatesEntity, b: CoordinatesEntity): List<CoordinatesEntity> {
val points = ArrayList<CoordinatesEntity>()
val numberOfPoints = 5
val stepX = (b.x - a.x) / numberOfPoints
val stepY = (b.y - a.y) / numberOfPoints
points.add(a)
for (i in 0 until numberOfPoints) {
val lastPoint = points.last()
points.add(CoordinatesEntity(lastPoint.x + stepX, lastPoint.y + stepY))
}
points.add(b)
return points
}
数据class:
data class CoordinatesEntity(var x: Float, var y: Float)
当然我们只能得到之间的固定点,因为有不确定的计数。 下面是如何写它们。