下面列出了android.util.TypedValue#complexToFloat ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
Description parseValue(TypedValue value) {
Description d = new Description();
if (value == null) {
d.type = ABSOLUTE;
d.value = 0;
} else {
if (value.type == TypedValue.TYPE_FRACTION) {
d.type = (value.data & TypedValue.COMPLEX_UNIT_MASK) ==
TypedValue.COMPLEX_UNIT_FRACTION_PARENT ?
RELATIVE_TO_PARENT : RELATIVE_TO_SELF;
d.value = TypedValue.complexToFloat(value.data);
return d;
} else if (value.type == TypedValue.TYPE_FLOAT) {
d.type = ABSOLUTE;
d.value = value.getFloat();
return d;
} else if (value.type >= TypedValue.TYPE_FIRST_INT &&
value.type <= TypedValue.TYPE_LAST_INT) {
d.type = ABSOLUTE;
d.value = value.data;
return d;
}
}
d.type = ABSOLUTE;
d.value = 0.0f;
return d;
}
/**
* Size descriptions can appear inthree forms:
* <ol>
* <li>An absolute size. This is represented by a number.</li>
* <li>A size relative to the size of the object being animated. This
* is represented by a number followed by "%".</li> *
* <li>A size relative to the size of the parent of object being
* animated. This is represented by a number followed by "%p".</li>
* </ol>
* @param value The typed value to parse
* @return The parsed version of the description
*/
static Description parseValue(TypedValue value) {
Description d = new Description();
if (value == null) {
d.type = ABSOLUTE;
d.value = 0;
} else {
if (value.type == TypedValue.TYPE_FRACTION) {
d.type = (value.data & TypedValue.COMPLEX_UNIT_MASK) ==
TypedValue.COMPLEX_UNIT_FRACTION_PARENT ?
RELATIVE_TO_PARENT : RELATIVE_TO_SELF;
d.value = TypedValue.complexToFloat(value.data);
return d;
} else if (value.type == TypedValue.TYPE_FLOAT) {
d.type = ABSOLUTE;
d.value = value.getFloat();
return d;
} else if (value.type >= TypedValue.TYPE_FIRST_INT &&
value.type <= TypedValue.TYPE_LAST_INT) {
d.type = ABSOLUTE;
d.value = value.data;
return d;
}
}
d.type = ABSOLUTE;
d.value = 0.0f;
return d;
}
private Description parseValue(TypedValue value) {
Description d = new Description();
if (value == null) {
d.type = ABSOLUTE;
d.value = 0;
} else {
if (value.type == TypedValue.TYPE_FRACTION) {
d.type = (value.data & TypedValue.COMPLEX_UNIT_MASK) ==
TypedValue.COMPLEX_UNIT_FRACTION_PARENT ?
RELATIVE_TO_PARENT : RELATIVE_TO_SELF;
d.value = TypedValue.complexToFloat(value.data);
return d;
} else if (value.type == TypedValue.TYPE_FLOAT) {
d.type = ABSOLUTE;
d.value = value.getFloat();
return d;
} else if (value.type >= TypedValue.TYPE_FIRST_INT &&
value.type <= TypedValue.TYPE_LAST_INT) {
d.type = ABSOLUTE;
d.value = value.data;
return d;
}
}
d.type = ABSOLUTE;
d.value = 0.0f;
return d;
}
/**
* 获得dimen.xml中原始的值
*
* @param context the context
* @param id the id
* @return xml def
*/
public static int getXmlDef(Context context, int id) {
synchronized (mTmpValue) {
TypedValue value = mTmpValue;
context.getResources().getValue(id, value, true);
return (int) TypedValue.complexToFloat(value.data);
}
}
/**
* get the raw value from a complex value ( Ex: complex = 14sp, returns 14)
*/
private float getRawSizeValue(TypedArray a, int index, float defValue) {
TypedValue outValue = new TypedValue();
boolean result = a.getValue(index, outValue);
if (!result) {
return defValue;
}
return TypedValue.complexToFloat(outValue.data);
}
Description parseValue(TypedValue value) {
Description d = new Description();
if (value == null) {
d.type = ABSOLUTE;
d.value = 0;
} else {
if (value.type == TypedValue.TYPE_FRACTION) {
d.type = (value.data & TypedValue.COMPLEX_UNIT_MASK) ==
TypedValue.COMPLEX_UNIT_FRACTION_PARENT ?
RELATIVE_TO_PARENT : RELATIVE_TO_SELF;
d.value = TypedValue.complexToFloat(value.data);
return d;
} else if (value.type == TypedValue.TYPE_FLOAT) {
d.type = ABSOLUTE;
d.value = value.getFloat();
return d;
} else if (value.type >= TypedValue.TYPE_FIRST_INT &&
value.type <= TypedValue.TYPE_LAST_INT) {
d.type = ABSOLUTE;
d.value = value.data;
return d;
}
}
d.type = ABSOLUTE;
d.value = 0.0f;
return d;
}
public static int getActionBarHeightInDp(Context context) {
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
final DisplayMetrics dm = context.getResources().getDisplayMetrics();
if (Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv,
true))
actionBarHeight = (int) TypedValue.complexToFloat(tv.data);
} else {
tv.data = 48;
actionBarHeight = (int) TypedValue.complexToFloat(tv.data);
}
return actionBarHeight;
}
/**
* 获取资源中的数值,没有经过转换,比如dp,sp等
*/
public static int getResourceValue(Context context, int resId) {
TypedValue value = mTmpValue;
context.getResources().getValue(resId, value, true);
return (int) TypedValue.complexToFloat(value.data);
}