Java Code Examples for android.content.res.ColorStateList#getColorForState()

The following examples show how to use android.content.res.ColorStateList#getColorForState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DrawableHelper.java    From Mover with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: SimpleMonthView.java    From DateTimePicker with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: SimpleMonthView.java    From SublimePicker with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 4
Source File: EmTintManager.java    From AndroidTint with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 5
Source File: LinearLayout.java    From Carbon with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 6
Source File: Toolbar.java    From Carbon with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 7
Source File: MarkerDrawable.java    From sealrtc-android with MIT License 5 votes vote down vote up
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();
}
 
Example 8
Source File: Toolbar.java    From Carbon with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 9
Source File: TextView.java    From Carbon with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 10
Source File: YcRoundRectDrawable.java    From CardViewShadowColor with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: RoundRectDrawable.java    From OptionRoundCardview with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 12
Source File: SimpleMonthView.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 13
Source File: StatePaintDrawable.java    From Genius-Android with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 14
Source File: RelativeLayout.java    From Carbon with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 15
Source File: SwitchButton.java    From AssistantBySDK with Apache License 2.0 5 votes vote down vote up
@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();
		}
	}
}
 
Example 16
Source File: TabletTabRecyclerAdapter.java    From ChromeLikeTabSwitcher with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 17
Source File: SimpleMonthView.java    From SublimePicker with Apache License 2.0 4 votes vote down vote up
void setDayHighlightColor(ColorStateList dayHighlightColor) {
    final int pressedColor = dayHighlightColor.getColorForState(
            SUtils.resolveStateSet(SUtils.STATE_ENABLED | SUtils.STATE_PRESSED), 0);
    mDayHighlightPaint.setColor(pressedColor);
    invalidate();
}
 
Example 18
Source File: TintableImageView.java    From Expert-Android-Programming with MIT License 4 votes vote down vote up
public void setColorFilter(ColorStateList tint) {
    this.tint = tint;
    super.setColorFilter(tint.getColorForState(getDrawableState(), 0));
}
 
Example 19
Source File: SimpleMonthView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
void setMonthTextColor(ColorStateList monthTextColor) {
    final int enabledColor = monthTextColor.getColorForState(ENABLED_STATE_SET, 0);
    mMonthPaint.setColor(enabledColor);
    invalidate();
}
 
Example 20
Source File: RadialSelectorView.java    From StyleableDateTimePicker with MIT License 4 votes vote down vote up
/**
 * 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;
}