Search in sources :

Example 66 with ColorStateList

use of android.content.res.ColorStateList in project android_frameworks_base by DirtyUnicorns.

the class TimePickerClockDelegate method applyLegacyColorFixes.

/**
     * 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);
}
Also used : TypedArray(android.content.res.TypedArray) ColorStateList(android.content.res.ColorStateList) Nullable(android.annotation.Nullable)

Example 67 with ColorStateList

use of android.content.res.ColorStateList in project android_frameworks_base by DirtyUnicorns.

the class TextView method setTextAppearance.

/**
     * Sets the text color, size, style, hint color, and highlight color
     * from the specified TextAppearance resource.
     *
     * @deprecated Use {@link #setTextAppearance(int)} instead.
     */
@Deprecated
public void setTextAppearance(Context context, @StyleRes int resId) {
    final TypedArray ta = context.obtainStyledAttributes(resId, R.styleable.TextAppearance);
    final int textColorHighlight = ta.getColor(R.styleable.TextAppearance_textColorHighlight, 0);
    if (textColorHighlight != 0) {
        setHighlightColor(textColorHighlight);
    }
    final ColorStateList textColor = ta.getColorStateList(R.styleable.TextAppearance_textColor);
    if (textColor != null) {
        setTextColor(textColor);
    }
    final int textSize = ta.getDimensionPixelSize(R.styleable.TextAppearance_textSize, 0);
    if (textSize != 0) {
        setRawTextSize(textSize);
    }
    final ColorStateList textColorHint = ta.getColorStateList(R.styleable.TextAppearance_textColorHint);
    if (textColorHint != null) {
        setHintTextColor(textColorHint);
    }
    final ColorStateList textColorLink = ta.getColorStateList(R.styleable.TextAppearance_textColorLink);
    if (textColorLink != null) {
        setLinkTextColor(textColorLink);
    }
    final String fontFamily = ta.getString(R.styleable.TextAppearance_fontFamily);
    final int typefaceIndex = ta.getInt(R.styleable.TextAppearance_typeface, -1);
    final int styleIndex = ta.getInt(R.styleable.TextAppearance_textStyle, -1);
    setTypefaceFromAttrs(fontFamily, typefaceIndex, styleIndex);
    final int shadowColor = ta.getInt(R.styleable.TextAppearance_shadowColor, 0);
    if (shadowColor != 0) {
        final float dx = ta.getFloat(R.styleable.TextAppearance_shadowDx, 0);
        final float dy = ta.getFloat(R.styleable.TextAppearance_shadowDy, 0);
        final float r = ta.getFloat(R.styleable.TextAppearance_shadowRadius, 0);
        setShadowLayer(r, dx, dy, shadowColor);
    }
    if (ta.getBoolean(R.styleable.TextAppearance_textAllCaps, false)) {
        setTransformationMethod(new AllCapsTransformationMethod(getContext()));
    }
    if (ta.hasValue(R.styleable.TextAppearance_elegantTextHeight)) {
        setElegantTextHeight(ta.getBoolean(R.styleable.TextAppearance_elegantTextHeight, false));
    }
    if (ta.hasValue(R.styleable.TextAppearance_letterSpacing)) {
        setLetterSpacing(ta.getFloat(R.styleable.TextAppearance_letterSpacing, 0));
    }
    if (ta.hasValue(R.styleable.TextAppearance_fontFeatureSettings)) {
        setFontFeatureSettings(ta.getString(R.styleable.TextAppearance_fontFeatureSettings));
    }
    ta.recycle();
}
Also used : TypedArray(android.content.res.TypedArray) ColorStateList(android.content.res.ColorStateList) SpannedString(android.text.SpannedString) SpannableString(android.text.SpannableString) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) AllCapsTransformationMethod(android.text.method.AllCapsTransformationMethod)

Example 68 with ColorStateList

use of android.content.res.ColorStateList in project android_frameworks_base by DirtyUnicorns.

the class NotificationColorUtil method processTextAppearanceSpan.

