Android 自定义属性 attribute 多种类型的处理
2022-12-14 本文已影响0人
小强开学前
谷歌源码
@NonNull
private static CornerSize getCornerSize(
TypedArray a, int index, @NonNull CornerSize defaultValue) {
TypedValue value = a.peekValue(index);
if (value == null) {
return defaultValue;
}
if (value.type == TypedValue.TYPE_DIMENSION) {
// Eventually we might want to change this to call getDimension() since corner sizes support
// floats.
return new AbsoluteCornerSize(
TypedValue.complexToDimensionPixelSize(value.data, a.getResources().getDisplayMetrics()));
} else if (value.type == TypedValue.TYPE_FRACTION) {
return new RelativeCornerSize(value.getFraction(1.0f, 1.0f));
} else {
return defaultValue;
}
}