如何绘制在画布上大尺寸的文字?画布、上大、尺寸、文字

2023-09-05 23:19:42 作者:你撞我心上啦

我要画在画布上一个月的文字以及屏幕高度垂直。

I want to draw on canvas month's text vertical along screen height.

油漆初始化:

   this.paint = new Paint();
   this.paint.setAntiAlias(true);
   this.paint.setDither(true);
   this.paint.setSubpixelText(true);
   this.paint.setColor(color_text_dark);
   this.paint.setTextAlign(Align.RIGHT);

图:

   // Set the scale to the widest month
   float scale = getHeight() / this.max_month_width;
   String month_string = FULL_MONTH_NAME_FORMATTER.
                         format(active_month_calendar.getTime());
   canvas.save();
   canvas.translate(getWidth(), 0);
   canvas.rotate(-90);
   canvas.scale(scale, scale);
   canvas.drawText(month_string, 0, 0, this.paint);
   canvas.restore();

结果看起来不错的华电国际的屏幕,但很丑陋和像素化上的 xhdpi 的之一。

Result looks good on hdpi screen, but very ugly and pixelated on xhdpi one.

我做了各种设备的详细测试,并理解什么结果取决于Android的版本,而不是屏幕像素密度和分辨率。

I did more test on various devices, and understood what result depends on Android version, not screen density and resolution.

code正常工作的2.x的平台,但不会对4.0.3+工作。假设,Android的平局在这里实现改变。 全部code,你可以看到这里。

Code works fine on 2.x platform, but doesn't work on 4.0.3+. Suppose, Android draw implementation was changed here. Full code you can see here.

华电国际 版本 2.3.5 (还测试的 2.2 )

xhdpi 版本 4.2 (还测试的 4.1 4.0.3 )

尝试不同的变化油漆反锯齿,子像素文本没有任何影响。我该如何解决这个问题?

Trying different variations for paint antialias, subpixel text has no effect. How can I fix this issue?

推荐答案

现在的问题是,你正在绘制文本在一个大小和缩放的结果了。一旦你确定你想要多宽的文​​字是,你应该使用调用Paint.measureText(),通过Paint.setTextSize调整大小()相应。一旦正确的措施,那么你做你的呼叫Canvas.drawText()。

The problem is that you're drawing text at one size and scaling the result up. Once you've determined how wide you want the text to be, you should use calls to Paint.measureText(), adjusting the size via Paint.setTextSize() accordingly. Once it measures correctly, then you do your call to Canvas.drawText().

另一种方法是不衡量在所有的文字,只是立即拨打:

An alternative would be to not measure the text at all and just immediately call:

paint.setTextSize(paint.getSize() * scale)

有没有保证文本将适合在这种情况下,虽然。

There's no guarantee the text will fit in this case, though.

所有其它的变换调用应该导致插值,所以它应该给你非常锋利的线条。

None of your other transform calls should result in interpolation, so it should give you very sharp lines.

修改的

下面是一个code样品和对比截图:

Here is a code sample and comparison screenshot:

canvas.save();
canvas.scale(10, 10);
canvas.drawText("Hello", 0, 10, mTextPaint);
canvas.restore();
float textSize = mTextPaint.getTextSize();
mTextPaint.setTextSize(textSize * 10);
canvas.drawText("Hello", 0, 300, mTextPaint);
mTextPaint.setTextSize(textSize);