下面列出了android.content.res.ColorStateList#getDefaultColor() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private ColorStateList createDefaultColorStateList(int baseColorThemeAttr) {
final TypedValue value = new TypedValue();
if (!getContext().getTheme().resolveAttribute(baseColorThemeAttr, value, true)) {
return null;
}
ColorStateList baseColor = SkinCompatResources.getColorStateList(getContext(), value.resourceId);
int colorPrimary = SkinCompatResources.getColor(getContext(), mDefaultTintResId);
int defaultColor = baseColor.getDefaultColor();
return new ColorStateList(new int[][]{
DISABLED_STATE_SET,
CHECKED_STATE_SET,
EMPTY_STATE_SET
}, new int[]{
baseColor.getColorForState(DISABLED_STATE_SET, defaultColor),
colorPrimary,
defaultColor
});
}
void applyGeneralAttributeSet(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
ColorStateList textColors = mGeneralSelector.getTextColors();
if(textColors != null){
mDefaultColor = textColors.getDefaultColor();
}
mSpeedyColorStateList = new SpeedyColorStateList(mDefaultColor);
if(attrs != null){
TypedArray a = mGeneralSelector.getContext().obtainStyledAttributes(attrs, R.styleable.SpeedySelectorTextColor, defStyleAttr, defStyleRes);
if(a != null){
parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateDefaultTextColor, StateType.STATE_DEFAULT);
parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateFocusedTextColor, StateType.STATE_FOCUSED);
parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateWindowFocusedTextColor, StateType.STATE_WINDOW_FOCUSED);
parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateEnabledTextColor, StateType.STATE_ENABLED);
parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateCheckedTextColor, StateType.STATE_CHECKED);
parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateSelectedTextColor, StateType.STATE_SELECTED);
parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStateActivatedTextColor, StateType.STATE_ACTIVATED);
parserGeneralAttribute(a, R.styleable.SpeedySelectorTextColor_spStatePressedTextColor, StateType.STATE_PRESSED);
parserGeneralAttribute(a,R.styleable.SpeedySelectorTextColor_spStateActiveTextColor,StateType.STATE_ACTIVE);
a.recycle();
}
}
requestSelector();
}
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);
}
}
};
}
@NonNull
private static int[] extractColors(ColorStateList colorStateList, int[][] states) {
int[] colors = new int[states.length];
int idx = 0;
for (int[] stateSet : states) {
// The empty state is peculiar in that getColorForState() will not return
// the default color, but rather any color defined in the ColorStateList
// for any state.
// https://developer.android.com/reference/android/content/res/ColorStateList.html
// "Each item defines a set of state spec and color pairs, where the state
// spec is a series of attributes set to either true or false to represent
// inclusion or exclusion. If an attribute is not specified for an item,
// it may be any value."
// "An item with no state spec is considered to match any set of states
// and is generally useful as a final item to be used as a default."
colors[idx++] = stateSet.length == 0 ? colorStateList.getDefaultColor()
: colorStateList.getColorForState(stateSet, 0);
}
return colors;
}
/**
* 设置DOtView的focus时的颜色
* @param color
*/
public void setBannerDotColor(ColorStateList color){
mIndicatorFocusDrawable = new ColorDrawable(color.getDefaultColor());
if(mCurrentDot != null) {
mCurrentDot.setDrawable(mIndicatorFocusDrawable);
}
}
/**
* The legacy text color might have been poorly defined. Ensures that it
* has an appropriate activated state, using the selected state if one
* exists or modifying the default text color otherwise.
*
* @param color a legacy text color, or {@code null}
* @return a color state list with an appropriate activated state, or
* {@code null} if a valid activated state could not be generated
*/
@Nullable
private ColorStateList applyLegacyColorFixes(@Nullable ColorStateList color) {
if (color == null || color.hasState(R.attr.state_activated)) {
return color;
}
final int activatedColor;
final int defaultColor;
if (color.hasState(R.attr.state_selected)) {
activatedColor = color.getColorForState(StateSet.get(
StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_SELECTED), 0);
defaultColor = color.getColorForState(StateSet.get(
StateSet.VIEW_STATE_ENABLED), 0);
} else {
activatedColor = color.getDefaultColor();
// Generate a non-activated color using the disabled alpha.
final TypedArray ta = mContext.obtainStyledAttributes(ATTRS_DISABLED_ALPHA);
final float disabledAlpha = ta.getFloat(0, 0.30f);
defaultColor = multiplyAlphaComponent(activatedColor, disabledAlpha);
}
if (activatedColor == 0 || defaultColor == 0) {
// We somehow failed to obtain the colors.
return null;
}
final int[][] stateSet = new int[][] {{ R.attr.state_activated }, {}};
final int[] colors = new int[] { activatedColor, defaultColor };
return new ColorStateList(stateSet, colors);
}
public MarkerDrawable(@NonNull ColorStateList tintList, int closedSize) {
super(tintList);
mInterpolator = new AccelerateDecelerateInterpolator();
mClosedStateSize = closedSize;
mStartColor = tintList.getColorForState(new int[]{android.R.attr.state_pressed}, tintList.getDefaultColor());
mEndColor = tintList.getDefaultColor();
}
public MarkerDrawable(@NonNull ColorStateList tintList, int closedSize) {
super(tintList);
mInterpolator = new AccelerateDecelerateInterpolator();
mClosedStateSize = closedSize;
mStartColor =
tintList.getColorForState(
new int[] {android.R.attr.state_enabled, android.R.attr.state_pressed},
tintList.getDefaultColor());
mEndColor = tintList.getDefaultColor();
}
/**
* The legacy text color might have been poorly defined. Ensures that it
* has an appropriate activated state, using the selected state if one
* exists or modifying the default text color otherwise.
*
* @param color a legacy text color, or {@code null}
* @return a color state list with an appropriate activated state, or
* {@code null} if a valid activated state could not be generated
*/
@Nullable
private ColorStateList applyLegacyColorFixes(@Nullable ColorStateList color) {
if (color == null || Utils.colorHasState(color, android.R.attr.state_activated)) { // color.hasState(android.R.attr.state_activated)
return color;
}
final int activatedColor;
final int defaultColor;
if (Utils.colorHasState(color, android.R.attr.state_selected)) { // color.hasState(android.R.attr.state_selected)
activatedColor = color.getColorForState(StateSet.get(
StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_SELECTED), 0);
defaultColor = color.getColorForState(StateSet.get(
StateSet.VIEW_STATE_ENABLED), 0);
} else {
activatedColor = color.getDefaultColor();
// Generate a non-activated color using the disabled alpha.
final TypedArray ta = mContext.obtainStyledAttributes(ATTRS_DISABLED_ALPHA);
final float disabledAlpha = ta.getFloat(0, 0.30f);
defaultColor = multiplyAlphaComponent(activatedColor, disabledAlpha);
ta.recycle();
}
if (activatedColor == 0 || defaultColor == 0) {
// We somehow failed to obtain the colors.
return null;
}
final int[][] stateSet = new int[][]{{android.R.attr.state_activated}, {}};
final int[] colors = new int[]{activatedColor, defaultColor};
return new ColorStateList(stateSet, colors);
}
/**
* 设置DOtView的focus时的颜色
* @param color
*/
public void setBannerDotColor(ColorStateList color){
mIndicatorFocusDrawable = new ColorDrawable(color.getDefaultColor());
if(mCurrentDot != null) {
mCurrentDot.setDrawable(mIndicatorFocusDrawable);
}
}
private void bindTime(Alarm alarm) {
String time = DateFormat.getTimeFormat(getContext()).format(new Date(alarm.ringsAt()));
if (DateFormat.is24HourFormat(getContext())) {
mTime.setText(time);
} else {
TimeTextUtils.setText(time, mTime);
}
// Use a mock TextView to get our colors, because its ColorStateList is never
// mutated for the lifetime of this ViewHolder (even when reused).
// This solution is robust against dark/light theme changes, whereas using
// color resources is not.
TextView colorsSource = (TextView) itemView.findViewById(R.id.colors_source);
ColorStateList colors = colorsSource.getTextColors();
int def = colors.getDefaultColor();
// Too light
// int disabled = colors.getColorForState(new int[] {-android.R.attr.state_enabled}, def);
// Material guidelines say text hints and disabled text should have the same color.
int disabled = colorsSource.getCurrentHintTextColor();
// However, digging around in the system's textColorHint for 21+ says its 50% black for our
// light theme. I'd like to follow what the guidelines says, but I want code that is robust
// against theme changes. Alternatively, override the attribute values to what you want
// in both your dark and light themes...
// int disabled = ContextCompat.getColor(getContext(), R.color.text_color_disabled_light);
// We only have two states, so we don't care about losing the other state colors.
mTime.setTextColor(alarm.isEnabled() ? def : disabled);
}
public void setColor(@NonNull ColorStateList tintStateList) {
int defaultColor = tintStateList.getDefaultColor();
mFocusedColor = tintStateList.getColorForState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, defaultColor);
mPressedColor = tintStateList.getColorForState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, defaultColor);
mDisabledColor = tintStateList.getColorForState(new int[]{-android.R.attr.state_enabled}, defaultColor);
//The ripple should be partially transparent
mFocusedColor = getModulatedAlphaColor(130, mFocusedColor);
mPressedColor = getModulatedAlphaColor(130, mPressedColor);
mDisabledColor = getModulatedAlphaColor(130, mDisabledColor);
}
public void setColor(@NonNull ColorStateList tintStateList) {
int defaultColor = tintStateList.getDefaultColor();
mFocusedColor = tintStateList.getColorForState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, defaultColor);
mPressedColor = tintStateList.getColorForState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, defaultColor);
mDisabledColor = tintStateList.getColorForState(new int[]{-android.R.attr.state_enabled}, defaultColor);
//The ripple should be partially transparent
mFocusedColor = getModulatedAlphaColor(130, mFocusedColor);
mPressedColor = getModulatedAlphaColor(130, mPressedColor);
mDisabledColor = getModulatedAlphaColor(130, mDisabledColor);
}
public MarkerDrawable(@NonNull ColorStateList tintList, int closedSize) {
super(tintList);
mInterpolator = new AccelerateDecelerateInterpolator();
mClosedStateSize = closedSize;
mStartColor = tintList.getColorForState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, tintList.getDefaultColor());
mEndColor = tintList.getDefaultColor();
}
public MarkerDrawable(@NonNull ColorStateList tintList, int closedSize) {
super(tintList);
mInterpolator = new AccelerateDecelerateInterpolator();
mClosedStateSize = closedSize;
mStartColor = tintList.getColorForState(new int[]{android.R.attr.state_pressed}, tintList.getDefaultColor());
mEndColor = tintList.getDefaultColor();
}
public MarkerDrawable(@NonNull ColorStateList tintList, int closedSize) {
super(tintList);
mInterpolator = new AccelerateDecelerateInterpolator();
mClosedStateSize = closedSize;
mStartColor = tintList.getColorForState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, tintList.getDefaultColor());
mEndColor = tintList.getDefaultColor();
}
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);
}
@Override
public void setTextColor(ColorStateList colors) {
mTextColor = colors.getDefaultColor();
super.setTextColor(colors);
}
@Override
public void setTextColor(ColorStateList colors) {
mTextColor = colors.getDefaultColor();
super.setTextColor(colors);
}
public StateDrawable(@NonNull ColorStateList tintStateList) {
super();
mTintStateList = tintStateList;
mCurrentColor = tintStateList.getDefaultColor();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}