下面列出了怎么用android.support.annotation.StyleableRes的API类实例代码及写法,或者点击链接到github查看源代码。
private void setSelectorDrawable(StateListDrawable stateListDrawable, @StyleableRes int solidAttr, @StyleableRes int strokeAttr, @AttrRes int functionId) throws Exception {
if (typedArray.hasValue(solidAttr) || typedArray.hasValue(strokeAttr)) {
GradientDrawable tmpDrawable = DrawableFactory.getDrawable(typedArray);
if (typedArray.hasValue(solidAttr)) {
tmpDrawable.setColor(typedArray.getColor(solidAttr, 0));
}
if (typedArray.hasValue(strokeAttr)) {
int strokeWidth = typedArray.getDimensionPixelSize(R.styleable.background_bl_stroke_width, 0);
int strokeColor = typedArray.getColor(strokeAttr, 0);
float strokeDashWidth = typedArray.getDimension(R.styleable.background_bl_stroke_dashWidth, 0f);
float strokeGap = typedArray.getDimension(R.styleable.background_bl_stroke_dashGap, 0f);
tmpDrawable.setStroke(strokeWidth, strokeColor, strokeDashWidth, strokeGap);
}
stateListDrawable.addState(new int[]{functionId}, tmpDrawable);
}
}
@ColorInt
private static int[] resolveThemeColors(@NonNull Context context, @AttrRes @StyleableRes int[] attrs, @ColorInt int[] defaultColors) {
if (attrs.length != defaultColors.length)
throw new IllegalArgumentException("Argument attrs must be the same size as defaultColors");
TypedValue typedValue = new TypedValue();
TypedArray a = context.obtainStyledAttributes(typedValue.data, attrs);
for (int i = 0; i < attrs.length; i++) {
defaultColors[i] = a.getColor(0, defaultColors[i]);
}
a.recycle();
return defaultColors;
}
public static void initCustomFont(TextView view, AttributeSet attributeSet,
@StyleableRes int[] attrs, int attrIndex) {
TypedArray typedArray = view.getContext().getTheme()
.obtainStyledAttributes(attributeSet, attrs, 0, 0);
try {
int fontIndex = typedArray.getInt(attrIndex, DEFAULT_FONT);
String fontPath = FONTS.get(fontIndex);
if (fontPath != null) {
view.setTypeface(Typeface.createFromAsset(view.getContext().getAssets(), fontPath));
} else {
throw new IllegalArgumentException("invalid font path");
}
} finally {
typedArray.recycle();
}
}
private void addFrame(@StyleableRes int itemDrawableId, @StyleableRes int itemDurationId){
if(animationTa.hasValue(itemDrawableId)){
Drawable itemDrawable = animationTa.getDrawable(itemDrawableId);
if(itemDrawable != null){
if(animationTa.hasValue(itemDurationId)){
drawable.addFrame(itemDrawable, animationTa.getInt(itemDurationId, 0));
}else {
drawable.addFrame(itemDrawable, duration);
}
}
}
}
public void onGlobalLayoutChange(@StyleableRes int headerRes, @StyleableRes int footerRes){
int headerId = mTypedArrayResourceProvider.getResourceId(headerRes);
if(headerId != 0) {
mStickyScrollPresentation.initHeaderView(headerId);
}
int footerId = mTypedArrayResourceProvider.getResourceId(footerRes);
if(footerId != 0){
mStickyScrollPresentation.initFooterView(footerId);
}
mTypedArrayResourceProvider.recycle();
}
public static Drawable getDrawable(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex) {
Drawable val = a.getDrawable(index);
if (val == null) {
return a.getDrawable(fallbackIndex);
}
return val;
}
public static String getString(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex) {
String val = a.getString(index);
if (val == null) {
return a.getString(fallbackIndex);
}
return val;
}
public static CharSequence[] getTextArray(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex) {
CharSequence[] val = a.getTextArray(index);
if (val == null) {
return a.getTextArray(fallbackIndex);
}
return val;
}
@NonNull
private static int[] resolveColorAttributesFromTheme(Context context, @StyleableRes int[] attrs) {
final TypedArray ta = context.obtainStyledAttributes(attrs);
final int[] colors = new int[attrs.length];
for (int idxAttr = 0; idxAttr < colors.length; idxAttr++) {
colors[idxAttr] = ta.getColor(idxAttr, 0);
}
ta.recycle();
return colors;
}
@Px
static int getDimension(Context context,
TypedArray array,
@StyleableRes int attr,
@DimenRes int defaultDimenRes) {
return array.getDimensionPixelOffset(
attr, context.getResources().getDimensionPixelSize(defaultDimenRes));
}
@ColorInt
@SuppressWarnings("deprecation")
static int getColor(Context context,
TypedArray array,
@StyleableRes int attr,
@ColorRes int defaultColorRes) {
if (Build.VERSION.SDK_INT >= 23) {
return array.getColor(attr, context.getColor(defaultColorRes));
} else {
return array.getColor(attr, context.getResources().getColor(defaultColorRes));
}
}
@ColorInt
static int[] getColors(Context context,
TypedArray array,
@StyleableRes int attr,
@ArrayRes int defaultColorsRes) {
return context.getResources().getIntArray(
array.getResourceId(attr, defaultColorsRes));
}
@DrawableRes
@Nullable
static int[] getDrawableResources(Context context,
TypedArray array,
@StyleableRes int attr) {
int resourceId = array.getResourceId(attr, -1);
return resourceId == -1 ? null : context.getResources().getIntArray(resourceId);
}
static int getInteger(Context context,
TypedArray array,
@StyleableRes int attr,
@IntegerRes int defaultIntRes) {
return array.getInteger(
attr, context.getResources().getInteger(defaultIntRes));
}
@NonNull
private static int[] resolveColorAttributesFromTheme(Context context, @StyleableRes int[] attrs) {
final TypedArray ta = context.obtainStyledAttributes(attrs);
final int[] colors = new int[attrs.length];
for (int idxAttr = 0; idxAttr < colors.length; idxAttr++) {
colors[idxAttr] = ta.getColor(idxAttr, 0);
}
ta.recycle();
return colors;
}
private int getDimension(TypedArray a, @StyleableRes int styleableId,
@DimenRes int defaultDimension) {
int result = a.getDimensionPixelSize(styleableId, NOT_PRESENT);
if (result == NOT_PRESENT) {
result = getResources().getDimensionPixelSize(defaultDimension);
}
return result;
}
@NonNull
@Override
public TypedArray obtainStyledAttributes(@StyleRes int resId, @StyleableRes int[] attrs)
{
return mActivity.obtainStyledAttributes(resId, attrs);
}
public static TypedArray obtainStyledAttributes(@StyleableRes int[] attrs) {
return Base.getTheme().obtainStyledAttributes(attrs);
}
public static TypedArray obtainStyledAttributes(@StyleRes int resid, @StyleableRes int[] attrs) {
return Base.getTheme().obtainStyledAttributes(resid, attrs);
}
public static TypedArray obtainStyledAttributes(AttributeSet set, @StyleableRes int[] attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
return Base.getTheme().obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes);
}
public static TypedArray obtainStyledAttributes(@StyleableRes int[] attrs) {
return Base.getContext().obtainStyledAttributes(attrs);
}
public static TypedArray obtainStyledAttributes(AttributeSet set, @StyleableRes int[] attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
return Base.getContext().obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes);
}
public static TypedArray obtainStyledAttributes(AttributeSet set, @StyleableRes int[] attrs) {
return Base.getContext().obtainStyledAttributes(set, attrs);
}
public static TypedArray obtainStyledAttributes(@StyleRes int resid, @StyleableRes int[] attrs) {
return Base.getContext().obtainStyledAttributes(resid, attrs);
}
public ResourceProvider(Context context, AttributeSet attrs, @StyleableRes int[] styleRes) {
mTypeArray = context.obtainStyledAttributes(attrs, styleRes);
}
@Override
public int getResourceId(@StyleableRes int styleResId) {
return mTypeArray.getResourceId(styleResId, 0);
}
public static boolean getBoolean(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, boolean defaultValue) {
return a.getBoolean(index, a.getBoolean(fallbackIndex, defaultValue));
}
public static int getInt(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, int defaultValue) {
return a.getInt(index, a.getInt(fallbackIndex, defaultValue));
}
@AnyRes
public static int getResourceId(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, @AnyRes int defaultValue) {
return a.getResourceId(index, a.getResourceId(fallbackIndex, defaultValue));
}
@SuppressLint("RestrictedApi")
private static int[] getIntArray(TypedArray a, @StyleableRes int index,
@StyleableRes int fallbackIndex) {
int resourceId = TypedArrayUtils.getResourceId(a, index, fallbackIndex, 0);
return a.getResources().getIntArray(resourceId);
}