Search in sources :

Example 76 with ColorStateList

use of android.content.res.ColorStateList in project Douya by DreaminginCodeZH.

the class TintHelper method createNavigationItemTintList.

private static ColorStateList createNavigationItemTintList(int baseColorAttrRes, int primaryColor, Context context) {
    ColorStateList baseColor = ViewUtils.getColorStateListFromAttrRes(baseColorAttrRes, context);
    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), primaryColor, defaultColor });
}
Also used : ColorStateList(android.content.res.ColorStateList)

Example 77 with ColorStateList

use of android.content.res.ColorStateList in project Douya by DreaminginCodeZH.

the class ViewUtils method getColorStateListFromAttrRes.

public static ColorStateList getColorStateListFromAttrRes(int attrRes, Context context) {
    int[] attrs = new int[] { attrRes };
    // TODO: Switch to TintTypedArray when they added this overload.
    TypedArray a = context.obtainStyledAttributes(attrs);
    // 0 is an invalid identifier according to the docs of {@link Resources}.
    int resId = a.getResourceId(0, 0);
    ColorStateList colorStateList = null;
    if (resId != 0) {
        colorStateList = AppCompatResources.getColorStateList(context, resId);
    }
    a.recycle();
    return colorStateList;
}
Also used : TypedArray(android.content.res.TypedArray) ColorStateList(android.content.res.ColorStateList)

Example 78 with ColorStateList

use of android.content.res.ColorStateList in project MaterialProgressBar by DreaminginCodeZH.

the class HorizontalProgressDrawable method updateSecondaryProgressTint.

@SuppressLint("NewApi")
private void updateSecondaryProgressTint() {
    if (mHasSecondaryProgressTintColor) {
        int tintColor = mSecondaryProgressTintColor;
        if (!getShowBackground()) {
            // Alpha of tintColor may not be mBackgroundAlpha because we modulated it in
            // setTint().
            float backgroundAlpha = (float) Color.alpha(tintColor) / 0xFF;
            tintColor = ColorUtils.setAlphaComponent(tintColor, Math.round(0xFF * compositeAlpha(backgroundAlpha, backgroundAlpha)));
        }
        mSecondaryProgressDrawable.setTint(tintColor);
    } else if (mHasSecondaryProgressTint) {
        ColorStateList tint = mSecondaryProgressTint;
        if (!getShowBackground()) {
            // Composite alpha so that the secondary progress looks as before.
            tint = tint.withAlpha(Math.round(0xFF * compositeAlpha(mBackgroundAlpha, mBackgroundAlpha)));
        }
        mSecondaryProgressDrawable.setTintList(tint);
    }
}
Also used : ColorStateList(android.content.res.ColorStateList) SuppressLint(android.annotation.SuppressLint) SuppressLint(android.annotation.SuppressLint)

Example 79 with ColorStateList

use of android.content.res.ColorStateList in project MagicaSakura by Bilibili.

the class RippleDrawableInflateImpl method inflateDrawable.

@Override
public Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        final int innerDepth = parser.getDepth() + 1;
        int type;
        int depth;
        int markIndex = -1;
        int layerAttrUseCount = 0;
        int drawableUseCount = 0;
        int space = STEP;
        //L,T,R,B,S,E,id
        int[][] childLayersAttrs = new int[space][ATTRS.length];
        Drawable[] drawables = new Drawable[space];
        ColorStateList csl = DrawableUtils.getTintColorList(context, attrs, android.R.attr.color);
        if (csl == null) {
            return null;
        }
        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 = DrawableUtils.getAttrDrawable(context, attrs, android.R.attr.drawable);
            if (DrawableUtils.getAttrResourceId(context, attrs, android.R.attr.id, 0) == android.R.id.mask) {
                markIndex = layerAttrUseCount - 1;
            }
            // 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 = DrawableUtils.createFromXmlInner(context, parser, attrs);
            } else {
                final ColorStateList cls = DrawableUtils.getTintColorList(context, attrs, R.attr.drawableTint);
                if (cls != null) {
                    drawable = ThemeUtils.tintDrawable(drawable, cls, DrawableUtils.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 {
            RippleDrawable rippleDrawable;
            rippleDrawable = new RippleDrawable(csl, null, markIndex >= 0 ? drawables[markIndex] : null);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                int radius = DrawableUtils.getAttrDimensionPixelSize(context, attrs, android.R.attr.radius, RippleDrawable.RADIUS_AUTO);
                rippleDrawable.setRadius(radius);
            }
            for (int i = 0; i < drawables.length; i++) {
                if (i == markIndex) {
                    continue;
                }
                addLayer(rippleDrawable, drawables[i]);
                int[] childLayersAttr = childLayersAttrs[i];
                if (childLayersAttr[0] != 0 || childLayersAttr[1] != 0 || childLayersAttr[2] != 0 || childLayersAttr[3] != 0) {
                    rippleDrawable.setLayerInset(i, childLayersAttr[0], childLayersAttr[1], childLayersAttr[2], childLayersAttr[3]);
                }
                if (childLayersAttr[4] != 0) {
                    rippleDrawable.setId(i, childLayersAttr[4]);
                }
            }
            return rippleDrawable;
        }
    }
    return null;
}
Also used : RippleDrawable(android.graphics.drawable.RippleDrawable) Drawable(android.graphics.drawable.Drawable) ColorStateList(android.content.res.ColorStateList) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) RippleDrawable(android.graphics.drawable.RippleDrawable)

Example 80 with ColorStateList

use of android.content.res.ColorStateList in project MagicaSakura by Bilibili.

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)280 Paint (android.graphics.Paint)90 TypedArray (android.content.res.TypedArray)76 Drawable (android.graphics.drawable.Drawable)48 TextPaint (android.text.TextPaint)42 Resources (android.content.res.Resources)25 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)22 TypedValue (android.util.TypedValue)21 TextView (android.widget.TextView)19 Context (android.content.Context)18 SuppressLint (android.annotation.SuppressLint)17 SmallTest (android.test.suitebuilder.annotation.SmallTest)13 AllCapsTransformationMethod (android.text.method.AllCapsTransformationMethod)13 RippleDrawable (android.graphics.drawable.RippleDrawable)12 StateListDrawable (android.graphics.drawable.StateListDrawable)12 View (android.view.View)12 Nullable (android.annotation.Nullable)10 Bitmap (android.graphics.Bitmap)10 TextAppearanceSpan (android.text.style.TextAppearanceSpan)9 PorterDuff (android.graphics.PorterDuff)8