为绘画设置TextSize

时间:2017-11-27 10:22:12

标签: android android-canvas

我正在寻找动态设置textPaint文本大小的选项。我想设置textSize值逐渐增加,直到到达屏幕中心并逐渐减小直到屏幕底部。

<H5>1</H5>
<H4>2</H4>
<H3>3</H3>
<H2>4</H2>
<H1>5</H1>
<H2>6</H2>
<H3>7</H3>
<H4>8</H4>
<H5>9</H5>

截至目前,我将文字大小设为默认值。我想在我的onDraw()方法中动态设置这个集合

有没有办法实现这个目标?

更新: 我尝试在onDraw()方法中设置paint,而不是在

下面共享的构造函数
Rect bounds = getBounds();
    float textX, textY;
    float pivotX, pivotY;

    Log.d("TAG", "canvas center point "+ canvas.getWidth() / 2 +", "+ canvas.getHeight() / 2);
    Log.d("TAG", "Bounds Center Point "+ bounds.centerX() + ", "+bounds.centerY());

    StaticLayout mTextLayout = new StaticLayout(text, paint, bounds.width() + 75, Layout.Alignment.ALIGN_CENTER, 1.0f, 1.0f, false);
    canvas.save();

    if (paint.getTextAlign() == Paint.Align.LEFT) {
        textX = bounds.centerX() + 50;
        textY = bounds.centerY() + 8;

    }
    else if (paint.getTextAlign() == Paint.Align.RIGHT) {
        textX = bounds.centerX() - 100;
        textY = bounds.centerY() + 10;
    }
    else
    {
        textX = bounds.centerX() + 8;
        textY = bounds.centerY() + 8;
     }

    canvas.translate( textX, textY - mTextLayout.getHeight() / 2);
    paint.setTextSize((bounds.centerY()) / 10); //Setting text size dynamically
     mTextLayout.draw(canvas);
    canvas.restore();

1 个答案:

答案 0 :(得分:2)

使用setSpan方法

text.setSpan...

使用span你也可以改变文字颜色