尝试在矩形内绘制文本,但在android上绘制矩形

时间:2019-09-30 08:25:01

标签: java android kotlin text drawing

new picture 现在,我正在尝试在绘制的矩形内绘制文本(数字)。我想制作2048,以了解有关android和kotlin用例的更多信息。但是,当我要创建图块时,我遇到了一个大问题。通常情况下,数字应该位于矩形内部,但就我而言,它们是原始矩形上方的一个矩形。因此,在图块的最后一行中,我没有数字,而在顶部,则是实际游戏场上方的数字。如果您真的不明白我的问题,只需发表评论,然后我会尽力使其更好。

重要提示:仅更改文本的y线无济于事,然后它会被下一个矩形覆盖。

这是我用来绘制单个图块的课程:

fun drawTile() {
        var number = tile.number
        var cordX = tile.x
        var cordY = tile.y

        shapeDrawable = ShapeDrawable(RectShape())
        shapeDrawable.setBounds(cordX, cordY, tileWidth, tileHeight)

        shapeDrawable.getPaint().color = tile.tileColor
        shapeDrawable.draw(canvas)

        if(number != 0) {

            var paintText = Paint(Paint.ANTI_ALIAS_FLAG)
            paintText.color = Color.BLACK
            paintText.textSize = 50f

            val numberString = "$number"

            paintText.setShadowLayer(10f, 10f, 10f, Color.BLACK)

            canvas.drawText(numberString, cordX.toFloat()+tileWidth/2, cordY.toFloat()+10, paintText) 

        }

        shapeDrawable.draw(canvas)

Here is an pic how it looks ingame

1 个答案:

答案 0 :(得分:0)

实际上,只需更改y坐标即可解决问题,您只需要绘制形状

fun drawTile() {
        var number = tile.number
        var cordX = tile.x
        var cordY = tile.y

        shapeDrawable = ShapeDrawable(RectShape())
        shapeDrawable.setBounds(cordX, cordY, tileWidth, tileHeight)

        shapeDrawable.getPaint().color = tile.tileColor

        shapeDrawable.draw(canvas)
        if(number != 0) {

            var paintText = Paint(Paint.ANTI_ALIAS_FLAG)
            paintText.color = Color.BLACK
            paintText.textSize = 50f

            val numberString = "$number"

            paintText.setShadowLayer(10f, 10f, 10f, Color.BLACK)

            canvas.drawText(numberString, cordX.toFloat() + tileWidth/2, cordY.toFloat() + tileHeight / 2, paintText) 

        }