繁体   English   中英

Android canvas.drawText()没有显示

[英]Android canvas.drawText() not showing

所以,我有这个问题,我的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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM