浅扒Android动态设置字体大小 您所在的位置:网站首页 广东矮脚虎是谁的绰号 浅扒Android动态设置字体大小

浅扒Android动态设置字体大小

#浅扒Android动态设置字体大小| 来源: 网络整理| 查看: 265

说点废话

Android开发中,TextView类的控件应该说是很常用了。一般来说我们是通过android:textSize="20sp" 来设置字体大小,但是很多时候也需要动态设置字体大小,调用也很简单:

textView.setTextSize(textSize);

为了适配各种各样的型号,我们一般会将字体大小定义到dimens.xml之中:

16sp

然后在java代码中设置定义好的字体大小:

float dimen = getResources().getDimension(R.dimen.text_size); textView.setTextSize(dimen);

满心欢喜的运行一下,看一效果,结果发现字体奇大无比!!!远非16sp!!!难道不应该通过getDimension()取值吗?通过logcat我发现,在Nexus 6p并且16sp下,在通过getDimension(R.dimen.text_size)得到返回值是56.0! 实际上,在java代码中取在dimens.xml中定义的值一共有三种:

getDimension()getDimensionPixelOffset()getDimensionPixelSize()

看到这三个函数的名称时,还是会有点不知所云。本着“不求甚解,遍历式开发”的原则,我把这三种方式都试了一遍,结果发现字体大小没一个是对的,这就诡异了。难道这里有平行宇宙?至此,我只能翻出我的英汉大词典,让我们去探寻一下docs吧。

getDimension() /** * Retrieve a dimensional for a particular resource ID. Unit * conversions are based on the current {@link DisplayMetrics} associated * with the resources. * * @param id The desired resource identifier, as generated by the aapt * tool. This integer encodes the package, type, and resource * entry. The value 0 is an invalid identifier. * * @return Resource dimension value multiplied by the appropriate * metric. */ public float getDimension(@DimenRes int id) throws NotFoundException { }

通过注释我们不难发现,getDimension()是根据指定id获取一个基于当前DisplayMetrics的值。这个值究竟是什么也没有说,只知道是float,并且单位转换是基于当前资源的,但肯定不是像素,如果是像素应该是int。

getDimensionPixelSize /** * Retrieve a dimensional for a particular resource ID for use * as a size in raw pixels. This is the same as * {@link #getDimension}, except the returned value is converted to * integer pixels for use as a size. A size conversion involves * rounding the base value, and ensuring that a non-zero base value * is at least one pixel in size. * * @param id The desired resource identifier, as generated by the aapt * tool. This integer encodes the package, type, and resource * entry. The value 0 is an invalid identifier. * * @return Resource dimension value multiplied by the appropriate * metric and truncated to integer pixels. */ public int getDimensionPixelSize(@DimenRes int id) throws NotFoundException { }

getDimensionPixelSize()的功能与getDimension()类似,不同的是将结果转换为int,并且小数部分四舍五入,这个结果将作为尺寸。getDimensionPixelSize()进行了尺寸转换,这个转换实际是上四舍五入的结果,并且保证返回值是一个至少是1像素的非零数值。

getDimensionPixelOffset() /** * Retrieve a dimensional for a particular resource ID for use * as an offset in raw pixels. This is the same as * {@link #getDimension}, except the returned value is converted to * integer pixels for you. An offset conversion involves simply * truncating the base value to an integer. * * @param id The desired resource identifier, as generated by the aapt * tool. This integer encodes the package, type, and resource * entry. The value 0 is an invalid identifier. * * @return Resource dimension value multiplied by the appropriate * metric and truncated to integer pixels. */ public int getDimensionPixelOffset(@DimenRes int id) throws NotFoundException { }

getDimensionPixelOffset()与getDimension()功能类似,不同的是将结果转换为int,这个结果将用作原始像素的偏移量。偏移转换(offset conversion,函数命名中的offset是这个意思)的作用之一是将基础值简单地截短为整数,注意直接截断小数位,即取整(其实就是把float强制转化为int,注意不是四舍五入)。

阶段性总结

由此可见,这三个函数返回的都是绝对尺寸,而不是相对尺寸(dp\sp等)。如果getDimension()返回结果是30.5f,那么getDimensionPixelSize()返回结果就是31,getDimensionPixelOffset()返回结果就是30。 至此,应该说getDimensionPixelSize() getDimension() getDimensionPixelOffset()我们已经大致有所了解了,但是如果想更深入了解一下,就需要深入源码以验证上述解释。

扒源码

深入源码,我们可以发现其实这三个函数的实现大同小异,以getDimension()为例:

public float getDimension(@DimenRes int id) throws NotFoundException { final TypedValue value = obtainTempTypedValue(); try { final ResourcesImpl impl = mResourcesImpl; impl.getValue(id, value, true); if (value.type == TypedValue.TYPE_DIMENSION) { return TypedValue.complexToDimension(value.data, impl.getDisplayMetrics()); } throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) + " type #0x" + Integer.toHexString(value.type) + " is not valid"); } finally { releaseTempTypedValue(value); } }

