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

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

问题描述

有关我的Andr​​oid的游戏,我有一些调用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?

在此先感谢。

推荐答案

最简单的方法是用独立的规模像素为单位定义字体大小在你的资源( SP ) - 这个单位是一样的密度无关的像素( DP ),因为它考虑到了屏密度,但它也考虑到了用户的字体大小设置。

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 /值文件夹 dimens.xml 文件,然后输入以下code增加一个新的层面名为 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>

您可以然后使用获得的文本大小在应用程序

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.

有关详细信息,请参见 href="http://developer.android.com/guide/topics/resources/more-resources.html#Dimension"> Android开发者页面。

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

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

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