private TextAppearanceSpan processTextAppearanceSpan(TextAppearanceSpan span) {
    ColorStateList colorStateList = span.getTextColor();
    if (colorStateList != null) {
        int[] colors = colorStateList.getColors();
        boolean changed = false;
        for (int i = 0; i < colors.length; i++) {
            if (ImageUtils.isGrayscale(colors[i])) {
                // list.
                if (!changed) {
                    colors = Arrays.copyOf(colors, colors.length);
                }
                colors[i] = processColor(colors[i]);
                changed = true;
            }
        }
        if (changed) {
            return new TextAppearanceSpan(span.getFamily(), span.getTextStyle(), span.getTextSize(), new ColorStateList(colorStateList.getStates(), colors), span.getLinkTextColor());
        }
    }
    return span;
}
Also used : TextAppearanceSpan(android.text.style.TextAppearanceSpan) ColorStateList(android.content.res.ColorStateList)

Example 69 with ColorStateList

use of android.content.res.ColorStateList in project android_frameworks_base by DirtyUnicorns.

the class ShapeDrawable method updateStateFromTypedArray.

private void updateStateFromTypedArray(TypedArray a) {
    final ShapeState state = mShapeState;
    final Paint paint = state.mPaint;
    // Account for any configuration changes.
    state.mChangingConfigurations |= a.getChangingConfigurations();
    // Extract the theme attributes, if any.
    state.mThemeAttrs = a.extractThemeAttrs();
    int color = paint.getColor();
    color = a.getColor(R.styleable.ShapeDrawable_color, color);
    paint.setColor(color);
    boolean dither = paint.isDither();
    dither = a.getBoolean(R.styleable.ShapeDrawable_dither, dither);
    paint.setDither(dither);
    setIntrinsicWidth((int) a.getDimension(R.styleable.ShapeDrawable_width, state.mIntrinsicWidth));
    setIntrinsicHeight((int) a.getDimension(R.styleable.ShapeDrawable_height, state.mIntrinsicHeight));
    final int tintMode = a.getInt(R.styleable.ShapeDrawable_tintMode, -1);
    if (tintMode != -1) {
        state.mTintMode = Drawable.parseTintMode(tintMode, Mode.SRC_IN);
    }
    final ColorStateList tint = a.getColorStateList(R.styleable.ShapeDrawable_tint);
    if (tint != null) {
        state.mTint = tint;
    }
}
Also used : ColorStateList(android.content.res.ColorStateList) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 70 with ColorStateList

use of android.content.res.ColorStateList in project android_frameworks_base by DirtyUnicorns.

the class ColorStateListTest method testStateIsInList.

@SmallTest
public void testStateIsInList() throws Exception {
    ColorStateList colorStateList = mResources.getColorStateList(R.color.color1);
    int[] focusedState = { android.R.attr.state_focused };
    int focusColor = colorStateList.getColorForState(focusedState, R.color.failColor);
    assertEquals(mResources.getColor(R.color.testcolor1), focusColor);
}
Also used : ColorStateList(android.content.res.ColorStateList) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

ColorStateList (android.content.res.ColorStateList)269 Paint (android.graphics.Paint)90 TypedArray (android.content.res.TypedArray)76 Drawable (android.graphics.drawable.Drawable)43 TextPaint (android.text.TextPaint)42 Resources (android.content.res.Resources)25 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)22 TypedValue (android.util.TypedValue)21 Context (android.content.Context)18 TextView (android.widget.TextView)18 SmallTest (android.test.suitebuilder.annotation.SmallTest)13 AllCapsTransformationMethod (android.text.method.AllCapsTransformationMethod)13 SuppressLint (android.annotation.SuppressLint)12 View (android.view.View)12 StateListDrawable (android.graphics.drawable.StateListDrawable)11 Nullable (android.annotation.Nullable)10 Bitmap (android.graphics.Bitmap)10 TextAppearanceSpan (android.text.style.TextAppearanceSpan)9 PorterDuff (android.graphics.PorterDuff)7 RippleDrawable (android.graphics.drawable.RippleDrawable)7