getDimension()/getDimensionPixelSize() - mutliplier issue

后端 未结 5 1084
悲哀的现实
悲哀的现实 2021-02-07 06:07

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

...
    20sp         


        
5条回答
  •  深忆病人
    2021-02-07 06:37

    Just to clarify (information obtained by inspecting Android source code):

    Resources.getDimension() and getDimensionPixelOffset()/getDimensionPixelSize() differ only in that the former returns float while the latter two return the same value rounded to int appropriately. For all of them, the return value is in raw pixels.

    All three functions are implemented by calling Resources.getValue() and converting the thus obtained TypedValue by calling TypedValue.complexToDimension(), TypedValue.complexToDimensionPixelOffset() and TypedValue.complexToDimensionPixelSize(), respectively.

    Therefore, if you want to obtain the "raw" value together with the unit specified in the XML source, call Resources.getValue() and use the methods of the TypedValue class.

提交回复
热议问题