AttrsUtils

public class AttrsUtils {

/**
 * get attrs size
 *
 * @param context
 * @param attr
 * @return
 */
public static float getTypeValueSize(Context context, int attr) {
    float textSize = 0;
    try {
        TypedValue typedValue = new TypedValue();
        int[] attribute = new int[]{attr};
        TypedArray array = context.obtainStyledAttributes(typedValue.resourceId, attribute);
        textSize = array.getDimensionPixelSize(0, 0);
        array.recycle();
        return textSize;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return textSize;
}

/**
 * get attrs size
 *
 * @param context
 * @param attr
 * @return
 */
public static int getTypeValueSizeForInt(Context context, int attr) {
    int textSize = 0;
    try {
        TypedValue typedValue = new TypedValue();
        int[] attribute = new int[]{attr};
        TypedArray array = context.obtainStyledAttributes(typedValue.resourceId, attribute);
        textSize = array.getDimensionPixelSize(0, 0);
        array.recycle();
        return textSize;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return textSize;
}

/**
 * get attrs color
 *
 * @param context
 * @param attr
 * @return
 */
public static int getTypeValueColor(Context context, int attr) {
    int color = 0;
    try {
        TypedValue typedValue = new TypedValue();
        int[] attribute = new int[]{attr};
        TypedArray array = context.obtainStyledAttributes(typedValue.resourceId, attribute);
        color = array.getColor(0, 0);
        array.recycle();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return color;
}

/**
 * get attrs color
 *
 * @param context
 * @param attr
 * @return
 */
public static ColorStateList getTypeValueColorStateList(Context context, int attr) {
    ColorStateList colorStateList = null;
    try {
        TypedValue typedValue = new TypedValue();
        int[] attribute = new int[]{attr};
        TypedArray array = context.obtainStyledAttributes(typedValue.resourceId, attribute);
        colorStateList = array.getColorStateList(0);
        array.recycle();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return colorStateList;
}

/**
 * attrs status color or black
 *
 * @param context
 * @param attr
 * @return
 */
public static boolean getTypeValueBoolean(Context context, int attr) {
    boolean flag = false;
    try {
        TypedValue typedValue = new TypedValue();
        int[] attribute = new int[]{attr};
        TypedArray array = context.obtainStyledAttributes(typedValue.resourceId, attribute);
        flag = array.getBoolean(0, false);
        array.recycle();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return flag;
}

/**
 * attrs drawable
 *
 * @param context
 * @param attr
 * @param defaultResId
 * @return
 */
public static Drawable getTypeValueDrawable(Context context, int attr, int defaultResId) {
    Drawable drawable = null;
    try {
        TypedValue typedValue = new TypedValue();
        int[] attribute = new int[]{attr};
        TypedArray array = context.obtainStyledAttributes(typedValue.resourceId, attribute);
        drawable = array.getDrawable(0);
        array.recycle();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return drawable == null ? ContextCompat.getDrawable(context, defaultResId) : drawable;
}

/**
 * getColorStateList
 *
 * @param colors
 * @return
 */
public static ColorStateList getColorStateList(int[] colors) {
    try {
        if (colors.length == 2) {
            int[][] states = new int[2][];
            states[0] = new int[]{-android.R.attr.state_selected};
            states[1] = new int[]{android.R.attr.state_selected};
            return new ColorStateList(states, colors);
        } else {
            return ColorStateList.valueOf(colors[0]);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容