Android calculates how many words can be displayed in one line

Android calculates how many words can be displayed in one line

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 }个文字")

Guess you like

Origin blog.csdn.net/zhijiandedaima/article/details/130964159