gpt4 book ai didi

Android canvas.drawText() 不显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:24:58 31 4
gpt4 key购买 nike

所以,我遇到了这个问题,我在背景图像顶部的 Canvas 上绘制的 android 文本没有显示。我的代码:

@Override
public void draw(Canvas canvas) {
final float scaleFactorX = getWidth() / WIDTH;
final float scaleFactorY = getHeight() / HEIGHT;

if(canvas != null) {
final int savedState = canvas.save();
canvas.scale(scaleFactorX, scaleFactorY);

Paint textPaint = new Paint();
textPaint.setColor(Color.RED);
textPaint.setTextSize(20);
textPaint.setAntiAlias(true);

canvas.drawBitmap(background, 0, 0, null);
canvas.drawBitmap(button_start, (canvas.getWidth() - button_start.getScaledWidth(canvas)) / 2, canvas.getHeight() / 4, null);
canvas.drawText("Test text", 0, 0, textPaint);
canvas.restoreToCount(savedState);
}
}

有人知道我做错了什么吗?

最佳答案

基线y轴不为0,试试这个

    Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
textPaint.setColor(Color.RED);
textPaint.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()));
textPaint.setTextAlign(Align.LEFT);
FontMetrics metric = textPaint.getFontMetrics();
int textHeight = (int) Math.ceil(metric.descent - metric.ascent);
int y = (int)(textHeight - metric.descent);
canvas.drawText("text", 0, y, textPaint);

关于Android canvas.drawText() 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31367578/

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