下面列出了android.content.res.TypedArray#peekValue() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Default constructor for inflating BottomSheetBehaviors from layout.
*
* @param context The {@link Context}.
* @param attrs The {@link AttributeSet}.
*/
public BottomSheetBehaviorV2(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
android.support.design.R.styleable.BottomSheetBehavior_Layout);
TypedValue value = a.peekValue(android.support.design.R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight);
if (value != null && value.data == PEEK_HEIGHT_AUTO) {
setPeekHeight(value.data);
} else {
setPeekHeight(a.getDimensionPixelSize(
android.support.design.R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight, PEEK_HEIGHT_AUTO));
}
setHideable(a.getBoolean(android.support.design.R.styleable.BottomSheetBehavior_Layout_behavior_hideable, false));
setSkipCollapsed(a.getBoolean(android.support.design.R.styleable.BottomSheetBehavior_Layout_behavior_skipCollapsed,
false));
a.recycle();
ViewConfiguration configuration = ViewConfiguration.get(context);
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
}
private static int inferValueTypeOfKeyframe(Resources res, Theme theme, AttributeSet attrs) {
int valueType;
TypedArray a;
if (theme != null) {
a = theme.obtainStyledAttributes(attrs, R.styleable.Keyframe, 0, 0);
} else {
a = res.obtainAttributes(attrs, R.styleable.Keyframe);
}
TypedValue keyframeValue = a.peekValue(R.styleable.Keyframe_value);
boolean hasValue = (keyframeValue != null);
// When no value type is provided, check whether it's a color type first.
// If not, fall back to default value type (i.e. float type).
if (hasValue && isColorType(keyframeValue.type)) {
valueType = VALUE_TYPE_COLOR;
} else {
valueType = VALUE_TYPE_FLOAT;
}
a.recycle();
return valueType;
}
public static void loadColors(Resources.Theme theme, int resId) {
TypedArray ta = theme.obtainStyledAttributes(resId, R.styleable.IRCColors);
sColorValues = new int[R.styleable.IRCColors.length];
for (int i = 0; i < sColorValues.length; i++) {
try {
int j = i;
TypedValue tv;
while ((tv = ta.peekValue(j)) != null && tv.type == TypedValue.TYPE_ATTRIBUTE)
j = Arrays.binarySearch(R.styleable.IRCColors, tv.data);
sColorValues[i] = ta.getColor(j, Color.RED);
} catch (UnsupportedOperationException e) {
e.printStackTrace();
sColorValues[i] = Color.RED;
}
}
ta.recycle();
}
/**
* Default constructor for inflating ViewPagerBottomSheetBehaviors from layout.
*
* @param context The {@link Context}.
* @param attrs The {@link AttributeSet}.
*/
public ViewPagerBottomSheetBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.BottomSheetBehavior_Layout);
TypedValue value = a.peekValue(R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight);
if (value != null && value.data == PEEK_HEIGHT_AUTO) {
setPeekHeight(value.data);
} else {
setPeekHeight(a.getDimensionPixelSize(
R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight, PEEK_HEIGHT_AUTO));
}
setHideable(a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_hideable, false));
setSkipCollapsed(a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_skipCollapsed,
false));
a.recycle();
ViewConfiguration configuration = ViewConfiguration.get(context);
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
}
private static String getData(Context context, AttributeSet set, int attribute)
throws ReflectiveOperationException {
final TypedArray sa = context.obtainStyledAttributes(set, new int[] {attribute});
try {
final TypedValue tv = sa.peekValue(0);
CharSequence data = null;
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
data = context.getText(tv.resourceId);
} else {
data = tv.string;
}
}
return (data != null) ? data.toString() : null;
} finally {
sa.recycle();
}
}
public BottomSheetBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BottomSheetBehavior_Layout);
this.shapeThemingEnabled = a.hasValue(R.styleable.BottomSheetBehavior_Layout_shapeAppearance);
boolean hasBackgroundTint = a.hasValue(R.styleable.BottomSheetBehavior_Layout_backgroundTint);
if (hasBackgroundTint) {
ColorStateList bottomSheetColor =
MaterialResources.getColorStateList(
context, a, R.styleable.BottomSheetBehavior_Layout_backgroundTint);
createMaterialShapeDrawable(context, attrs, hasBackgroundTint, bottomSheetColor);
} else {
createMaterialShapeDrawable(context, attrs, hasBackgroundTint);
}
createShapeValueAnimator();
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
this.elevation = a.getDimension(R.styleable.BottomSheetBehavior_Layout_android_elevation, -1);
}
TypedValue value = a.peekValue(R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight);
if (value != null && value.data == PEEK_HEIGHT_AUTO) {
setPeekHeight(value.data);
} else {
setPeekHeight(
a.getDimensionPixelSize(
R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight, PEEK_HEIGHT_AUTO));
}
setHideable(a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_hideable, false));
setFitToContents(
a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_fitToContents, true));
setSkipCollapsed(
a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_skipCollapsed, false));
setSaveFlags(a.getInt(R.styleable.BottomSheetBehavior_Layout_behavior_saveFlags, SAVE_NONE));
setHalfExpandedRatio(a.getFloat(R.styleable.BottomSheetBehavior_Layout_behavior_halfExpandedRatio, 0.5f));
setExpandedOffset(a.getInt(R.styleable.BottomSheetBehavior_Layout_behavior_expandedOffset, 0));
a.recycle();
ViewConfiguration configuration = ViewConfiguration.get(context);
maximumVelocity = configuration.getScaledMaximumFlingVelocity();
}
private boolean parsePackageItemInfo(Package owner, PackageItemInfo outInfo,
String[] outError, String tag, TypedArray sa,
int nameRes, int labelRes, int iconRes, int logoRes) {
String name = sa.getNonConfigurationString(nameRes, 0);
if (name == null) {
outError[0] = tag + " does not specify android:name";
return false;
}
outInfo.name
= buildClassName(owner.applicationInfo.packageName, name, outError);
if (outInfo.name == null) {
return false;
}
int iconVal = sa.getResourceId(iconRes, 0);
if (iconVal != 0) {
outInfo.icon = iconVal;
outInfo.nonLocalizedLabel = null;
}
int logoVal = sa.getResourceId(logoRes, 0);
if (logoVal != 0) {
outInfo.logo = logoVal;
}
TypedValue v = sa.peekValue(labelRes);
if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
outInfo.nonLocalizedLabel = v.coerceToString();
}
outInfo.packageName = owner.packageName;
return true;
}
public static float getFraction(final TypedArray a, final int index, final float defValue) {
final TypedValue value = a.peekValue(index);
if (value == null || !isFractionValue(value)) {
return defValue;
}
return a.getFraction(index, 1, 1, defValue);
}
public static int getDimensionPixelSize(final TypedArray a, final int index) {
final TypedValue value = a.peekValue(index);
if (value == null || !isDimensionValue(value)) {
return ResourceUtils.UNDEFINED_DIMENSION;
}
return a.getDimensionPixelSize(index, ResourceUtils.UNDEFINED_DIMENSION);
}
/**
* Default constructor for inflating AnchorBottomSheetBehaviors from layout.
*
* @param context The {@link Context}.
* @param attrs The {@link AttributeSet}.
*/
public AnchorBottomSheetBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
android.support.design.R.styleable.BottomSheetBehavior_Layout);
TypedValue value = a.peekValue(android.support.design.R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight);
if (value != null && value.data == PEEK_HEIGHT_AUTO) {
setPeekHeight(value.data);
} else {
setPeekHeight(a.getDimensionPixelSize(
android.support.design.R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight, PEEK_HEIGHT_AUTO));
}
setHideable(a.getBoolean(android.support.design.R.styleable.BottomSheetBehavior_Layout_behavior_hideable, false));
setSkipCollapsed(a.getBoolean(android.support.design.R.styleable.BottomSheetBehavior_Layout_behavior_skipCollapsed,
false));
setSkipAnchored(a.getBoolean(R.styleable.AnchorBottomSheetBehavior_Layout_behavior_skipAnchored, false));
a.recycle();
a = context.obtainStyledAttributes(attrs, R.styleable.AnchorBottomSheetBehavior_Layout);
mAnchorOffset = (int) a.getDimension(R.styleable.AnchorBottomSheetBehavior_Layout_behavior_anchorOffset, 0);
//noinspection WrongConstant
mState = a.getInt(R.styleable.AnchorBottomSheetBehavior_Layout_behavior_defaultState, mState);
a.recycle();
ViewConfiguration configuration = ViewConfiguration.get(context);
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
}
public static int getEnumValue(final TypedArray a, final int index, final int defValue) {
final TypedValue value = a.peekValue(index);
if (value == null) {
return defValue;
}
if (isIntegerValue(value)) {
return a.getInt(index, defValue);
}
return defValue;
}
private boolean parsePackageItemInfo(DynamicApkInfo owner, PackageItemInfo outInfo,
String[] outError, String tag, TypedArray sa,
int nameRes, int labelRes, int iconRes, int logoRes, int bannerRes) {
String name = sa.getString(nameRes);
if (name == null) {
outError[0] = tag + " does not specify android:name";
return false;
}
outInfo.name
= buildClassName(owner.applicationInfo.packageName, name, outError);
if (outInfo.name == null) {
return false;
}
int iconVal = sa.getResourceId(iconRes, 0);
if (iconVal != 0) {
outInfo.icon = iconVal;
outInfo.nonLocalizedLabel = null;
}
int logoVal = sa.getResourceId(logoRes, 0);
if (logoVal != 0) {
outInfo.logo = logoVal;
}
int bannerVal = sa.getResourceId(bannerRes, 0);
if (bannerVal != 0) {
outInfo.banner = bannerVal;
}
TypedValue v = sa.peekValue(labelRes);
if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
outInfo.nonLocalizedLabel = v.coerceToString();
}
outInfo.packageName = owner.packageName;
return true;
}
public DashboardTile(Context context, AttributeSet attrs) {
TypedArray sa = context.obtainStyledAttributes(attrs, R.styleable.DashboardTile);
TypedValue tv;
id = sa.getResourceId(R.styleable.DashboardTile_dashboard_id, TILE_ID_UNDEFINED);
iconRes = sa.getResourceId(R.styleable.DashboardTile_dashboard_icon, 0);
fragment = sa.getString(R.styleable.DashboardTile_dashboard_fragment);
tv = sa.peekValue(R.styleable.DashboardTile_dashboard_title);
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
titleRes = tv.resourceId;
} else {
title = tv.string;
}
}
tv = sa.peekValue(R.styleable.DashboardTile_dashboard_summary);
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
summaryRes = tv.resourceId;
} else {
summary = tv.string;
}
}
sa.recycle();
}
public static float getFraction(final TypedArray a, final int index, final float defValue) {
final TypedValue value = a.peekValue(index);
if (value == null || !isFractionValue(value)) {
return defValue;
}
return a.getFraction(index, 1, 1, defValue);
}
public static float getDimensionOrFraction(final TypedArray a, final int index, final int base,
final float defValue) {
final TypedValue value = a.peekValue(index);
if (value == null) {
return defValue;
}
if (isFractionValue(value)) {
return a.getFraction(index, base, base, defValue);
} else if (isDimensionValue(value)) {
return a.getDimension(index, defValue);
}
return defValue;
}
public static int getEnumValue(final TypedArray a, final int index, final int defValue) {
final TypedValue value = a.peekValue(index);
if (value == null) {
return defValue;
}
if (isIntegerValue(value)) {
return a.getInt(index, defValue);
}
return defValue;
}
static float getDimensionOrFraction(TypedArray a, int index, int base, float defValue) {
TypedValue value = a.peekValue(index);
if (value == null) return defValue;
if (value.type == TypedValue.TYPE_DIMENSION) {
return a.getDimensionPixelOffset(index, Math.round(defValue));
} else if (value.type == TypedValue.TYPE_FRACTION) {
// Round it to avoid values like 47.9999 from getting truncated
//return Math.round(a.getFraction(index, base, base, defValue));
return a.getFraction(index, base, base, defValue);
}
return defValue;
}
float getAttrFloatOrFraction(Context context, AttributeSet attrs, int attr, float defaultValue, float base, float pbase) {
TypedArray a = DrawableUtils.obtainAttributes(context.getResources(), context.getTheme(), attrs, new int[]{attr});
TypedValue tv = a.peekValue(0);
float v = defaultValue;
if (tv != null) {
boolean isFraction = tv.type == TypedValue.TYPE_FRACTION;
v = isFraction ? tv.getFraction(base, pbase) : tv.getFloat();
}
a.recycle();
return v;
}
public Builder(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LinearProgressDrawable, defStyleAttr, defStyleRes);
int resId;
progressPercent(a.getFloat(R.styleable.LinearProgressDrawable_pv_progress, 0));
secondaryProgressPercent(a.getFloat(R.styleable.LinearProgressDrawable_pv_secondaryProgress, 0));
TypedValue value = a.peekValue(R.styleable.LinearProgressDrawable_lpd_maxLineWidth);
if(value == null)
maxLineWidth(0.75f);
else if(value.type == TypedValue.TYPE_FRACTION)
maxLineWidth(a.getFraction(R.styleable.LinearProgressDrawable_lpd_maxLineWidth, 1, 1, 0.75f));
else
maxLineWidth(a.getDimensionPixelSize(R.styleable.LinearProgressDrawable_lpd_maxLineWidth, 0));
value = a.peekValue(R.styleable.LinearProgressDrawable_lpd_minLineWidth);
if(value == null)
minLineWidth(0.25f);
else if(value.type == TypedValue.TYPE_FRACTION)
minLineWidth(a.getFraction(R.styleable.LinearProgressDrawable_lpd_minLineWidth, 1, 1, 0.25f));
else
minLineWidth(a.getDimensionPixelSize(R.styleable.LinearProgressDrawable_lpd_minLineWidth, 0));
strokeSize(a.getDimensionPixelSize(R.styleable.LinearProgressDrawable_lpd_strokeSize, ThemeUtil.dpToPx(context, 4)));
verticalAlign(a.getInteger(R.styleable.LinearProgressDrawable_lpd_verticalAlign, LinearProgressDrawable.ALIGN_BOTTOM));
strokeColors(a.getColor(R.styleable.LinearProgressDrawable_lpd_strokeColor, ThemeUtil.colorPrimary(context, 0xFF000000)));
if((resId = a.getResourceId(R.styleable.LinearProgressDrawable_lpd_strokeColors, 0)) != 0){
TypedArray ta = context.getResources().obtainTypedArray(resId);
int[] colors = new int[ta.length()];
for(int j = 0; j < ta.length(); j++)
colors[j] = ta.getColor(j, 0);
ta.recycle();
strokeColors(colors);
}
strokeSecondaryColor(a.getColor(R.styleable.LinearProgressDrawable_lpd_strokeSecondaryColor, 0));
reverse(a.getBoolean(R.styleable.LinearProgressDrawable_lpd_reverse, false));
travelDuration(a.getInteger(R.styleable.LinearProgressDrawable_lpd_travelDuration, context.getResources().getInteger(android.R.integer.config_longAnimTime)));
transformDuration(a.getInteger(R.styleable.LinearProgressDrawable_lpd_transformDuration, context.getResources().getInteger(android.R.integer.config_mediumAnimTime)));
keepDuration(a.getInteger(R.styleable.LinearProgressDrawable_lpd_keepDuration, context.getResources().getInteger(android.R.integer.config_shortAnimTime)));
if((resId = a.getResourceId(R.styleable.LinearProgressDrawable_lpd_transformInterpolator, 0)) != 0)
transformInterpolator(AnimationUtils.loadInterpolator(context, resId));
progressMode(a.getInteger(R.styleable.LinearProgressDrawable_pv_progressMode, ProgressView.MODE_INDETERMINATE));
inAnimDuration(a.getInteger(R.styleable.LinearProgressDrawable_lpd_inAnimDuration, context.getResources().getInteger(android.R.integer.config_mediumAnimTime)));
outAnimDuration(a.getInteger(R.styleable.LinearProgressDrawable_lpd_outAnimDuration, context.getResources().getInteger(android.R.integer.config_mediumAnimTime)));
a.recycle();
}
private static Keyframe loadKeyframe(Resources res, Theme theme, AttributeSet attrs,
int valueType)
throws XmlPullParserException, IOException {
TypedArray a;
if (theme != null) {
a = theme.obtainStyledAttributes(attrs, R.styleable.Keyframe, 0, 0);
} else {
a = res.obtainAttributes(attrs, R.styleable.Keyframe);
}
Keyframe keyframe = null;
float fraction = a.getFloat(R.styleable.Keyframe_fraction, -1);
TypedValue keyframeValue = a.peekValue(R.styleable.Keyframe_value);
boolean hasValue = (keyframeValue != null);
if (valueType == VALUE_TYPE_UNDEFINED) {
// When no value type is provided, check whether it's a color type first.
// If not, fall back to default value type (i.e. float type).
if (hasValue && isColorType(keyframeValue.type)) {
valueType = VALUE_TYPE_COLOR;
} else {
valueType = VALUE_TYPE_FLOAT;
}
}
if (hasValue) {
switch (valueType) {
case VALUE_TYPE_FLOAT:
float value = a.getFloat(R.styleable.Keyframe_value, 0);
keyframe = Keyframe.ofFloat(fraction, value);
break;
case VALUE_TYPE_COLOR:
case VALUE_TYPE_INT:
int intValue = a.getInt(R.styleable.Keyframe_value, 0);
keyframe = Keyframe.ofInt(fraction, intValue);
break;
}
} else {
keyframe = (valueType == VALUE_TYPE_FLOAT) ? Keyframe.ofFloat(fraction) :
Keyframe.ofInt(fraction);
}
final int resID = a.getResourceId(R.styleable.Keyframe_interpolator, 0);
if (resID > 0) {
final Interpolator interpolator = AnimationUtils.loadInterpolator(res, theme, resID);
keyframe.setInterpolator(interpolator);
}
a.recycle();
return keyframe;
}