• Android R.fraction


    来源

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

    10%

    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的

    type="fraction">30%
    30%p

    测试没p的:

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

    测试:

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

    结果:

    分析:

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

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

     测试:

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

    结果:

    分析:

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

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

    测试有p的:

     测试:

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

    结果:

    分析:

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

  • 相关阅读:
    超越代写!5步教你轻松利用ChatGPT创作文本
    享元模式介绍(含案例代码)
    学生HTML网页作业作品:HTML+CSS网站设计与实现【红色喜庆邀请函 3页】
    跟着实例学Go语言(三)
    Spring(ioc)
    不想搞了,4年经验去面试10分钟就结束了,如今测试岗为什么这么难......
    [架构之路-235]:目标系统 - 纵向分层 - 数据库 - 数据库系统基础与概述:数据库定义、核心概念、系统组成
    UG\NX二次开发 选择基准平面 UF_UI_select_with_single_dialog
    头文件和编译的问题
    基于腾讯元器搭建前端小助手
  • 原文地址:https://blog.csdn.net/lxjlxj2333/article/details/134293508