如何在Kotlin中绘制矩形?

时间:2018-05-13 17:07:33

标签: android kotlin android-canvas

我只是一个新手并试图在Kotlin中使用画布。我试图根据用户输入绘制矩形,我将从另一个活动中获取。我不能做的是将数组传递给重写类。这是我的代码。

using System.Linq;

1 个答案:

答案 0 :(得分:2)

class Canvass (context: Context, layerheight: ArrayList<Double>): View(context) {

    private var layerheight: ArrayList<Double> = layerheight
    override fun onDraw (canvas: Canvas) {

        canvas.drawRGB (255, 255, 255)
        val width = width
        val footingpaint = Paint ()
        val textpaint = Paint ()
        val brush3 = Paint ()
        val brush4 = Paint ()

        footingpaint.setARGB (255, 128, 128, 128)
        textpaint.setARGB (255, 255, 255, 255)
        brush3.setARGB (255, 138, 36, 36)
        brush4.setARGB (255, 138, 36, 36)

        canvas.drawRect (((width/2)-10).toFloat(), 0f, ((width/2)+ 10) .toFloat (), 40f, footingpaint)
        canvas.drawRect (70f, 40f, (width - 70) .toFloat (), 80f, footingpaint)
        for(i in (0..layerheight.size)){
            println(layerheight)
        }

    }
}

找到它但是如果有更好的解决方案,我很乐意检查它。