画布的StaticLayout定位中心不能像canvas.drawText吗?

问题描述 投票:0回答:1

[使用常规drawText绘制时,文本将定位在画布的中心。但是我的要求是放置多行文本,因此我必须使用StaticLayout,但是StaticLayout不会像drawText那样放置这是到目前为止我尝试过的。

class TestView : View {

private lateinit var staticLayout: StaticLayout
private lateinit var ststicTextPaint: TextPaint
private lateinit var textPaint: TextPaint
private val helloworld = "Hello world!"

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {

}

constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
    context,
    attrs,
    defStyleAttr
) {
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(
    context,
    attrs,
    defStyleAttr,
    defStyleRes
) {
}

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
    super.onSizeChanged(w, h, oldw, oldh)
    setUp(w, h)
}

private fun setUp(w: Int, h: Int) {
    textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
    textPaint.color = Color.BLUE
    textPaint.textSize = 40f
    textPaint.textAlign = Paint.Align.CENTER

    ststicTextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
    ststicTextPaint.color = Color.GREEN
    ststicTextPaint.textSize = 40f
  //  textPaint.textAlign = Paint.Align.CENTER

    staticLayout = StaticLayout(
        helloworld,
        ststicTextPaint,
        w,
        Layout.Alignment.ALIGN_CENTER,
        0f,
        0f,
        false
    )
}

override fun onDraw(canvas: Canvas?) {
    super.onDraw(canvas)
    //Just drawn a rect with cross-hair to know the relative position
    val rect = Rect(0, 0, width, height)
    val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    paint.style = Paint.Style.STROKE
    paint.color = Color.RED
    paint.strokeWidth = 5f
    canvas?.drawRect(rect, paint)
    canvas?.drawLine((width/2).toFloat(), 0F, (width/2).toFloat(), height.toFloat(),paint)
    canvas?.drawLine(0F, (height/2).toFloat(),width.toFloat(), (height/2).toFloat(),paint)

    canvas?.drawText(helloworld, (width / 2).toFloat(), (height / 2).toFloat(), textPaint)
    //   canvas?.drawText(helloworld, (width / 2).toFloat(), (height / 2).toFloat(), textPaint)
    staticLayout.draw(canvas, (width / 2).toFloat(), (height / 2).toFloat())
}

fun StaticLayout.draw(canvas: Canvas?, x: Float, y: Float) {
    canvas?.withTranslation(x, y) {
        draw(canvas)
    }
  }
}

这是我俩都得到的enter image description here蓝色的使用普通drawText完成,绿色的使用StaticLayout

android android-canvas android-custom-view drawtext staticlayout
1个回答
0
投票

我找出问题的人。这是因为我在将StaticLayout文本的宽度居中时忽略了它的宽度。

在这里,我为StaticLayout提供View的整个宽度。

    staticLayout = StaticLayout(
        "Hello world is helloworld",
        ststicTextPaint,
        w,
        Layout.Alignment.ALIGN_CENTER,
        1f,
        0f,
        false
    )

绘制视图时

staticLayout.draw(canvas, ((width / 2)-staticLayout.width/2).toFloat(), (height / 2).toFloat())

我减去了`StaticLayout宽度的一半宽度,以便将其放置在确切的中心。enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.