Search in sources :

Example 46 with ColorFilter

use of android.graphics.ColorFilter in project Shuttle by timusus.

the class DrawableUtils method getBlackBitmap.

/**
 * Takes a drawable resource and turns it black
 *
 * @param baseDrawableResId the resource id of the drawable to theme
 * @return a themed {@link android.graphics.drawable.Drawable}
 */
public static Bitmap getBlackBitmap(Context context, int baseDrawableResId) {
    Drawable baseDrawable = ContextCompat.getDrawable(context, baseDrawableResId).getConstantState().newDrawable();
    ColorFilter colorFilter = new LightingColorFilter(ContextCompat.getColor(context, R.color.black), 0);
    baseDrawable.mutate().setColorFilter(colorFilter);
    Bitmap bitmap = Bitmap.createBitmap(baseDrawable.getIntrinsicWidth(), baseDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    baseDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    baseDrawable.draw(canvas);
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) ColorFilter(android.graphics.ColorFilter) LightingColorFilter(android.graphics.LightingColorFilter) Canvas(android.graphics.Canvas) Drawable(android.graphics.drawable.Drawable) LightingColorFilter(android.graphics.LightingColorFilter)

Example 47 with ColorFilter

use of android.graphics.ColorFilter in project Shuttle by timusus.

the class DrawableUtils method getColoredBitmap.

/**
 * Takes a drawable resource and applies the current theme highlight color to it
 *
 * @param baseDrawableResId the resource id of the drawable to theme
 * @return a themed {@link android.graphics.drawable.Drawable}
 */
public static Bitmap getColoredBitmap(Context context, int baseDrawableResId) {
    Drawable baseDrawable = ContextCompat.getDrawable(context, baseDrawableResId).getConstantState().newDrawable();
    ColorFilter highlightColorFilter = new LightingColorFilter(Aesthetic.get(context).colorPrimary().blockingFirst(), 0);
    baseDrawable.mutate().setColorFilter(highlightColorFilter);
    Bitmap bitmap = Bitmap.createBitmap(baseDrawable.getIntrinsicWidth(), baseDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    baseDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    baseDrawable.draw(canvas);
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) ColorFilter(android.graphics.ColorFilter) LightingColorFilter(android.graphics.LightingColorFilter) Canvas(android.graphics.Canvas) Drawable(android.graphics.drawable.Drawable) LightingColorFilter(android.graphics.LightingColorFilter)

Example 48 with ColorFilter

use of android.graphics.ColorFilter in project remusic by aa112901.

the class StateListDrawableUtils method inflateDrawable.

@Override
protected Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    StateListDrawable sd = null;
    ArrayList<int[]> states = new ArrayList<>();
    ArrayList<Drawable> drawables = new ArrayList<>();
    SparseArray<ColorFilter> mColorFilterMap = null;
    final int innerDepth = parser.getDepth() + 1;
    int type;
    int depth;
    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;
        }
        Drawable dr = getAttrDrawable(context, attrs, android.R.attr.drawable);
        states.add(extractStateSet(attrs));
        // attributes and extracting states.
        if (dr == 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");
            }
            dr = createFromXmlInner(context, parser, attrs);
        } else {
            ColorFilter colorFilter = getAttrColorFilter(context, attrs, R.attr.drawableTint, R.attr.drawableTintMode);
            if (colorFilter != null) {
                if (mColorFilterMap == null) {
                    mColorFilterMap = new SparseArray<>();
                }
                mColorFilterMap.put(drawables.size(), colorFilter);
            }
        }
        drawables.add(dr);
    }
    if (states.size() >= 1) {
        if (mColorFilterMap != null) {
            sd = new FilterableStateListDrawable();
            for (int i = 0; i < states.size(); i++) {
                ((FilterableStateListDrawable) sd).addState(states.get(i), drawables.get(i), mColorFilterMap.get(i));
            }
        } else {
            sd = new StateListDrawable();
            for (int i = 0; i < states.size(); i++) {
                sd.addState(states.get(i), drawables.get(i));
            }
        }
    }
    return sd;
}
Also used : FilterableStateListDrawable(com.bilibili.magicasakura.drawables.FilterableStateListDrawable) ColorFilter(android.graphics.ColorFilter) ArrayList(java.util.ArrayList) StateListDrawable(android.graphics.drawable.StateListDrawable) Drawable(android.graphics.drawable.Drawable) FilterableStateListDrawable(com.bilibili.magicasakura.drawables.FilterableStateListDrawable) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) StateListDrawable(android.graphics.drawable.StateListDrawable) FilterableStateListDrawable(com.bilibili.magicasakura.drawables.FilterableStateListDrawable)

