本文介绍了getDimension()/ getDimensionPixelSize() - mutliplier问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的Andr​​oid 2.3.5设备这是正常/华电国际。我有一个dimens.xml在我的项目:

So I have android 2.3.5 device which is NORMAL/HDPI. I have a dimens.xml in my project:

...
    <dimen name="gameresult_congrats_label_msg_textSize">20sp</dimen>
...

该文件是在值正常/值,华电国际等文件夹的绝对相同。在我的第一个活动的应用程序使用说明我值:

this file is absolutely identical in values-normal/values-hdpi and so on folders.In my first activity app shows me that value using:

Toast.makeText(this, "textSize is "+getResources().getDimensionPixelSize(R.dimen.gameresult_congrats_label_msg_textSize), Toast.LENGTH_SHORT).show();

和它显示30.我也尝试过:

and it displays 30. I Tried also:

Toast.makeText(this, "textSize is "+getResources().getDimension(R.dimen.gameresult_congrats_label_msg_textSize), Toast.LENGTH_SHORT).show();

但结果是一样的。但只有当我尝试这样的:

but result is the same. But only when I tried this:

Toast.makeText(this, "textSize is "+getResources().getString(R.dimen.gameresult_congrats_label_msg_textSize), Toast.LENGTH_SHORT).show();

我得到了我的20SP终于来了!但是,这是为什么? 官方文档说,这些方法返回

资源维度值乘以相应的指标。

我查了一下这个改变我的价值为25,我38,这意味着AOS使用1.5倍增。但为什么?它已经获得来自相应的文件夹的价值,这意味着它得到一个准备使用的价值!从哪里AOS获取1.5倍乘数?我知道,这取决于DisplayMetrics。但是,它是如何计算的1.5倍?
更新
我都懂倍增,但你看,真正的问题在这里是一倍左右比例。而这就是为什么我没有问过这个问题。
所以,如果我有一些layout.xml(在水库\布局文件夹中)与TexView样定义的:

I checked this by changing my value to 25 and I got 38 which means aos uses 1.5 multiplier. But why? It already gets value from appropriate folder which means it gets a ready to use value! From where aos gets that 1.5x multiplier? I know it depends on DisplayMetrics. But how it calculates 1.5x?
UPDATE
I understand about multiplier but, you see, the real problem here is about double scaling. And thats why I did asked this question.
So if I have some layout.xml (in res\layout folder) with TexView defined like:

<TextView
    android:id="@+id/congratsLabel"
    ...
    android:textSize="@dimen/gameresult_congrats_label_msg_textSize" />

一切看起来OK。我的意思是TextView的是像林期待。
现在,让我们在code做相同的:

Everything looks ok. I mean textview is like Im expecting.
Now lets do the same in code:

TextView congratsLabel = fineViewById(R.id.congratsLabel);
textSize = getResources().getDimension(R.dimen.gameresult_congrats_label_msg_textSize)
congratsLabel.setTextSize(textSize)

和这里是问题! getResources()。getDimension()返回一个 SCALED 的价值和多数民众赞成确定。但得到的我的TextView的大小将是1.5比我预期的Cuz setTextSize适用于SP和来这里的第二个规模!而这就是为什么AOS使得到的文本大小调整为45(最初被定义为20SP)。

and here is the issue!!! getResources().getDimension() returns a SCALED value and thats ok. But the resulting size of my textView will be 1.5 greater than I expecting cuz setTextSize works with SP and here comes the second scale! And thats why AOS makes resulting text size scaled to 45 (originally defined as 20sp).

推荐答案

按照支持不同的屏幕密度的训练,华电国际是正常的1.5倍(MDPI)的大小。由于 getDimensionPixelSize 转换为像素时需要这种差异考虑在内,则返回值将是1.5倍于 SP 你的价值。

Per the Supporting Different Screen Densities training, hdpi is 1.5x normal (mdpi) sizes. As getDimensionPixelSize takes this difference into account when converting into pixels, the returned value will be 1.5x your value in sp.

注意 SP 还依赖于用户的preferred文字的大小,因此可以改变甚至比1.5倍的预期值。

Note that sp is also dependent on the user's preferred text size and can therefore change to be even larger than 1.5x your expected value.

这篇关于getDimension()/ getDimensionPixelSize() - mutliplier问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!