类TypedValue是动态类型数据的容器,其主要用于盛放Resources的值。上述代码第7行就是根据id获取TypedValue的值,getDimension()、getDimensionPixelOffset()和getDimensionPixelSize()函数体唯一的不同就是第7行:

getDimension()调用的是TypedValue的complexToDimension()方法 getDimensionPixelSize调用的是TypedValue的complexToDimensionPixelSize()方法 getDimensionPixelOffset调用的是TypedValue的complexToDimensionPixelOffset()方法

顺藤摸瓜,我们继续深入ypedValue,查看complexToDimension()、complexToDimensionPixelSize()和complexToDimensionPixelOffset()函数的区别,会发现这三个函数体内容依旧大同小异,以complexToDimension()为例:

public static float complexToDimension(int data, DisplayMetrics metrics) { return applyDimension( (data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK, complexToFloat(data), metrics); }

complexToDimensionPixelOffset()与complexToDimension()不同的是将结果进行了强转,实际上相当直接截断小数部分; complexToDimensionPixelSize()是将结果进行四舍五入,并取整。这里的四舍五入实际上就是把结果加上0.5f然后进行强转(有兴趣了解原理的可以留言)。

applyDimension()

各位看官,源码已经看到了这里,是否已感觉很无趣?但applyDimension()的实现已经脱光了在等着你呢:

public static float applyDimension(int unit, float value,DisplayMetrics metrics) { switch (unit) { case COMPLEX_UNIT_PX: return value; case COMPLEX_UNIT_DIP: return value * metrics.density; case COMPLEX_UNIT_SP: return value * metrics.scaledDensity; case COMPLEX_UNIT_PT: return value * metrics.xdpi * (1.0f/72); case COMPLEX_UNIT_IN: return value * metrics.xdpi; case COMPLEX_UNIT_MM: return value * metrics.xdpi * (1.0f/25.4f); } return 0; }

在上述代码中,我们发现在applyDimension()中根据单位的不同,将float乘上不同的系数。如dip/dp需乘上屏幕系数,sp则需乘上字号的缩放系数,pt、in、mm等也是根据相应的算法进行换算(从COMPLEX_UNIT_PX直接返回float可以看出,该方法是将数值转成像素数)。

再次总结

通过上述探索,我们不难发现,在Adroid并没有在java代码中直接获取dimens.xml中定义的dp(dip)/sp的值的API,只有getDimension()、getDimensionPixelOffset()和getDimensionPixelSize()这个三个方法来获取绝对尺寸。但有时候我们确实需要动态获取dimen.xml中的值,并为TextView设置字体大小。而这种方法直接应用在textView.setTextSize(dimen);都是有问题的。那我们将从TextView入手,寻找一个正确的姿势来设置字体大小。

setTextSize()

首先把代码端上来:

public void setTextSize(float size) { setTextSize(TypedValue.COMPLEX_UNIT_SP, size); }

原来setTextSize(float)调用了他的重载方法setTextSize(int,float),并且第一个参数传的默认值是TypedValue.COMPLEX_UNIT_SP,眼熟吗,没错就是之前提到的。那么,我们继续看看一下setTextSize(int,float)做了什么:

public void setTextSize(int unit, float size) { if (!isAutoSizeEnabled()) { setTextSizeInternal(unit, size, true /* shouldRequestLayout */); } }

很显然是调用了setTextSizeInternal(unit, size, true /* shouldRequestLayout */);。看到这累不,不过看都看了就再看看呗,说不定比苍老师好看:

private void setTextSizeInternal(int unit, float size, boolean shouldRequestLayout) { Context c = getContext(); Resources r; if (c == null) { r = Resources.getSystem(); } else { r = c.getResources(); } setRawTextSize(TypedValue.applyDimension(unit, size, r.getDisplayMetrics()),shouldRequestLayout); }

高能!!!TypedValue.applyDimension(unit, size, r.getDisplayMetrics())是不是很眼熟???还记得applyDimension()是怎么处理数据的吗?

我们发现在applyDimension()中根据单位的不同,将float乘上不同的系数。如dip/dp需乘上屏幕系数,sp则需乘上字号的缩放系数,pt、in、mm等也是根据相应的算法进行换算(从COMPLEX_UNIT_PX直接返回float可以看出,该方法是将数值转成像素数)

综上,setTextSize(float)给传的值的单位其实是SP,但通过getDimension()取的值却不是这样的。为了证实默认单位是SP,各位看官可以直接传个16,看看和16sp是不是一样的。所以问题是不得到了解决?

结论

Android中并不提供直接从dimens.xml获取dp/sp数值的方法,通过getDimensionPixelSize() getDimension() getDimensionPixelOffset()获取的值是经过处理的。所以正确地动态设置TextView字体大小的姿势应该是:

int dimen = getResources().getDimensionPixelSize(R.dimen.text_size); textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,dimen);


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有