gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:04:17 25 4
gpt4 key购买 nike

new picture现在我正在尝试在我绘制的矩形内绘制文本(数字)。我想做 2048 来了解更多关于 android 和 kotlin 的用例。但是当我想创建瓷砖时我遇到了一个大问题。通常,数字应该位于矩形内部,但在我的例子中,它们比原始矩形高一个矩形。所以在最后一行图 block 上我没有数字,而在顶部有实际游戏区域上方的数字。如果您不太明白我的问题,请写评论,我会尽力改进。

重要提示:仅更改文本的 y 线并没有帮助,它只会被下一个矩形 overdraw 。

这是我用来绘制单个图 block 的类:

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

最佳答案

实际上只要改变 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)

}

关于java - 尝试在矩形内绘制文本,但在 android 上在矩形上方绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58164062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com