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)

相关推荐
二流小码农41 分钟前
鸿蒙开发:DevEcoStudio中的代码提取
android·ios·harmonyos
江湖有缘1 小时前
使用obsutil工具在OBS上完成基本的数据存取【玩转华为云】
android·java·华为云
移动开发者1号2 小时前
Android 多 BaseUrl 动态切换策略(结合 ServiceManager 实现)
android·kotlin
移动开发者1号3 小时前
Kotlin实现文件上传进度监听:RequestBody封装详解
android·kotlin
AJi5 小时前
Android音视频框架探索(三):系统播放器MediaPlayer的创建流程
android·ffmpeg·音视频开发
柿蒂6 小时前
WorkManager 任务链详解:优雅处理云相册上传队列
android
alexhilton7 小时前
使用用例(Use Case)以让Android代码更简洁
android·kotlin·android jetpack
峥嵘life7 小时前
Android xml的Preference设置visibility=“gone“ 无效分析解决
android·xml
用户2018792831677 小时前
通俗故事:驱动二进制文件在AOSP中的角色
android
穷人小水滴7 小时前
在 Termux 中签名 apk 文件
android·linux·apk