下面列出了android.content.res.ColorStateList#valueOf() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void setTint(@NonNull Button button, @ColorInt int color) {
ColorStateList s1 = ColorStateList.valueOf(color);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
button.setCompoundDrawableTintList(s1);
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
Drawable drawable = DrawableCompat.wrap(button.getCompoundDrawables()[1]);
button.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
DrawableCompat.setTintList(drawable, s1);
} else {
PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
mode = PorterDuff.Mode.MULTIPLY;
}
if (button.getCompoundDrawables()[1] != null)
button.getCompoundDrawables()[1].setColorFilter(color, mode);
}
}
private void setTint(@ColorInt int color,
boolean skipIndeterminate) {
ColorStateList sl = ColorStateList.valueOf(color);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
progress.setProgressTintList(sl);
progress.setSecondaryProgressTintList(sl);
if (!skipIndeterminate)
progress.setIndeterminateTintList(sl);
} else {
PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
mode = PorterDuff.Mode.MULTIPLY;
}
if (!skipIndeterminate && progress.getIndeterminateDrawable() != null)
progress.getIndeterminateDrawable().setColorFilter(color, mode);
if (progress.getProgressDrawable() != null)
progress.getProgressDrawable().setColorFilter(color, mode);
}
}
/**
* 按下触摸效果
*/
public static Drawable getPressDrawable(Drawable defaultDrawable, Drawable effectDrawable,
@ColorInt int rippleColor, Drawable contentDrawable, Drawable maskDrawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return new RippleDrawable(ColorStateList.valueOf(rippleColor),
contentDrawable,
maskDrawable);
} else {
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, effectDrawable);
stateListDrawable.addState(new int[]{}, defaultDrawable);
return stateListDrawable;
}
}
public void setBorderColor(ColorStateList colors) {
if (borderColor.equals(colors)) { return; }
borderColor =
(colors != null) ? colors : ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
updateDrawableAttrs();
updateBackgroundDrawableAttrs(false);
if (borderWidth > 0) {
invalidate();
}
}
/**
* Returns a {@link ColorStateList} that is safe to pass to {@link
* android.graphics.drawable.RippleDrawable}.
*
* <p>If given a null ColorStateList, this will return a new transparent ColorStateList since
* RippleDrawable requires a non-null ColorStateList.
*
* <p>If given a non-null ColorStateList, this method will log a warning for API 22-27 if the
* ColorStateList is transparent in the default state and non-transparent in the pressed state.
* This will result in using the pressed state color for the ripple until the finger is lifted at
* which point the ripple will transition to the default state color (transparent), making the
* ripple appear to terminate prematurely.
*/
@NonNull
public static ColorStateList sanitizeRippleDrawableColor(@Nullable ColorStateList rippleColor) {
if (rippleColor != null) {
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP_MR1
&& VERSION.SDK_INT <= VERSION_CODES.O_MR1
&& Color.alpha(rippleColor.getDefaultColor()) == 0
&& Color.alpha(rippleColor.getColorForState(ENABLED_PRESSED_STATE_SET, Color.TRANSPARENT))
!= 0) {
Log.w(LOG_TAG, TRANSPARENT_DEFAULT_COLOR_WARNING);
}
return rippleColor;
}
return ColorStateList.valueOf(Color.TRANSPARENT);
}
public ActiveItemImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.cornerRadius = 0.0f;
this.borderWidth = 0.0f;
this.borderColor = ColorStateList.valueOf(-16777216);
this.isOval = false;
this.mutateBackground = false;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView, defStyle, 0);
int index = a.getInt(R.styleable.RoundedImageView_android_scaleType, -1);
if (index >= 0) {
setScaleType(SCALE_TYPES[index]);
} else {
setScaleType(ScaleType.FIT_CENTER);
}
this.cornerRadius = (float) a.getDimensionPixelSize(R.styleable.RoundedImageView_riv_corner_radius, -1);
this.borderWidth = (float) a.getDimensionPixelSize(R.styleable.RoundedImageView_riv_border_width, -1);
if (this.cornerRadius < 0.0f) {
this.cornerRadius = 0.0f;
}
if (this.borderWidth < 0.0f) {
this.borderWidth = 0.0f;
}
this.borderColor = a.getColorStateList(R.styleable.RoundedImageView_riv_border_color);
if (this.borderColor == null) {
this.borderColor = ColorStateList.valueOf(-16777216);
}
this.mutateBackground = a.getBoolean(R.styleable.RoundedImageView_riv_mutate_background, false);
this.isOval = a.getBoolean(R.styleable.RoundedImageView_riv_oval, false);
updateDrawableAttrs();
updateBackgroundDrawableAttrs(true);
a.recycle();
}
public void setBorderColor(ColorStateList colors) {
if (mBorderColor.equals(colors)) { return; }
mBorderColor =
(colors != null) ? colors : ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
updateDrawableAttrs();
updateBackgroundDrawableAttrs(false);
if (mBorderWidth > 0) {
invalidate();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Drawable createButtonRippleBg(Context context,
int colorButtonNormal,
int colorControlHighlight) {
return new RippleDrawable(ColorStateList.valueOf(colorControlHighlight),
null, createButtonShape(context, colorButtonNormal));
}
/**
* 初始化。
*
* @param context 上下文。
* @param attributeSet 属性。
*/
private void initialize(Context context, AttributeSet attributeSet) {
mPaint.setAntiAlias(true);
TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CircleProgressbar);
if (typedArray.hasValue(R.styleable.CircleProgressbar_circle_color)){
inCircleColors = typedArray.getColorStateList(R.styleable.CircleProgressbar_circle_color);
} else{
inCircleColors = ColorStateList.valueOf(Color.TRANSPARENT);
}
circleColor = inCircleColors.getColorForState(getDrawableState(), Color.TRANSPARENT);
typedArray.recycle();
}
public SmartTabLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// Disable the Scroll Bar
setHorizontalScrollBarEnabled(false);
final DisplayMetrics dm = getResources().getDisplayMetrics();
final float density = dm.density;
int tabBackgroundResId = NO_ID;
boolean textAllCaps = TAB_VIEW_TEXT_ALL_CAPS;
ColorStateList textColors;
float textSize = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP, dm);
int textHorizontalPadding = (int) (TAB_VIEW_PADDING_DIPS * density);
int textMinWidth = (int) (TAB_VIEW_TEXT_MIN_WIDTH * density);
boolean distributeEvenly = DEFAULT_DISTRIBUTE_EVENLY;
int customTabLayoutId = NO_ID;
int customTabTextViewId = NO_ID;
boolean clickable = TAB_CLICKABLE;
int titleOffset = (int) (TITLE_OFFSET_DIPS * density);
TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.stl_SmartTabLayout, defStyle, 0);
tabBackgroundResId = a.getResourceId(
R.styleable.stl_SmartTabLayout_stl_defaultTabBackground, tabBackgroundResId);
textAllCaps = a.getBoolean(
R.styleable.stl_SmartTabLayout_stl_defaultTabTextAllCaps, textAllCaps);
textColors = a.getColorStateList(
R.styleable.stl_SmartTabLayout_stl_defaultTabTextColor);
textSize = a.getDimension(
R.styleable.stl_SmartTabLayout_stl_defaultTabTextSize, textSize);
textHorizontalPadding = a.getDimensionPixelSize(
R.styleable.stl_SmartTabLayout_stl_defaultTabTextHorizontalPadding, textHorizontalPadding);
textMinWidth = a.getDimensionPixelSize(
R.styleable.stl_SmartTabLayout_stl_defaultTabTextMinWidth, textMinWidth);
customTabLayoutId = a.getResourceId(
R.styleable.stl_SmartTabLayout_stl_customTabTextLayoutId, customTabLayoutId);
customTabTextViewId = a.getResourceId(
R.styleable.stl_SmartTabLayout_stl_customTabTextViewId, customTabTextViewId);
distributeEvenly = a.getBoolean(
R.styleable.stl_SmartTabLayout_stl_distributeEvenly, distributeEvenly);
clickable = a.getBoolean(
R.styleable.stl_SmartTabLayout_stl_clickable, clickable);
titleOffset = a.getLayoutDimension(
R.styleable.stl_SmartTabLayout_stl_titleOffset, titleOffset);
a.recycle();
this.titleOffset = titleOffset;
this.tabViewBackgroundResId = tabBackgroundResId;
this.tabViewTextAllCaps = textAllCaps;
this.tabViewTextColors = (textColors != null)
? textColors
: ColorStateList.valueOf(TAB_VIEW_TEXT_COLOR);
this.tabViewTextSize = textSize;
this.tabViewTextHorizontalPadding = textHorizontalPadding;
this.tabViewTextMinWidth = textMinWidth;
this.internalTabClickListener = clickable ? new InternalTabClickListener() : null;
this.distributeEvenly = distributeEvenly;
if (customTabLayoutId != NO_ID) {
setCustomTabView(customTabLayoutId, customTabTextViewId);
}
this.tabStrip = new SmartTabStrip(context, attrs);
if (distributeEvenly && tabStrip.isIndicatorAlwaysInCenter()) {
throw new UnsupportedOperationException(
"'distributeEvenly' and 'indicatorAlwaysInCenter' both use does not support");
}
// Make sure that the Tab Strips fills this View
setFillViewport(!tabStrip.isIndicatorAlwaysInCenter());
addView(tabStrip, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
/**
* Same function as {@link #setHintTextColor(int)}. (The built-in one is a final method that can't be overridden, so use this method instead.)
*/
public void setMetHintTextColor(int color) {
textColorHintStateList = ColorStateList.valueOf(color);
resetHintTextColor();
}
/**
* Set color for the keypad text
*
* @param color - Resource id of the color to be set
*/
public void setKeyTextColor(int color) {
ColorStateList colorStateList = ColorStateList.valueOf(color);
textPaint.setColor(colorStateList.getColorForState(getDrawableState(), 0));
invalidate();
}
@Override
public void setElevationShadowColor(int color) {
ambientShadowColor = spotShadowColor = ColorStateList.valueOf(color);
setElevation(elevation);
setTranslationZ(translationZ);
}
public TitleBarView setActionTextColor(int mActionTextColor) {
this.mActionTextColor = ColorStateList.valueOf(mActionTextColor);
return this;
}
@Override
public void setElevationShadowColor(int color) {
ambientShadowColor = spotShadowColor = ColorStateList.valueOf(color);
setElevation(elevation);
setTranslationZ(translationZ);
}
/**
* 设置右文案颜色
*/
public EasyTextView textRightColor(int color) {
this.mRightColor = ColorStateList.valueOf(color);
return this;
}
public RoundedImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView, defStyle, 0);
int index = a.getInt(R.styleable.RoundedImageView_android_scaleType, -1);
if (index >= 0)
{
setScaleType(SCALE_TYPES[index]);
}
else
{
// default scaletype to FIT_CENTER
setScaleType(ScaleType.FIT_CENTER);
}
cornerRadius = a.getDimensionPixelSize(R.styleable.RoundedImageView_corner_radius, -1);
borderWidth = a.getDimensionPixelSize(R.styleable.RoundedImageView_border_width, -1);
// don't allow negative values for radius and border
if (cornerRadius < 0)
{
cornerRadius = DEFAULT_RADIUS;
}
if (borderWidth < 0)
{
borderWidth = DEFAULT_BORDER_WIDTH;
}
borderColor = a.getColorStateList(R.styleable.RoundedImageView_border_color);
if (borderColor == null)
{
borderColor = ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
}
mutateBackground = a.getBoolean(R.styleable.RoundedImageView_mutate_background, false);
isOval = a.getBoolean(R.styleable.RoundedImageView_oval, false);
updateDrawableAttrs();
updateBackgroundDrawableAttrs(true);
a.recycle();
}
public ClipDrawable(Drawable drawable, ClipOutlineProvider provider, float strokeWidth,
int strokeColor) {
this(drawable, provider, strokeWidth, ColorStateList.valueOf(strokeColor));
}
/**
* Set delete icon color
*
* @param color the color to set
*/
public void setDeleteIconColor(@ColorInt int color) {
mDeleteIconColor = ColorStateList.valueOf(color);
mDeletable = true;
inflateWithAttributes();
}
/**
* Set the border color.
*
* @param color the color to set.
* @return the builder for chaining.
*/
public RoundedTransformationBuilder borderColor(int color) {
mBorderColor = ColorStateList.valueOf(color);
return this;
}