Android计算一行可以显示多少字
val captionStr ="测试文字"
val paint = Paint()
paint.textSize = fontSize
val bounds = Rect()
paint.getTextBounds(captionStr , 0 , 1 , bounds)
val wordWidth = bounds.width()
Log.d("caowj", "单个文字的宽度:${wordWidth}")
val maxCount = screenWidth/ wordWidth.toFloat()
// val count = ceil(maxCount).toInt()//向上取整
val count = floor(maxCount).toInt()//向下取整
Log.d("caowj", "一行可以显示:${count }个文字")
该代码示例展示了在Android中如何使用Paint对象和getTextBounds方法计算单个字符的宽度,进而确定屏幕宽度下一行能显示多少个文字。通过将屏幕宽度除以单个文字的宽度,得到一行可显示的文字数量,并进行了向下取整处理。

4758

被折叠的 条评论
为什么被折叠?



