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)

相关推荐
似霰4 小时前
传统 Hal 开发笔记6----App 访问硬件服务
android·framework·hal
爱装代码的小瓶子5 小时前
【c++知识铺子】封装map和set(详细版)
android·java·c++
私人珍藏库5 小时前
AutoGLM无需豆包手机,让AI自动帮你点外卖-刷视频
android·ai·智能手机·工具·软件·辅助·autoglm
孤舟簔笠翁6 小时前
【Android驱动14】Android系统Crash工具使用方法和分析
android
帅得不敢出门7 小时前
MTK Android11 APP调用OTA升级
android·java·开发语言·framework
2501_915909067 小时前
苹果应用加密方案的一种方法,在没有源码的前提下,如何处理 IPA 的安全问题
android·安全·ios·小程序·uni-app·iphone·webview
用户2018792831677 小时前
Android App 换肤原理:用 "装修小房子" 故事浅谈
android
百锦再7 小时前
与AI沟通的正确方式——AI提示词:原理、策略与精通之道
android·java·开发语言·人工智能·python·ui·uni-app
2501_915909067 小时前
iOS 项目中常被忽略的 Bundle ID 管理问题
android·ios·小程序·https·uni-app·iphone·webview
dora7 小时前
如何防防防之防抓包伪造请求
android·安全