【问题标题】:How to draw "path from circles" from A point to B point in onDraw如何在 onDraw 中从 A 点到 B 点绘制“圆的路径”
【发布时间】:2021-04-29 08:47:41
【问题描述】:

我需要在画布上绘制从 A 到 B 的 onDraw 路径,并带有许多圆圈(气泡)。像这样:

如何做到这一点?

我已经有了通过点击图像绘制的图钉(点从数组中获取)

@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)
        }
    

}

但不明白如何从圆圈中绘制路径。请帮忙!

【问题讨论】:

    标签: android drawing


    【解决方案1】:

    我创建了解决方案。这是获得点之间的算法:

    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
            }
    

    数据类:

    data class CoordinatesEntity(var x: Float, var y: Float)
    

    当然,我们只能得到之间的固定点,因为有不确定的计数。 以下是它们的编写方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多