packages/apps/SystemUI$ resgrep status_bar_icon_drawing_alpha
在Android原生代码中,有一个项目,就是SystemUi用到了,搜索上面这句可以看到是下面这样用的。
final float alpha = res.getFraction(R.dimen.status_bar_icon_drawing_alpha, 1, 1);
setAlpha(alpha);
You specify fractions in XML as so:
<item name="fraction" type="fraction">5%</item>
<item name="parent_fraction" type="fraction">2%p</item>
Where 5% would be 0.05 when actually used.
Then:
// 0.05f
getResources().getFraction(R.fraction.fraction, 1, 1);
// 0.02f
getResources().getFraction(R.fraction.parent_fraction, 1, 1);
// 0.10f
getResources().getFraction(R.fraction.fraction, 2, 1);
// 0.10f
getResources().getFraction(R.fraction.fraction, 2, 2);
// 0.04f
getResources().getFraction(R.fraction.parent_fraction, 1, 2);
// 0.04f
getResources().getFraction(R.fraction.parent_fraction, 2, 2);
As you can see, depending on the type of fraction, the getFraction
method multiples the values accordingly. If you specify a parent fraction (%p
), it uses the second argument (pbase
), ignoring the first. On the other hand, specifying a normal fraction, only the base
argument is used, multiplying he fraction by this.
<item type="fraction" name="fraction_test">5%</item>
<item name="xxx" type="drawable" format="reference">@drawable/abc_ab_bottom_solid_dark_holo</item>
用eclipse的自动提示发现:type的参数有很多,从bool, integer, drawable.... format也有很多格式。我用加粗的行做测试,结果如下:
float result = getActivity().getResources().getFraction(R.fraction.fraction_test, 5, 6);
1)如果fraction_test 是5%,那么result就是:5%*5 ~ 0.25 (约等于0.25,然后转为int就是0.25)