下面列出了android.content.res.ColorStateList#getColorForState() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Applies the specified text appearance resource to a paint, returning the
* text color if one is set in the text appearance.
*
* @param p the paint to modify
* @param resId the resource ID of the text appearance
* @return the text color, if available
*/
private ColorStateList applyTextAppearance(Paint p, int resId) {
final TypedArray ta = getContext().obtainStyledAttributes(null,
R.styleable.TextAppearance, 0, resId);
final String fontFamily = ta.getString(R.styleable.TextAppearance_fontFamily);
if (fontFamily != null) {
p.setTypeface(Typeface.create(fontFamily, Typeface.NORMAL));
}
p.setTextSize(ta.getDimensionPixelSize(
R.styleable.TextAppearance_android_textSize, (int) p.getTextSize()));
final ColorStateList textColor = Utils.getColorStateList(getContext(), ta, R.styleable.TextAppearance_android_textColor);
if (textColor != null) {
final int enabledColor = textColor.getColorForState(ENABLED_STATE_SET, 0);
p.setColor(enabledColor);
}
ta.recycle();
return textColor;
}
/**
* Ensures the tint filter is consistent with the current tint color and
* mode.
*/
private static PorterDuffColorFilter updateTintFilter(ColorStateList tint,
PorterDuff.Mode tintMode,
int[] drawableState) {
if (tint == null || tintMode == null) {
return null;
}
final int color = tint.getColorForState(drawableState, Color.TRANSPARENT);
PorterDuffColorFilter filter = COLOR_FILTER_CACHE.get(color, tintMode);
if(filter == null) {
filter = new PorterDuffColorFilter(color, tintMode);
COLOR_FILTER_CACHE.put(color, tintMode, filter);
}
return filter;
}
/**
* Ensures the tint filter is consistent with the current tint color and
* mode.
*/
private PorterDuffColorFilter createTintFilter(ColorStateList tint, PorterDuff.Mode tintMode) {
if (tint == null || tintMode == null) {
return null;
}
final int color = tint.getColorForState(getState(), Color.TRANSPARENT);
return new PorterDuffColorFilter(color, tintMode);
}
/**
* Adapts the color of a button, which allows to add a new tab.
*
* @param addTabItem
* The add tab item, which corresponds to the button, whose color should be adapted, as
* an instance of the class {@link AddTabItem}. The add tab item may not be null
*/
private void adaptAddTabButtonColor(@NonNull final AddTabItem addTabItem) {
ColorStateList colorStateList = getStyle().getAddTabButtonColor();
int[] stateSet = new int[]{};
int color = colorStateList.getColorForState(stateSet, colorStateList.getDefaultColor());
View view = addTabItem.getView();
Drawable background = view.getBackground();
background.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (!mIsThumbUseDrawable && mThumbColor != null) {
mCurrThumbColor = mThumbColor.getColorForState(getDrawableState(), mCurrThumbColor);
} else {
setDrawableState(mThumbDrawable);
}
int[] nextState = isChecked() ? UNCHECKED_PRESSED_STATE : CHECKED_PRESSED_STATE;
ColorStateList textColors = getTextColors();
if (textColors != null) {
int defaultTextColor = textColors.getDefaultColor();
mOnTextColor = textColors.getColorForState(CHECKED_PRESSED_STATE, defaultTextColor);
mOffTextColor = textColors.getColorForState(UNCHECKED_PRESSED_STATE, defaultTextColor);
}
if (!mIsBackUseDrawable && mBackColor != null) {
mCurrBackColor = mBackColor.getColorForState(getDrawableState(), mCurrBackColor);
mNextBackColor = mBackColor.getColorForState(nextState, mCurrBackColor);
} else {
if (mBackDrawable instanceof StateListDrawable && mFadeBack) {
mBackDrawable.setState(nextState);
mNextBackDrawable = mBackDrawable.getCurrent().mutate();
} else {
mNextBackDrawable = null;
}
setDrawableState(mBackDrawable);
if (mBackDrawable != null) {
mCurrentBackDrawable = mBackDrawable.getCurrent().mutate();
}
}
}
@Override
public void setOutlineSpotShadowColor(ColorStateList color) {
spotShadowColor = color;
if (Carbon.IS_PIE_OR_HIGHER) {
super.setOutlineSpotShadowColor(color.getColorForState(getDrawableState(), color.getDefaultColor()));
} else {
setElevation(elevation);
setTranslationZ(translationZ);
}
}
/**
* Ensures the tint filter is consistent with the current tint color and
* mode.
*/
PorterDuffColorFilter updateTintFilter(PorterDuffColorFilter tintFilter, ColorStateList tint,
PorterDuff.Mode tintMode) {
if (tint == null || tintMode == null) {
return null;
}
final int color = tint.getColorForState(getState(), Color.TRANSPARENT);
if (tintFilter == null) {
return new PorterDuffColorFilter(color, tintMode);
}
//tintFilter.setColor(color);
//tintFilter.setMode(tintMode);
try {
//noinspection unchecked
Class<PorterDuffColorFilter> tClass = (Class<PorterDuffColorFilter>) tintFilter.getClass();
Method method = tClass.getMethod("setColor", Integer.class);
method.invoke(tintFilter, color);
method = tClass.getMethod("setMode", PorterDuff.Mode.class);
method.invoke(tintFilter, tintMode);
return tintFilter;
} catch (Exception e) {
return new PorterDuffColorFilter(color, tintMode);
}
}
void setDaySelectorColor(ColorStateList dayBackgroundColor) {
final int activatedColor = dayBackgroundColor.getColorForState(
StateSet.get(StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_ACTIVATED), 0);
mDaySelectorPaint.setColor(activatedColor);
mDayHighlightSelectorPaint.setColor(activatedColor);
mDayHighlightSelectorPaint.setAlpha(SELECTED_HIGHLIGHT_ALPHA);
invalidate();
}
private static PorterDuffColorFilter createTintFilter(ColorStateList tint, PorterDuff.Mode tintMode, final int[] state) {
if (tint == null || tintMode == null) {
return null;
}
final int color = tint.getColorForState(state, Color.TRANSPARENT);
return getPorterDuffColorFilter(color, tintMode);
}
void setDaySelectorColor(ColorStateList dayBackgroundColor) {
final int activatedColor = dayBackgroundColor.getColorForState(
SUtils.resolveStateSet(SUtils.STATE_ENABLED | SUtils.STATE_ACTIVATED), 0);
mDaySelectorPaint.setColor(activatedColor);
mDayRangeSelectorPaint.setColor(activatedColor);
// TODO: expose as attr?
mDayRangeSelectorPaint.setAlpha(150);
invalidate();
}
/**
* Ensures the tint filter is consistent with the current tint color and
* mode.
*/
private PorterDuffColorFilter createTintFilter(ColorStateList tint, PorterDuff.Mode tintMode) {
if (tint == null || tintMode == null) {
return null;
}
final int color = tint.getColorForState(getState(), Color.TRANSPARENT);
return new PorterDuffColorFilter(color, tintMode);
}
@Override
public void setOutlineSpotShadowColor(ColorStateList color) {
spotShadowColor = color;
if (Carbon.IS_PIE_OR_HIGHER) {
super.setOutlineSpotShadowColor(color.getColorForState(getDrawableState(), color.getDefaultColor()));
} else {
setElevation(elevation);
setTranslationZ(translationZ);
}
}
@Override
public void setOutlineAmbientShadowColor(ColorStateList color) {
ambientShadowColor = color;
if (Carbon.IS_PIE_OR_HIGHER) {
super.setOutlineAmbientShadowColor(color.getColorForState(getDrawableState(), color.getDefaultColor()));
} else {
setElevation(elevation);
setTranslationZ(translationZ);
}
}
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();
}
@Override
public void setOutlineSpotShadowColor(ColorStateList color) {
spotShadowColor = color;
if (Carbon.IS_PIE_OR_HIGHER) {
super.setOutlineSpotShadowColor(color.getColorForState(getDrawableState(), color.getDefaultColor()));
} else {
setElevation(elevation);
setTranslationZ(translationZ);
}
}
@Override
public void setOutlineAmbientShadowColor(ColorStateList color) {
ambientShadowColor = color;
if (Carbon.IS_PIE_OR_HIGHER) {
super.setOutlineAmbientShadowColor(color.getColorForState(getDrawableState(), color.getDefaultColor()));
} else {
setElevation(elevation);
setTranslationZ(translationZ);
}
}
/**
* Initialize this selector with the state of the picker.
* @param context Current context.
* @param is24HourMode Whether the selector is in 24-hour mode, which will tell us
* whether the circle's center is moved up slightly to make room for the AM/PM circles.
* @param hasInnerCircle Whether we have both an inner and an outer circle of numbers
* that may be selected. Should be true for 24-hour mode in the hours circle.
* @param disappearsOut Whether the numbers' animation will have them disappearing out
* or disappearing in.
* @param selectionDegrees The initial degrees to be selected.
* @param isInnerCircle Whether the initial selection is in the inner or outer circle.
* Will be ignored when hasInnerCircle is false.
*/
public void initialize(Context context, boolean is24HourMode, boolean hasInnerCircle,
boolean disappearsOut, int selectionDegrees, boolean isInnerCircle) {
if (mIsInitialized) {
Log.e(TAG, "This RadialSelectorView may only be initialized once.");
return;
}
Resources res = context.getResources();
int color = res.getColor(R.color.blue);
TypedArray attrArray = getContext().getTheme().obtainStyledAttributes(R.styleable.DateTimePicker);
int attrCount = attrArray.getIndexCount();
for (int i = 0; i < attrCount; i++) {
int attr = attrArray.getIndex(i);
switch (attr) {
case R.styleable.DateTimePicker_hightlightedTextColor:
ColorStateList stateList = attrArray.getColorStateList(attr);
color = stateList.getColorForState(new int[]{android.R.attr.state_selected}, stateList.getDefaultColor());
break;
}
}
mPaint.setColor(color);
mPaint.setAntiAlias(true);
mSelectionAlpha = SELECTED_ALPHA;
// Calculate values for the circle radius size.
mIs24HourMode = is24HourMode;
if (is24HourMode) {
mCircleRadiusMultiplier = Float.parseFloat(
res.getString(R.string.circle_radius_multiplier_24HourMode));
} else {
mCircleRadiusMultiplier = Float.parseFloat(
res.getString(R.string.circle_radius_multiplier));
mAmPmCircleRadiusMultiplier =
Float.parseFloat(res.getString(R.string.ampm_circle_radius_multiplier));
}
// Calculate values for the radius size(s) of the numbers circle(s).
mHasInnerCircle = hasInnerCircle;
if (hasInnerCircle) {
mInnerNumbersRadiusMultiplier =
Float.parseFloat(res.getString(R.string.numbers_radius_multiplier_inner));
mOuterNumbersRadiusMultiplier =
Float.parseFloat(res.getString(R.string.numbers_radius_multiplier_outer));
} else {
mNumbersRadiusMultiplier =
Float.parseFloat(res.getString(R.string.numbers_radius_multiplier_normal));
}
mSelectionRadiusMultiplier =
Float.parseFloat(res.getString(R.string.selection_radius_multiplier));
// Calculate values for the transition mid-way states.
mAnimationRadiusMultiplier = 1;
mTransitionMidRadiusMultiplier = 1f + (0.05f * (disappearsOut? -1 : 1));
mTransitionEndRadiusMultiplier = 1f + (0.3f * (disappearsOut? 1 : -1));
mInvalidateUpdateListener = new InvalidateUpdateListener();
setSelection(selectionDegrees, isInnerCircle, false);
mIsInitialized = true;
}
void setMonthTextColor(ColorStateList monthTextColor) {
final int enabledColor = monthTextColor.getColorForState(ENABLED_STATE_SET, 0);
mMonthPaint.setColor(enabledColor);
invalidate();
}
public void setColorFilter(ColorStateList tint) {
this.tint = tint;
super.setColorFilter(tint.getColorForState(getDrawableState(), 0));
}
void setDayHighlightColor(ColorStateList dayHighlightColor) {
final int pressedColor = dayHighlightColor.getColorForState(
SUtils.resolveStateSet(SUtils.STATE_ENABLED | SUtils.STATE_PRESSED), 0);
mDayHighlightPaint.setColor(pressedColor);
invalidate();
}