java - 在 Canvas 上绘制多种尺寸的多条线

标签 java android canvas

我已经知道如何在 Canvas 上绘制多条线

但目前我只能为它们设置一种文本大小一次

现在,我想在 Canvas 上绘制多条具有多种尺寸的线,详细信息:

  • 文本 1:尺寸 16

  • 文本 2:尺寸 32

  • 文本 3:尺寸 14

实际上我不知道该怎么做,

请知道的人帮忙解答一下,

谢谢,

p/s:我在 Canvas 上绘制多条线的示例代码:

enter image description here

关于文字大小和位置的多行显示错误,因为我计算错误,我还在检查。

private TextPaint mTp;

private String[] strings = {
        " Topics : Asukabu Tawain \n ",
        "512 \n ",
        "Comments \n",
        "7:30 22/12/2017"
};

private float height = 0;

public CustomImageView(Context context) {
    super(context);

    // Initialize a new Bitmap object
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inMutable = true;
    Bitmap mutableBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.red_circle, opt);

    mTp = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    mTp.setColor(Color.WHITE);
    // text shadow
    mTp.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    textWidth = mutableBitmap.getWidth();

    setImageBitmap(mutableBitmap);
}

public CustomImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    x = (canvas.getWidth() - textWidth) / 2;

    for (int i = 0; i < strings.length; i++) {
        if (canvas.getWidth() < 150)
            mTp.setTextSize(0);
        else
            mTp.setTextSize(determineMaxTextSize(strings[i], height/2));

        StaticLayout mSl = new StaticLayout(
                strings[i], mTp, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);

        // get height of multiline text
        int textHeight = mSl.getHeight();

        // get position of text's top left corner
        y = (canvas.getHeight() - textHeight) / 2;

        // draw text to the Canvas center
        canvas.save();

        canvas.translate(x, y);
        mSl.draw(canvas);

        canvas.restore();
    }
}

public void setHeight(float height) {
    this.height = height;
}

private int determineMaxTextSize(String str, float maxWidth){
    int size = 0;
    Paint paint = new Paint();

    do {
        paint.setTextSize(++ size);
    } while(paint.measureText(str) < maxWidth);

    return size;
}

最佳答案

只需使用3种不同TextPaint即可绘制3种不同大小的文本。

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int width = canvas.getWidth();
    int height = width;
    paint.setColor(Color.RED);
    paint.setAntiAlias(true);
    canvas.drawCircle(width/2,height/2,width/2 - 10,paint);

    textPaint1.setColor(Color.WHITE);
    textPaint1.setTextSize(36);
    canvas.drawText("You received",width/2 - 108,height/2 - 80,textPaint1);

    textPaint2.setTextSize(72);
    textPaint2.setColor(Color.WHITE);
    canvas.drawText("135",width/2 - 72,height/2,textPaint2);

    textPaint3.setTextSize(32);
    textPaint3.setColor(Color.WHITE);
    canvas.drawText("comments",width/2 - 96,height/2 + 40,textPaint3);
}

还有一个建议:不要在onDraw方法中new Object,因为每次UI更新时都会调用它,创建这么多对象很容易触发GC并导致性能不佳。

关于java - 在 Canvas 上绘制多种尺寸的多条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41275203/

相关文章:

java - 在共享托管解决方案中的 JAVA 中托管 Restful Web 服务的位置

android - Koin 范围和接口(interface)

android - 使用 Achartengine 的堆积条形图

javascript - 如何找到 Canvas 上两个向量之间的 Angular ?

java - 是否有用于在 SOAP header 中指定和传播相关 ID 的 WS-* 标准?

java - 由于正在运行的线程,应用程序在关闭 fragment 时崩溃

java - 有谁知道为什么我的 Java 代码使用 n=n/2 时不能正常工作,而 n>>1 可以?

php - GCM 显示错误请求实体太大错误 413

javascript - 在 Javascript 中将进度条步骤转换为像素

Javascript 和 Canvas 3D 模型查看器