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)

相关推荐
Jeled2 小时前
Retrofit 与 OkHttp 全面解析与实战使用(含封装示例)
android·okhttp·android studio·retrofit
ii_best4 小时前
IOS/ 安卓开发工具按键精灵Sys.GetAppList 函数使用指南:轻松获取设备已安装 APP 列表
android·开发语言·ios·编辑器
2501_915909064 小时前
iOS 26 文件管理实战,多工具组合下的 App 数据访问与系统日志调试方案
android·ios·小程序·https·uni-app·iphone·webview
limingade6 小时前
手机转SIP-手机做中继网关-落地线路对接软交换呼叫中心
android·智能手机·手机转sip·手机做sip中继网关·sip中继
RainbowC06 小时前
GapBuffer高效标记管理算法
android·算法
程序员码歌6 小时前
豆包Seedream4.0深度体验:p图美化与文生图创作
android·前端·后端
、花无将7 小时前
PHP:下载、安装、配置,与apache搭建
android·php·apache
shaominjin1238 小时前
Android 约束布局(ConstraintLayout)的权重机制:用法与对比解析
android·网络
我命由我123459 小时前
Android 对话框 - 对话框全屏显示(设置 Window 属性、使用自定义样式、继承 DialogFragment 实现、继承 Dialog 实现)
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
怪兽201410 小时前
请例举 Android 中常用布局类型,并简述其用法以及排版效率
android·面试