How does one use Resources.getFraction()


转:http://stackoverflow.com/questions/11734470/how-does-one-use-resources-getfraction

How do I store a fractional value like 3.1416 in resources? What to write in the XML and how to retrieve it in Java code?

The documentation for getFraction() states:

public float getFraction (int id, int base, int pbase)

Retrieve a fractional unit for a particular resource ID.

Parameters
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

This answer shows a simple example of percentages without going into the details of what the arguments mean.

share improve this question
 

1 Answer

You specify fractions in XML as so:

   <item name="fraction" type="fraction">5%</item>
   <item name="parent_fraction" type="fraction">2%p</item>

Where 5% would be 0.05 when actually used.

Then:

// 0.05f
getResources().getFraction(R.fraction.fraction, 1, 1);
// 0.02f
getResources().getFraction(R.fraction.parent_fraction, 1, 1);
// 0.10f
getResources().getFraction(R.fraction.fraction, 2, 1);
// 0.10f
getResources().getFraction(R.fraction.fraction, 2, 2);
// 0.04f
getResources().getFraction(R.fraction.parent_fraction, 1, 2);
// 0.04f
getResources().getFraction(R.fraction.parent_fraction, 2, 2);

As you can see, depending on the type of fraction, the getFraction method multiples the values accordingly. If you specify a parent fraction (%p), it uses the second argument (pbase), ignoring the first. On the other hand, specifying a normal fraction, only the base argument is used, multiplying the fraction by this.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值