Android R.fraction

来源

我是在看Android10原生代码,绘制状态栏蓝牙电量相关类中第一次看到R.fraction的,如类BatteryMeterDrawable

复制代码
<fraction name="battery_button_height_fraction">10%</fraction>

mButtonHeightFraction = context.getResources().getFraction(

R.fraction.bt_battery_button_height_fraction, 1, 1);

方法解释

复制代码
public float getFraction(@FractionRes int id, int base, int pbase) 
源码中方法解释如下:
Retrieve a fractional unit for a particular resource ID.
Params:
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. base – The base value of this fraction. In other words, a standard fraction is multiplied by this value. pbase – The parent base value of this fraction. In other words, a parent fraction (nn%p) is multiplied by this value.
Returns:
Attribute fractional value multiplied by the appropriate base value.
Throws:
Resources.NotFoundException – Throws NotFoundException if the given ID does not exist.

用处

我自己没用过,待补充

测试

分别测试fraction的两种声明:有p的跟没p的

<item name="fraction" type="fraction">30%</item>

<item name="fraction" type="fraction">30%p</item>

测试没p的:

测试案例1:保持第三个参数不变

测试:

        //<item name="fraction" type="fraction">30%</item>
        float fraction1 = this.getResources().getFraction(R.fraction.fraction, 1, 1);
        float fraction2 = this.getResources().getFraction(R.fraction.fraction, 2, 1);
        float fraction3 = this.getResources().getFraction(R.fraction.fraction, 3, 1);
        float fraction4 = this.getResources().getFraction(R.fraction.fraction, 4, 1);

结果:

分析:

都是30%乘以第二个参数(base)

测试案例1:第三个参数改变

测试:

        //<item name="fraction" type="fraction">30%</item>
        float fraction1 = this.getResources().getFraction(R.fraction.fraction, 1, 2);
        float fraction2 = this.getResources().getFraction(R.fraction.fraction, 1, 3);
        float fraction3 = this.getResources().getFraction(R.fraction.fraction, 2, 2);
        float fraction4 = this.getResources().getFraction(R.fraction.fraction, 2, 3);

结果:

分析:

都是30%乘以第二个参数(base)

总结以上两个测试案例可知:在没p的情况下,getFraction方法返回的结果就是乘以第二个参数;

测试有p的:

测试:

        //<item name="fraction" type="fraction">30%p</item>
        float fraction1 = this.getResources().getFraction(R.fraction.fraction, 1, 2);
        float fraction2 = this.getResources().getFraction(R.fraction.fraction, 1, 3);
        float fraction3 = this.getResources().getFraction(R.fraction.fraction, 2, 2);
        float fraction4 = this.getResources().getFraction(R.fraction.fraction, 2, 3);

结果:

分析:

都是30%乘以第三个参数(pbase)

相关推荐
沐言人生3 小时前
Android10 Framework—Init进程-8.服务端属性文件创建和mmap映射
android
沐言人生3 小时前
Android10 Framework—Init进程-9.服务端属性值初始化
android·android studio·android jetpack
追光天使4 小时前
【Mac】和【安卓手机】 通过有线方式实现投屏
android·macos·智能手机·投屏·有线
小雨cc5566ru4 小时前
uniapp+Android智慧居家养老服务平台 0fjae微信小程序
android·微信小程序·uni-app
一切皆是定数5 小时前
Android车载——VehicleHal初始化(Android 11)
android·gitee
一切皆是定数5 小时前
Android车载——VehicleHal运行流程(Android 11)
android
problc5 小时前
Android 组件化利器:WMRouter 与 DRouter 的选择与实践
android·java
图王大胜6 小时前
Android SystemUI组件(11)SystemUIVisibility解读
android·framework·systemui·visibility
服装学院的IT男10 小时前
【Android 13源码分析】Activity生命周期之onCreate,onStart,onResume-2
android
Arms20610 小时前
android 全面屏最底部栏沉浸式
android