List of usage examples for android.content.res TypedArray peekValue
public TypedValue peekValue(@StyleableRes int index)
From source file:android.support.graphics.drawable.AnimatorInflaterCompat.java
private static int inferValueTypeFromValues(TypedArray styledAttributes, int valueFromId, int valueToId) { TypedValue tvFrom = styledAttributes.peekValue(valueFromId); boolean hasFrom = (tvFrom != null); int fromType = hasFrom ? tvFrom.type : 0; TypedValue tvTo = styledAttributes.peekValue(valueToId); boolean hasTo = (tvTo != null); int toType = hasTo ? tvTo.type : 0; int valueType; // Check whether it's color type. If not, fall back to default type (i.e. float type) if ((hasFrom && isColorType(fromType)) || (hasTo && isColorType(toType))) { valueType = VALUE_TYPE_COLOR;/*from w w w. ja v a 2 s . c om*/ } else { valueType = VALUE_TYPE_FLOAT; } return valueType; }
From source file:android.support.graphics.drawable.AnimatorInflaterCompat.java
private static PropertyValuesHolder getPVH(TypedArray styledAttributes, int valueType, int valueFromId, int valueToId, String propertyName) { TypedValue tvFrom = styledAttributes.peekValue(valueFromId); boolean hasFrom = (tvFrom != null); int fromType = hasFrom ? tvFrom.type : 0; TypedValue tvTo = styledAttributes.peekValue(valueToId); boolean hasTo = (tvTo != null); int toType = hasTo ? tvTo.type : 0; if (valueType == VALUE_TYPE_UNDEFINED) { // Check whether it's color type. If not, fall back to default type (i.e. float type) if ((hasFrom && isColorType(fromType)) || (hasTo && isColorType(toType))) { valueType = VALUE_TYPE_COLOR; } else {// w w w .ja v a 2s . c o m valueType = VALUE_TYPE_FLOAT; } } boolean getFloats = (valueType == VALUE_TYPE_FLOAT); PropertyValuesHolder returnValue = null; if (valueType == VALUE_TYPE_PATH) { String fromString = styledAttributes.getString(valueFromId); String toString = styledAttributes.getString(valueToId); PathParser.PathDataNode[] nodesFrom = PathParser.createNodesFromPathData(fromString); PathParser.PathDataNode[] nodesTo = PathParser.createNodesFromPathData(toString); if (nodesFrom != null || nodesTo != null) { if (nodesFrom != null) { TypeEvaluator evaluator = new PathDataEvaluator(); if (nodesTo != null) { if (!PathParser.canMorph(nodesFrom, nodesTo)) { throw new InflateException(" Can't morph from " + fromString + " to " + toString); } returnValue = PropertyValuesHolder.ofObject(propertyName, evaluator, nodesFrom, nodesTo); } else { returnValue = PropertyValuesHolder.ofObject(propertyName, evaluator, (Object) nodesFrom); } } else if (nodesTo != null) { TypeEvaluator evaluator = new PathDataEvaluator(); returnValue = PropertyValuesHolder.ofObject(propertyName, evaluator, (Object) nodesTo); } } } else { TypeEvaluator evaluator = null; // Integer and float value types are handled here. if (valueType == VALUE_TYPE_COLOR) { // special case for colors: ignore valueType and get ints evaluator = ArgbEvaluator.getInstance(); } if (getFloats) { float valueFrom; float valueTo; if (hasFrom) { if (fromType == TypedValue.TYPE_DIMENSION) { valueFrom = styledAttributes.getDimension(valueFromId, 0f); } else { valueFrom = styledAttributes.getFloat(valueFromId, 0f); } if (hasTo) { if (toType == TypedValue.TYPE_DIMENSION) { valueTo = styledAttributes.getDimension(valueToId, 0f); } else { valueTo = styledAttributes.getFloat(valueToId, 0f); } returnValue = PropertyValuesHolder.ofFloat(propertyName, valueFrom, valueTo); } else { returnValue = PropertyValuesHolder.ofFloat(propertyName, valueFrom); } } else { if (toType == TypedValue.TYPE_DIMENSION) { valueTo = styledAttributes.getDimension(valueToId, 0f); } else { valueTo = styledAttributes.getFloat(valueToId, 0f); } returnValue = PropertyValuesHolder.ofFloat(propertyName, valueTo); } } else { int valueFrom; int valueTo; if (hasFrom) { if (fromType == TypedValue.TYPE_DIMENSION) { valueFrom = (int) styledAttributes.getDimension(valueFromId, 0f); } else if (isColorType(fromType)) { valueFrom = styledAttributes.getColor(valueFromId, 0); } else { valueFrom = styledAttributes.getInt(valueFromId, 0); } if (hasTo) { if (toType == TypedValue.TYPE_DIMENSION) { valueTo = (int) styledAttributes.getDimension(valueToId, 0f); } else if (isColorType(toType)) { valueTo = styledAttributes.getColor(valueToId, 0); } else { valueTo = styledAttributes.getInt(valueToId, 0); } returnValue = PropertyValuesHolder.ofInt(propertyName, valueFrom, valueTo); } else { returnValue = PropertyValuesHolder.ofInt(propertyName, valueFrom); } } else { if (hasTo) { if (toType == TypedValue.TYPE_DIMENSION) { valueTo = (int) styledAttributes.getDimension(valueToId, 0f); } else if (isColorType(toType)) { valueTo = styledAttributes.getColor(valueToId, 0); } else { valueTo = styledAttributes.getInt(valueToId, 0); } returnValue = PropertyValuesHolder.ofInt(propertyName, valueTo); } } } if (returnValue != null && evaluator != null) { returnValue.setEvaluator(evaluator); } } return returnValue; }
From source file:com.android.settingslib.RestrictedSwitchPreference.java
public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); setWidgetLayoutResource(R.layout.restricted_switch_widget); mHelper = new RestrictedPreferenceHelper(context, this, attrs); if (attrs != null) { final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.RestrictedSwitchPreference); final TypedValue useAdditionalSummary = attributes .peekValue(R.styleable.RestrictedSwitchPreference_useAdditionalSummary); if (useAdditionalSummary != null) { mUseAdditionalSummary = (useAdditionalSummary.type == TypedValue.TYPE_INT_BOOLEAN && useAdditionalSummary.data != 0); }/*from ww w . j a v a 2 s . c o m*/ final TypedValue restrictedSwitchSummary = attributes .peekValue(R.styleable.RestrictedSwitchPreference_restrictedSwitchSummary); CharSequence data = null; if (restrictedSwitchSummary != null && restrictedSwitchSummary.type == TypedValue.TYPE_STRING) { if (restrictedSwitchSummary.resourceId != 0) { data = context.getString(restrictedSwitchSummary.resourceId); } else { data = restrictedSwitchSummary.string; } } mRestrictedSwitchSummary = data == null ? null : data.toString(); } if (mRestrictedSwitchSummary == null) { mRestrictedSwitchSummary = context.getString(R.string.disabled_by_admin); } if (mUseAdditionalSummary) { setLayoutResource(R.layout.restricted_switch_preference); useAdminDisabledSummary(false); } }
From source file:com.bilibili.magicasakura.utils.GradientDrawableUtils.java
float getAttrFloatOrFraction(Context context, AttributeSet attrs, int attr, float defaultValue, float base, float pbase) { TypedArray a = 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(); }//from w w w .j a va 2 s . c om a.recycle(); return v; }
From source file:com.bilibili.magicasakura.utils.GradientDrawableInflateImpl.java
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(); }/*from ww w. j a v a 2 s. c o m*/ a.recycle(); return v; }
From source file:com.bilibili.magicasakura.utils.GradientDrawableUtils.java
void setGradientRadius(Context context, AttributeSet attrs, GradientDrawable drawable, int gradientType) throws XmlPullParserException { TypedArray a = obtainAttributes(context.getResources(), context.getTheme(), attrs, new int[] { android.R.attr.gradientRadius }); TypedValue value = a.peekValue(0); if (value != null) { boolean radiusRel = value.type == TypedValue.TYPE_FRACTION; drawable.setGradientRadius(radiusRel ? value.getFraction(1.0f, 1.0f) : value.getFloat()); } else if (gradientType == GradientDrawable.RADIAL_GRADIENT) { throw new XmlPullParserException( "<gradient> tag requires 'gradientRadius' " + "attribute with radial type"); }/*from w w w . j av a 2s. c o m*/ a.recycle(); }
From source file:com.bilibili.magicasakura.utils.GradientDrawableInflateImpl.java
void setGradientRadius(Context context, AttributeSet attrs, GradientDrawable drawable, int gradientType) throws XmlPullParserException { TypedArray a = DrawableUtils.obtainAttributes(context.getResources(), context.getTheme(), attrs, new int[] { android.R.attr.gradientRadius }); TypedValue value = a.peekValue(0); if (value != null) { boolean radiusRel = value.type == TypedValue.TYPE_FRACTION; drawable.setGradientRadius(radiusRel ? value.getFraction(1.0f, 1.0f) : value.getFloat()); } else if (gradientType == GradientDrawable.RADIAL_GRADIENT) { throw new XmlPullParserException( "<gradient> tag requires 'gradientRadius' " + "attribute with radial type"); }//from w ww.jav a 2 s . com a.recycle(); }
From source file:net.alexjf.tmm.fragments.IconPickerFragment.java
private void updateIcons() { Resources res = getActivity().getResources(); TypedArray iconCats = res.obtainTypedArray(R.array.iconcats); int numCategories = iconCats.length(); iconCategories = new ArrayList<IconCategory>(numCategories); for (int i = 0; i < numCategories; i++) { int resId = iconCats.peekValue(i).resourceId; if (resId > 0) { IconCategory iconCat = new IconCategory(); TypedArray catInfo = res.obtainTypedArray(resId); iconCat.title = catInfo.getString(0); int entriesResId = catInfo.getResourceId(1, 0); if (entriesResId > 0) { TypedArray icons = res.obtainTypedArray(entriesResId); int numIcons = icons.length(); iconCat.drawableIds = new ArrayList<Integer>(numIcons); for (int j = 0; j < numIcons; j++) { int drawableId = icons.getResourceId(j, 0); iconCat.drawableIds.add(drawableId); }/* w ww . ja v a 2 s .c o m*/ icons.recycle(); } else { Log.e("TMM", "Error reading icon entries of cat with index " + i); } iconCategories.add(iconCat); } else { Log.e("TMM", "Error reading icon category with index " + i); } } iconCats.recycle(); Collections.sort(iconCategories, new IconCategory.Comparator()); }
From source file:com.achep.base.dashboard.DashboardCategory.java
public DashboardCategory(Context context, AttributeSet attrs) { TypedArray sa = context.obtainStyledAttributes(attrs, R.styleable.DashboardTile); id = sa.getResourceId(R.styleable.DashboardTile_dashboard_id, CAT_ID_UNDEFINED); TypedValue tv = sa.peekValue(R.styleable.DashboardTile_dashboard_title); if (tv != null && tv.type == TypedValue.TYPE_STRING) { if (tv.resourceId != 0) { titleRes = tv.resourceId;// ww w.j a v a 2 s. c o m } else { title = tv.string; } } sa.recycle(); }
From source file:android.support.design.widget.BottomSheetBehavior.java
/** * Default constructor for inflating BottomSheetBehaviors from layout. * * @param context The {@link Context}./*from ww w . j av a 2 s . co m*/ * @param attrs The {@link AttributeSet}. */ public BottomSheetBehavior(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(); }