下面列出了android.content.res.ColorStateList#isStateful() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Set the box's stroke color state list.
*
* @param boxStrokeColorStateList the color state list to use for the box's stroke
*/
public void setBoxStrokeColorStateList(@NonNull ColorStateList boxStrokeColorStateList) {
if (boxStrokeColorStateList.isStateful()) {
defaultStrokeColor = boxStrokeColorStateList.getDefaultColor();
disabledColor =
boxStrokeColorStateList.getColorForState(new int[] {-android.R.attr.state_enabled}, -1);
hoveredStrokeColor =
boxStrokeColorStateList.getColorForState(
new int[] {android.R.attr.state_hovered, android.R.attr.state_enabled}, -1);
focusedStrokeColor =
boxStrokeColorStateList.getColorForState(
new int[] {android.R.attr.state_focused, android.R.attr.state_enabled}, -1);
} else if (focusedStrokeColor != boxStrokeColorStateList.getDefaultColor()) {
// If attribute boxStrokeColor is not a color state list but only a single value, its value
// will be applied to the box's focus state.
focusedStrokeColor = boxStrokeColorStateList.getDefaultColor();
}
updateTextInputBoxState();
}
public static Tint withAlpha(@NonNull final Tint tint, final int alpha) {
return new Tint() {
@Override
public ColorStateList getColor(Context context) {
int appliedAlpha = alpha == ALPHA_DISABLED ? getThemeDisabledAlpha(context) : alpha;
ColorStateList stateColor = tint.getColor(context);
if (stateColor.isStateful()) {
// New ColorStateList object will be created each time calling .withAlpha()
return stateColor.withAlpha(appliedAlpha);
} else {
// Created ColorStateList object will be cached
int color = (stateColor.getDefaultColor() & 0xFFFFFF) | (appliedAlpha << 24);
return ColorStateList.valueOf(color);
}
}
};
}
public static ColorStateList getThemeColorStateList(Context context, ColorStateList origin) {
if (origin == null) return null;
if (origin.isStateful()) {
TintInfo tintInfo = parseColorStateList(origin);
if (tintInfo == null || tintInfo.isInvalid()) {
return origin;
}
int[] newColors;
int[][] newStates;
int index = 0;
boolean hasDisableColor = StateSet.stateSetMatches(tintInfo.mTintStates[0], DISABLED_STATE_SET);
if (!hasDisableColor) {
newStates = new int[tintInfo.mTintStates.length + 1][];
newColors = new int[tintInfo.mTintStates.length + 1];
newStates[index] = DISABLED_STATE_SET;
newColors[index] = getDisabledThemeAttrColor(context, R.attr.themeColorSecondary);
index++;
} else {
newStates = new int[tintInfo.mTintStates.length][];
newColors = new int[tintInfo.mTintStates.length];
}
for (int i = 0; i < tintInfo.mTintStates.length; i++) {
newStates[index] = tintInfo.mTintStates[i];
newColors[index] = replaceColor(context, tintInfo.mTintColors[i]);
index++;
}
return new ColorStateList(newStates, newColors);
}
return ColorStateList.valueOf(replaceColor(context, origin.getDefaultColor()));
}
public RoundButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundButton);
float pressedRatio = a.getFloat(R.styleable.RoundButton_btnPressedRatio, 0.80f);
int cornerRadius = a.getLayoutDimension(R.styleable.RoundButton_btnCornerRadius, 0);
ColorStateList solidColor = a.getColorStateList(R.styleable.RoundButton_btnSolidColor);
int strokeColor = a.getColor(R.styleable.RoundButton_btnStrokeColor, 0x0);
int strokeWidth = a.getDimensionPixelSize(R.styleable.RoundButton_btnStrokeWidth, 0);
int strokeDashWidth = a.getDimensionPixelSize(R.styleable.RoundButton_btnStrokeDashWidth, 0);
int strokeDashGap = a.getDimensionPixelSize(R.styleable.RoundButton_btnStrokeDashGap, 0);
a.recycle();
setSingleLine(true);
setGravity(Gravity.CENTER);
RoundDrawable rd = new RoundDrawable(cornerRadius == -1);
rd.setCornerRadius(cornerRadius == -1 ? 0 : cornerRadius);
rd.setStroke(strokeWidth, strokeColor, strokeDashWidth, strokeDashGap);
if (solidColor == null) {
solidColor = ColorStateList.valueOf(0);
}
if (solidColor.isStateful()) {
rd.setSolidColors(solidColor);
} else if (pressedRatio > 0.0001f) {
rd.setSolidColors(csl(solidColor.getDefaultColor(), pressedRatio));
} else {
rd.setColor(solidColor.getDefaultColor());
}
setBackground(rd);
}
public static ColorStateList getThemeColorStateList(Context context, ColorStateList origin) {
if (origin == null) return null;
if (origin.isStateful()) {
TintInfo tintInfo = parseColorStateList(origin);
if (tintInfo == null || tintInfo.isInvalid()) {
return origin;
}
int[] newColors;
int[][] newStates;
int index = 0;
boolean hasDisableColor = StateSet.stateSetMatches(tintInfo.mTintStates[0], DISABLED_STATE_SET);
if (!hasDisableColor) {
newStates = new int[tintInfo.mTintStates.length + 1][];
newColors = new int[tintInfo.mTintStates.length + 1];
newStates[index] = DISABLED_STATE_SET;
newColors[index] = getDisabledThemeAttrColor(context, R.attr.themeColorSecondary);
index++;
} else {
newStates = new int[tintInfo.mTintStates.length][];
newColors = new int[tintInfo.mTintStates.length];
}
for (int i = 0; i < tintInfo.mTintStates.length; i++) {
newStates[index] = tintInfo.mTintStates[i];
newColors[index] = replaceColor(context, tintInfo.mTintColors[i]);
index++;
}
return new ColorStateList(newStates, newColors);
}
return ColorStateList.valueOf(replaceColor(context, origin.getDefaultColor()));
}
private void refreshIconDrawableState(
CheckableImageButton iconView, ColorStateList colorStateList) {
Drawable icon = iconView.getDrawable();
if (iconView.getDrawable() == null || colorStateList == null || !colorStateList.isStateful()) {
return;
}
int color =
colorStateList.getColorForState(mergeIconState(iconView), colorStateList.getDefaultColor());
icon = DrawableCompat.wrap(icon).mutate();
DrawableCompat.setTintList(icon, ColorStateList.valueOf(color));
iconView.setImageDrawable(icon);
}
@Override
public void setTextColor(ColorStateList colors) {
super.setTextColor(colors);
if (!colors.isStateful()) {
mTextColor = colors.getDefaultColor();
}
else {
mTextColor = colors.getColorForState(getDrawableState(), colors.getDefaultColor());
}
initTextPaint();
invalidate();
}
private ColorStateList createSwitchThumbColorStateList(Context context) {
final int[][] states = new int[3][];
final int[] colors = new int[3];
int i = 0;
final ColorStateList thumbColor = SkinCompatThemeUtils.getThemeAttrColorStateList(context,
R.attr.colorSwitchThumbNormal);
if (thumbColor != null && thumbColor.isStateful()) {
// If colorSwitchThumbNormal is a valid ColorStateList, extract the default and
// disabled colors from it
// Disabled state
states[i] = SkinCompatThemeUtils.DISABLED_STATE_SET;
colors[i] = thumbColor.getColorForState(states[i], 0);
i++;
states[i] = SkinCompatThemeUtils.CHECKED_STATE_SET;
colors[i] = SkinCompatThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated);
i++;
// Default enabled state
states[i] = SkinCompatThemeUtils.EMPTY_STATE_SET;
colors[i] = thumbColor.getDefaultColor();
i++;
} else {
// Else we'll use an approximation using the default disabled alpha
// Disabled state
states[i] = SkinCompatThemeUtils.DISABLED_STATE_SET;
colors[i] = SkinCompatThemeUtils.getDisabledThemeAttrColor(context, R.attr.colorSwitchThumbNormal);
i++;
states[i] = SkinCompatThemeUtils.CHECKED_STATE_SET;
colors[i] = SkinCompatThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated);
i++;
// Default enabled state
states[i] = SkinCompatThemeUtils.EMPTY_STATE_SET;
colors[i] = SkinCompatThemeUtils.getThemeAttrColor(context, R.attr.colorSwitchThumbNormal);
i++;
}
return new ColorStateList(states, colors);
}
private ColorStateList createSwitchThumbColorStateList(Context context) {
final int[][] states = new int[3][];
final int[] colors = new int[3];
int i = 0;
final ColorStateList thumbColor = SkinCompatThemeUtils.getThemeAttrColorStateList(context,
R.attr.colorSwitchThumbNormal);
if (thumbColor != null && thumbColor.isStateful()) {
// If colorSwitchThumbNormal is a valid ColorStateList, extract the default and
// disabled colors from it
// Disabled state
states[i] = SkinCompatThemeUtils.DISABLED_STATE_SET;
colors[i] = thumbColor.getColorForState(states[i], 0);
i++;
states[i] = SkinCompatThemeUtils.CHECKED_STATE_SET;
colors[i] = SkinCompatThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated);
i++;
// Default enabled state
states[i] = SkinCompatThemeUtils.EMPTY_STATE_SET;
colors[i] = thumbColor.getDefaultColor();
i++;
} else {
// Else we'll use an approximation using the default disabled alpha
// Disabled state
states[i] = SkinCompatThemeUtils.DISABLED_STATE_SET;
colors[i] = SkinCompatThemeUtils.getDisabledThemeAttrColor(context, R.attr.colorSwitchThumbNormal);
i++;
states[i] = SkinCompatThemeUtils.CHECKED_STATE_SET;
colors[i] = SkinCompatThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated);
i++;
// Default enabled state
states[i] = SkinCompatThemeUtils.EMPTY_STATE_SET;
colors[i] = SkinCompatThemeUtils.getThemeAttrColor(context, R.attr.colorSwitchThumbNormal);
i++;
}
return new ColorStateList(states, colors);
}
private static boolean isStateful(@Nullable ColorStateList colorStateList) {
return colorStateList != null && colorStateList.isStateful();
}
public boolean isStateful() {
ColorStateList tintList = this.isCompatTintEnabled() && this.mState != null ? this.mState.mTint : null;
return tintList != null && tintList.isStateful() || this.mDrawable.isStateful();
}