Search in sources :

Example 16 with ColorStateList

use of android.content.res.ColorStateList in project Carbon by ZieIony.

the class EditText method drawableStateChanged.

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    if (rippleDrawable != null && rippleDrawable.getStyle() != RippleDrawable.Style.Background)
        rippleDrawable.setState(getDrawableState());
    if (stateAnimator != null)
        stateAnimator.setState(getDrawableState());
    ColorStateList textColors = getTextColors();
    if (textColors instanceof AnimatedColorStateList)
        ((AnimatedColorStateList) textColors).setState(getDrawableState());
    if (tint != null && tint instanceof AnimatedColorStateList)
        ((AnimatedColorStateList) tint).setState(getDrawableState());
    if (backgroundTint != null && backgroundTint instanceof AnimatedColorStateList)
        ((AnimatedColorStateList) backgroundTint).setState(getDrawableState());
}
Also used : ColorStateList(android.content.res.ColorStateList) DefaultNormalColorStateList(carbon.drawable.DefaultNormalColorStateList) DefaultColorStateList(carbon.drawable.DefaultColorStateList) AnimatedColorStateList(carbon.animation.AnimatedColorStateList) AnimatedColorStateList(carbon.animation.AnimatedColorStateList)

Example 17 with ColorStateList

use of android.content.res.ColorStateList in project remusic by aa112901.

the class ColorStateListUtils method createColorStateList.

static ColorStateList createColorStateList(Context context, int resId) {
    if (resId <= 0)
        return null;
    TypedValue value = new TypedValue();
    context.getResources().getValue(resId, value, true);
    ColorStateList cl = null;
    if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        //Assume that "color/theme_color_primary" and "color/theme_color_profile" have the same color value;
        //However, "color/theme_color_primary" need to replace by themeId, "color/theme_color_profile" not.
        //If use value.data may cause "color/theme_color_profile" still been replaced by themeId
        cl = ColorStateList.valueOf(ThemeUtils.replaceColorById(context, value.resourceId));
    } else {
        final String file = value.string.toString();
        try {
            if (file.endsWith("xml")) {
                final XmlResourceParser rp = context.getResources().getAssets().openXmlResourceParser(value.assetCookie, file);
                final AttributeSet attrs = Xml.asAttributeSet(rp);
                int type;
                while ((type = rp.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
                // Seek parser to start tag.
                }
                if (type != XmlPullParser.START_TAG) {
                    throw new XmlPullParserException("No start tag found");
                }
                cl = createFromXmlInner(context, rp, attrs);
                rp.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
    }
    return cl;
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet) ColorStateList(android.content.res.ColorStateList) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) TypedValue(android.util.TypedValue)

Example 18 with ColorStateList

use of android.content.res.ColorStateList in project remusic by aa112901.

the class ColorStateListUtils method inflateColorStateList.

static ColorStateList inflateColorStateList(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    int type;
    LinkedList<int[]> stateList = new LinkedList<>();
    LinkedList<Integer> colorList = new LinkedList<>();
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG || depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }
        TypedArray a1 = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.color });
        final int value = a1.getResourceId(0, Color.MAGENTA);
        final int baseColor = value == Color.MAGENTA ? Color.MAGENTA : ThemeUtils.replaceColorById(context, value);
        a1.recycle();
        TypedArray a2 = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.alpha });
        final float alphaMod = a2.getFloat(0, 1.0f);
        a2.recycle();
        colorList.add(alphaMod != 1.0f ? ColorUtils.setAlphaComponent(baseColor, Math.round(Color.alpha(baseColor) * alphaMod)) : baseColor);
        stateList.add(extractStateSet(attrs));
    }
    if (stateList.size() > 0 && stateList.size() == colorList.size()) {
        int[] colors = new int[colorList.size()];
        for (int i = 0; i < colorList.size(); i++) {
            colors[i] = colorList.get(i);
        }
        return new ColorStateList(stateList.toArray(new int[stateList.size()][]), colors);
    }
    return null;
}
Also used : TypedArray(android.content.res.TypedArray) ColorStateList(android.content.res.ColorStateList) LinkedList(java.util.LinkedList)