Example 49 with ColorFilter

use of android.graphics.ColorFilter in project Shuttle by timusus.

the class DrawableUtils method getBaseDrawable.

/**
     * Takes a drawable resource and colors it according to the base color of the theme
     *
     * @param baseDrawableResId the resource id of the drawable to theme
     * @return a themed {@link android.graphics.drawable.Drawable}
     */
public static Drawable getBaseDrawable(Context context, int baseDrawableResId) {
    Drawable baseDrawable = context.getResources().getDrawable(baseDrawableResId).getConstantState().newDrawable();
    ColorFilter colorFilter = new LightingColorFilter(ThemeUtils.getBaseColor(context), 0);
    baseDrawable.mutate().setColorFilter(colorFilter);
    return baseDrawable;
}
Also used : ColorFilter(android.graphics.ColorFilter) LightingColorFilter(android.graphics.LightingColorFilter) LayerDrawable(android.graphics.drawable.LayerDrawable) FilterableStateListDrawable(com.simplecity.amp_library.ui.views.FilterableStateListDrawable) Drawable(android.graphics.drawable.Drawable) LightingColorFilter(android.graphics.LightingColorFilter)

Example 50 with ColorFilter

use of android.graphics.ColorFilter in project Shuttle by timusus.

the class DrawableUtils method getColoredStateListDrawableWithThemeColor.

/**
     * Returns a {@link FilterableStateListDrawable}, coloring the passed in
     * drawable according to the theme and the passed in highlight color
     *
     * @param baseDrawable the drawable to use
     * @return an {@link FilterableStateListDrawable}, coloring the passed in
     * drawable according to the theme and the passed in highlight color
     */
public static Drawable getColoredStateListDrawableWithThemeColor(Context context, Drawable baseDrawable, @ThemeUtils.ThemeColor int color) {
    if (baseDrawable == null) {
        return null;
    }
    Drawable highlightDrawable = baseDrawable.getConstantState().newDrawable();
    int baseColor;
    if (color == ThemeUtils.WHITE) {
        baseColor = context.getResources().getColor(R.color.drawable_base_color_dark);
    } else {
        baseColor = context.getResources().getColor(R.color.drawable_base_color_light);
    }
    ColorFilter baseColorFilter = new LightingColorFilter(baseColor, 0);
    int accentColor = ColorUtils.getAccentColor();
    if (accentColor == ColorUtils.getPrimaryColor()) {
        accentColor = Color.WHITE;
    }
    ColorFilter highlightColorFilter = new LightingColorFilter(accentColor, 0);
    FilterableStateListDrawable filterableStateListDrawable = new FilterableStateListDrawable();
    filterableStateListDrawable.addState(new int[] { android.R.attr.state_pressed }, baseDrawable, highlightColorFilter);
    filterableStateListDrawable.addState(StateSet.WILD_CARD, highlightDrawable, baseColorFilter);
    return filterableStateListDrawable;
}
Also used : FilterableStateListDrawable(com.simplecity.amp_library.ui.views.FilterableStateListDrawable) ColorFilter(android.graphics.ColorFilter) LightingColorFilter(android.graphics.LightingColorFilter) LayerDrawable(android.graphics.drawable.LayerDrawable) FilterableStateListDrawable(com.simplecity.amp_library.ui.views.FilterableStateListDrawable) Drawable(android.graphics.drawable.Drawable) LightingColorFilter(android.graphics.LightingColorFilter)

Aggregations

ColorFilter (android.graphics.ColorFilter)75 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)26 LightingColorFilter (android.graphics.LightingColorFilter)25 Paint (android.graphics.Paint)25 Drawable (android.graphics.drawable.Drawable)23 Canvas (android.graphics.Canvas)14 FilterableStateListDrawable (com.simplecity.amp_library.ui.views.FilterableStateListDrawable)14 LayerDrawable (android.graphics.drawable.LayerDrawable)12 Bitmap (android.graphics.Bitmap)11 View (android.view.View)7 Test (org.junit.Test)7 Path (android.graphics.Path)5 Rect (android.graphics.Rect)5 RectF (android.graphics.RectF)5 ColorMatrixColorFilter (android.graphics.ColorMatrixColorFilter)4 ImageView (android.widget.ImageView)4 TextView (android.widget.TextView)4 Animator (android.animation.Animator)3 SuppressLint (android.annotation.SuppressLint)3 Intent (android.content.Intent)3