gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:57:07 25 4
gpt4 key购买 nike

我已经知道如何在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com