package org.aisen.wen.support.utils.md; import android.content.Context; import android.content.res.ColorStateList; import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.os.Build; import android.support.annotation.AttrRes; import android.support.v4.content.ContextCompat; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.RadioButton; import org.aisen.wen.R; /** * Created by wangdan on 16/1/11. */ public class MDHelper { public static void setRadioButtonTint(RadioButton radioButton, int color) { ColorStateList sl = new ColorStateList(new int[][]{ new int[]{-android.R.attr.state_checked}, new int[]{android.R.attr.state_checked} }, new int[]{ resolveColor(radioButton.getContext(), android.R.attr.textColorSecondary), color }); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { radioButton.setButtonTintList(sl); } else { Drawable drawable = ContextCompat.getDrawable(radioButton.getContext(), R.drawable.abc_btn_radio_material); DrawableWrapper d = new DrawableWrapper(drawable); d.setTintList(sl); radioButton.setButtonDrawable(d); } } public static void setProgressBarTint(ProgressBar progressBar, int color) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { ColorStateList stateList = ColorStateList.valueOf(color); progressBar.setProgressTintList(stateList); progressBar.setSecondaryProgressTintList(stateList); progressBar.setIndeterminateTintList(stateList); } else { PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN; if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) { mode = PorterDuff.Mode.MULTIPLY; } if (progressBar.getIndeterminateDrawable() != null) progressBar.getIndeterminateDrawable().setColorFilter(color, mode); if (progressBar.getProgressDrawable() != null) progressBar.getProgressDrawable().setColorFilter(color, mode); } } public static void setEditTextTint(EditText editText, int color) { ColorStateList s1 = ColorStateList.valueOf(color); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { editText.setBackgroundTintList(s1); } else { Drawable drawable = ContextCompat.getDrawable(editText.getContext(), R.drawable.abc_edit_text_material); DrawableWrapper d = new DrawableWrapper(drawable); d.setTintList(s1); setBackgroundCompat(editText, d); } } public static void setCheckBoxTint(CheckBox box, int color) { ColorStateList sl = new ColorStateList(new int[][]{ new int[]{-android.R.attr.state_checked}, new int[]{android.R.attr.state_checked} }, new int[]{ resolveColor(box.getContext(), android.R.attr.textColorSecondary), color }); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { box.setButtonTintList(sl); } else { Drawable drawable = ContextCompat.getDrawable(box.getContext(), R.drawable.abc_btn_check_material); DrawableWrapper d = new DrawableWrapper(drawable); d.setTintList(sl); box.setButtonDrawable(d); } } public static int resolveColor(Context context, @AttrRes int attr) { return resolveColor(context, attr, 0); } public static int resolveColor(Context context, @AttrRes int attr, int fallback) { TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr}); try { return a.getColor(0, fallback); } finally { a.recycle(); } } public static void setBackgroundCompat(View view, Drawable d) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { //noinspection deprecation view.setBackgroundDrawable(d); } else { view.setBackground(d); } } public static int adjustAlpha(int color, @SuppressWarnings("SameParameterValue") float factor) { int alpha = Math.round(Color.alpha(color) * factor); int red = Color.red(color); int green = Color.green(color); int blue = Color.blue(color); return Color.argb(alpha, red, green, blue); } public static Drawable resolveDrawable(Context context, @AttrRes int attr) { return resolveDrawable(context, attr, null); } private static Drawable resolveDrawable(Context context, @AttrRes int attr, @SuppressWarnings("SameParameterValue") Drawable fallback) { TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr}); try { Drawable d = a.getDrawable(0); if (d == null && fallback != null) d = fallback; return d; } finally { a.recycle(); } } public static int resolveDimension(Context context, @AttrRes int attr) { return resolveDimension(context, attr, -1); } private static int resolveDimension(Context context, @AttrRes int attr, int fallback) { TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr}); try { return a.getDimensionPixelSize(0, fallback); } finally { a.recycle(); } } public static boolean resolveBoolean(Context context, @AttrRes int attr, boolean fallback) { TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr}); try { return a.getBoolean(0, fallback); } finally { a.recycle(); } } public static boolean resolveBoolean(Context context, @AttrRes int attr) { return resolveBoolean(context, attr, false); } }