Android:Canvas.drawText()在不同屏幕分辨率下的文本大小

发布于 2024-10-24 13:21:20 字数 278 浏览 1 评论 0原文

对于我的 Android 游戏,我对 Canvas.drawText() 进行了一些调用。

为了进行测试,我使用标准字体大小,看起来效果很好。

然而,当我将分辨率提高到更高的密度时,会自动加载较大的图像,但文本现在非常小。

有没有一种简单的方法来计算文本应该绘制的大小,或者我必须手动执行此操作?

编辑:编辑我的帖子 @Suragch 的意义是什么?

For my Android game I have some calls to Canvas.drawText().

For testing, I use the standard font size which seems to be working fine.

However, when I bump up the resolution to a higher density, the larger images are automatically loaded but the text is now incredibly small.

Is there an easy way to calculate what size the text should be drawn at or am I bound to do this manually?

edit: What was the point of editing my post @Suragch ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巷雨优美回忆 2024-10-31 13:21:20

最简单的方法是使用与比例无关的像素 (sp) 单位定义资源中的字体大小 - 该单位类似于与密度无关的像素(dp 或 < code>dip),因为它考虑了屏幕密度,但也考虑了用户的字体大小设置。

要添加新尺寸,请在 res/values 文件夹中创建一个 dimens.xml 文件,然后输入以下代码以添加名为 myFontSize:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="myFontSize">20sp</dimen>
</resources>

然后,您可以使用应用程序中的文本大小。

int scaledSize = getResources().getDimensionPixelSize(R.dimen.myFontSize);

生成的大小将正确缩放,以考虑当前的屏幕密度和字体大小设置。

有关详细信息,请参阅有关更多资源的 Android 开发者页面

The easiest way is to define your font sizes in your resources with the units of scale-independent pixels (sp) -- this unit is like density independent pixels (dp or dip) in that it takes into account the screen density but it also takes into account the font-size setting of the user.

To add a new dimension create a dimens.xml file in your res/values folder and enter the following code to add a new dimension with the name myFontSize:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="myFontSize">20sp</dimen>
</resources>

You can then get the text size in your application using

int scaledSize = getResources().getDimensionPixelSize(R.dimen.myFontSize);

The resulting size will be correctly scaled to take into account the current screen density and font-size setting.

For more information see the Android Developers page on More Resources.

就像说晚安 2024-10-31 13:21:20

您可以使用简单的数学运算自己完成:

您需要计算一个关系,以针对任何画布尺寸绘制相同尺寸的文本,因此使用实际的画布尺寸,如下所示:

double relation = Math.sqrt(canvas.getWidth() * canvas.getHeight());

但是这个数字太大了,所以除以它适合您需要的一个,比方说 250:

relation = relation / 250;

现在您可以这样设置文本大小:

paint.setTextSize((float) (myFontSize * relation));

您不必将关系除以任何数字,但在这种情况下,您将不得不为变量使用非常小的字体大小myFontSize。对我来说,250 可以很好地使用可适应任何屏幕尺寸的常规字体大小,因为您已经从画布中计算了像素尺寸。

You can do it yourself using a simple math operation:

You need to calculate a relation that draws your text just the same size for any canvas size, so use the actual canvas size, like this:

double relation = Math.sqrt(canvas.getWidth() * canvas.getHeight());

But that number is just too big, so divide it by one that suits your needs, lets say 250:

relation = relation / 250;

Now you can set your text size like this:

paint.setTextSize((float) (myFontSize * relation));

You don't have to necessary divide relation by any number but in that case you will have to use very small font sizes for the variable myFontSize. For me 250 works just fine to use regular font sizes that will adjust to any screen size, since you are already taking in count the pixels dimension from your canvas.

蓝天白云 2024-10-31 13:21:20

最简单的方法是在资源目录中使用与比例无关的像素 (sp)与密度无关的像素 (dp) 为单位定义字体大小。然后使用获取文本大小。

int scaledSize = getResources().getDimensionPixelSize(R.dimen.font_size);
mTextView.setTextSize(scaledSize);

这将根据当前屏幕密度/分辨率和用户的字体大小设置适当缩放文本大小。

如何定义资源目录中的字体大小?

res->values文件夹中创建一个名为dimens.xml的文件。然后复制以下代码。

<resources>
    <dimen name="font_size">25sp</dimen>   // sp or dp accordingly
</resources>

标签 name 属性将用作资源 ID。

The easiest way is to define your font size in your resources directory with the units of scale-independent pixel (sp) or the density-independent pixel (dp). Then get the text size using

int scaledSize = getResources().getDimensionPixelSize(R.dimen.font_size);
mTextView.setTextSize(scaledSize);

This will scale the text-size appropriately according to the current screen density/resolution and the user's font-size setting.

How to define font size in resource directory?

Create a file named dimens.xml in the folder res->values. Then copy the following code.

<resources>
    <dimen name="font_size">25sp</dimen>   // sp or dp accordingly
</resources>

The name attribute of the tag <dimen> will be used as resource ID.

虐人心 2024-10-31 13:21:20

调用 Canvas.drawText()文字大小首先由传入的Paint对象决定,可以通过Paint.setTextSize()。文本大小由 Canvas 根据画布密度自动缩放,可以使用 Canvas.getDensity()

在设置要在 Canvas 上绘制的绘画对象上的文本大小时,请使用 dpsp 单位值,并让 Canvas 为您处理缩放。

When calling Canvas.drawText() the text size is first determined by the passed in Paint object, which can be set via Paint.setTextSize(). The text size is automatically scaled by Canvas based on the canvas density, which can be found using Canvas.getDensity().

When setting the text size on a paint object that will be drawn on Canvas, work with a unit value of dp or sp and let Canvas handle the scaling for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文