public static int getType(TypedArray array, int index){ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return array.getType(index); else{ TypedValue value = array.peekValue(index); return value == null ? TypedValue.TYPE_NULL : value.type; } }
private int loadTextSizeFromTextAppearance(int textAppearanceResId) { TypedArray a = getContext().obtainStyledAttributes(textAppearanceResId, R.styleable.TextAppearance); try { if (!DimenUtils.isPxVal(a.peekValue(R.styleable.TextAppearance_android_textSize))) return NO_VALID; return a.getDimensionPixelSize(R.styleable.TextAppearance_android_textSize, NO_VALID); } finally { a.recycle(); } }
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(); } a.recycle(); return v; }
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"); } a.recycle(); }
setSecondaryProgress(a.getFloat(attr, 0)); else if(attr == R.styleable.LinearProgressDrawable_lpd_maxLineWidth){ TypedValue value = a.peekValue(attr); if(value.type == TypedValue.TYPE_FRACTION) { mMaxLineWidthPercent = a.getFraction(attr, 1, 1, 0.75f); TypedValue value = a.peekValue(attr); if(value.type == TypedValue.TYPE_FRACTION) { mMinLineWidthPercent = a.getFraction(attr, 1, 1, 0.25f);
secondaryProgressPercent(a.getFloat(R.styleable.LinearProgressDrawable_pv_secondaryProgress, 0)); TypedValue value = a.peekValue(R.styleable.LinearProgressDrawable_lpd_maxLineWidth); if(value == null) maxLineWidth(0.75f); maxLineWidth(a.getDimensionPixelSize(R.styleable.LinearProgressDrawable_lpd_maxLineWidth, 0)); value = a.peekValue(R.styleable.LinearProgressDrawable_lpd_minLineWidth); if(value == null) minLineWidth(0.25f);
@Test public void obtainAttributes() { TypedArray typedArray = resources.obtainAttributes(Robolectric.buildAttributeSet() .addAttribute(R.attr.styleReference, "@xml/shortcuts") .build(), new int[]{R.attr.styleReference}); assertThat(typedArray).isNotNull(); assertThat(typedArray.peekValue(0).resourceId).isEqualTo(R.xml.shortcuts); }
int type = isInEditMode() ? TypedValue.TYPE_STRING : ta.peekValue(R.styleable.ParallaxView_src).type; if (type == TypedValue.TYPE_REFERENCE) { int resourceId = ta.getResourceId(R.styleable.ParallaxView_src, 0);
private static int resolveColor(@NonNull TypedArray ca, int i, @NonNull Resources.Theme theme) { if (ca.peekValue(i).type == TypedValue.TYPE_ATTRIBUTE) { final TypedValue typedValue = new TypedValue(); theme.resolveAttribute(ca.peekValue(i).data, typedValue, true); return typedValue.data; } else { return ca.getColor(i, 0); } }
TypedArray ca = getResources().obtainTypedArray(id); int[] c = new int[ca.length()]; for (int i = 0; i < c.length; i++) { if (ca.peekValue(i).type == TypedValue.TYPE_ATTRIBUTE) { TypedValue typedValue = new TypedValue(); getTheme().resolveAttribute(ca.peekValue(i).data, typedValue, true); c[i] = typedValue.data; } else { c[i] = ca.getColor(i, 0); } } ca.recycle();
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); }
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; }
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); }
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; }
/** * Retrieve the raw TypedValue for the attribute at <var>index</var> and return a temporary object * holding its data. This object is only valid until the next call on to {@link TypedArray}. */ @Nullable public static TypedValue peekNamedValue( TypedArray a, XmlPullParser parser, String attrName, @StyleableRes int resId) { return hasAttribute(parser, attrName) ? a.peekValue(resId) : null; }
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 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; }