Example 19 with ColorStateList

use of android.content.res.ColorStateList in project remusic by aa112901.

the class LayerDrawableUtils method inflateDrawable.

@Override
protected Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    final int innerDepth = parser.getDepth() + 1;
    int type;
    int depth;
    int layerAttrUseCount = 0;
    int drawableUseCount = 0;
    int space = STEP << 1;
    //L,T,R,B,S,E,id
    int[][] childLayersAttrs = new int[space][ATTRS.length];
    Drawable[] drawables = new Drawable[space];
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }
        if (depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }
        if (layerAttrUseCount >= childLayersAttrs.length) {
            int[][] dstInt = new int[drawables.length + STEP][ATTRS.length];
            System.arraycopy(childLayersAttrs, 0, dstInt, 0, childLayersAttrs.length);
            childLayersAttrs = dstInt;
        }
        updateLayerAttrs(context, attrs, childLayersAttrs[layerAttrUseCount]);
        layerAttrUseCount++;
        Drawable drawable = getAttrDrawable(context, attrs, android.R.attr.drawable);
        // element.
        if (drawable == null) {
            while ((type = parser.next()) == XmlPullParser.TEXT) {
            }
            if (type != XmlPullParser.START_TAG) {
                throw new XmlPullParserException(parser.getPositionDescription() + ": <item> tag requires a 'drawable' attribute or " + "child tag defining a drawable");
            }
            drawable = createFromXmlInner(context, parser, attrs);
        } else {
            final ColorStateList cls = getTintColorList(context, attrs, R.attr.drawableTint);
            if (cls != null) {
                drawable = ThemeUtils.tintDrawable(drawable, cls, getTintMode(context, attrs, R.attr.drawableTintMode));
            }
        }
        if (drawable != null) {
            if (drawableUseCount >= drawables.length) {
                Drawable[] dst = new Drawable[drawables.length + STEP];
                System.arraycopy(drawables, 0, dst, 0, drawables.length);
                drawables = dst;
            }
            drawables[drawableUseCount] = drawable;
            drawableUseCount++;
        }
    }
    if (drawables[0] == null || drawableUseCount != layerAttrUseCount) {
        return null;
    } else {
        LayerDrawable layerDrawable = new LayerDrawable(drawables);
        for (int i = 0; i < drawables.length; i++) {
            int[] childLayersAttr = childLayersAttrs[i];
            if (childLayersAttr[0] != 0 || childLayersAttr[1] != 0 || childLayersAttr[2] != 0 || childLayersAttr[3] != 0) {
                layerDrawable.setLayerInset(i, childLayersAttr[0], childLayersAttr[1], childLayersAttr[2], childLayersAttr[3]);
            }
            if (childLayersAttr[4] != 0) {
                layerDrawable.setId(i, childLayersAttr[4]);
            }
        }
        return layerDrawable;
    }
}
Also used : LayerDrawable(android.graphics.drawable.LayerDrawable) LayerDrawable(android.graphics.drawable.LayerDrawable) Drawable(android.graphics.drawable.Drawable) ColorStateList(android.content.res.ColorStateList) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 20 with ColorStateList

use of android.content.res.ColorStateList in project remusic by aa112901.

the class ThemeUtils method getDisabledThemeAttrColor.

public static int getDisabledThemeAttrColor(Context context, @AttrRes int attr) {
    final ColorStateList csl = getThemeAttrColorStateList(context, attr);
    if (csl != null && csl.isStateful()) {
        // If the CSL is stateful, we'll assume it has a disabled state and use it
        return csl.getColorForState(DISABLED_STATE_SET, csl.getDefaultColor());
    } else {
        // Else, we'll generate the color using disabledAlpha from the theme
        final TypedValue tv = getTypedValue();
        // Now retrieve the disabledAlpha value from the theme
        context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, tv, true);
        final float disabledAlpha = tv.getFloat();
        return getThemeAttrColor(context, attr, disabledAlpha);
    }
}
Also used : ColorStateList(android.content.res.ColorStateList) TypedValue(android.util.TypedValue)

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