Android:不同屏幕分辨率下的 Canvas.drawText() 文本大小 [英] Android: Canvas.drawText() text size on different screen resolutions

查看:18
本文介绍了Android:不同屏幕分辨率下的 Canvas.drawText() 文本大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的 Android 游戏,我有一些对 Canvas.drawText() 的调用.

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?

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

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

推荐答案

最简单的方法是使用与比例无关的像素单位 (sp) 来定义资源中的字体大小——这个单位就像密度无关像素(dpdip),因为它考虑了屏幕密度,但也考虑了用户的字体大小设置.

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.

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

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>

然后您可以使用

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.

有关更多信息,请参阅 Android 开发人员页面上的更多资源.

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

这篇关于Android:不同屏幕分辨率下的 Canvas.drawText() 文本大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