Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d9e788c4 authored by Alan Viverette's avatar Alan Viverette
Browse files

Add support for specifying Drawable color filter in XML

BUG: 12178044
Change-Id: Ie118aebf56bb4580c97b625e20f4e76bed4b6f6f
parent 23ab74de
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -377,6 +377,8 @@ package android {
    field public static final int colorActivatedHighlight = 16843664; // 0x1010390
    field public static final int colorBackground = 16842801; // 0x1010031
    field public static final int colorBackgroundCacheHint = 16843435; // 0x10102ab
    field public static final int colorFilterColor = 16843767; // 0x10103f7
    field public static final int colorFilterMode = 16843768; // 0x10103f8
    field public static final int colorFocusedHighlight = 16843663; // 0x101038f
    field public static final int colorForeground = 16842800; // 0x1010030
    field public static final int colorForegroundInverse = 16843270; // 0x1010206
+17 −0
Original line number Diff line number Diff line
@@ -4060,6 +4060,19 @@
             RTL (right-to-left).  See
             {@link android.graphics.drawable.Drawable#setAutoMirrored}. -->
        <attr name="autoMirrored" format="boolean" />
        <!-- If set, specifies the color to apply to the drawable as a color filter. By
             default, no color filter is applied. -->
        <attr name="colorFilterColor" format="color" />
        <!-- When a color filter color is set, specifies its Porter-Duff blending mode.
             The default value is multiply. -->
        <attr name="colorFilterMode">
            <!-- [Sa * Da, Sc * Dc] -->
            <enum name="multiply" value="14" />
            <!-- [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc] -->
            <enum name="screen" value="15" />
            <!-- [Da, Sc * Da + (1 - Sa) * Dc] -->
            <enum name="src_atop" value="9" />
        </attr>
    </declare-styleable>

    <!-- Drawable used to render several states. Each state is represented by
@@ -4344,6 +4357,8 @@
        <!-- Indicates if the drawable needs to be mirrored when its layout direction is
             RTL (right-to-left). -->
        <attr name="autoMirrored" />
        <attr name="colorFilterColor" />
        <attr name="colorFilterMode" />
    </declare-styleable>

    <!-- Drawable used to draw 9-patches. -->
@@ -4357,6 +4372,8 @@
        <!-- Indicates if the drawable needs to be mirrored when its layout direction is
             RTL (right-to-left). -->
        <attr name="autoMirrored" />
        <attr name="colorFilterColor" />
        <attr name="colorFilterMode" />
    </declare-styleable>

    <!-- Drawable used to draw a single color. -->
+2 −0
Original line number Diff line number Diff line
@@ -2097,6 +2097,8 @@
  <public type="attr" name="windowContentTransitions" />
  <public type="attr" name="windowContentTransitionManager" />
  <public type="attr" name="translationZ" />
  <public type="attr" name="colorFilterColor" />
  <public type="attr" name="colorFilterMode" />

  <public type="style" name="Widget.Holo.FragmentBreadCrumbs" />
  <public type="style" name="Widget.Holo.Light.FragmentBreadCrumbs" />
+16 −1
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@ import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Xfermode;
@@ -33,6 +35,7 @@ import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.LayoutDirection;
import android.view.Gravity;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

@@ -579,6 +582,18 @@ public class BitmapDrawable extends Drawable {
        setAutoMirrored(a.getBoolean(com.android.internal.R.styleable.BitmapDrawable_autoMirrored,
                false));

        if (a.hasValue(com.android.internal.R.styleable.BitmapDrawable_colorFilterColor)) {
            final int colorFilterColor = a.getColor(
                    com.android.internal.R.styleable.BitmapDrawable_colorFilterColor, 0);
            final int modeValue = a.getInt(
                    com.android.internal.R.styleable.BitmapDrawable_colorFilterMode,
                    Mode.MULTIPLY.ordinal());
            final Mode mode = Drawable.parseColorFilterMode(modeValue);
            if (mode != null) {
                setColorFilter(colorFilterColor, mode);
            }
        }

        final Paint paint = mBitmapState.mPaint;
        paint.setAntiAlias(a.getBoolean(com.android.internal.R.styleable.BitmapDrawable_antialias,
                paint.isAntiAlias()));
@@ -647,7 +662,7 @@ public class BitmapDrawable extends Drawable {
        }

        BitmapState(BitmapState bitmapState) {
            this(bitmapState.mBitmap);
            mBitmap = bitmapState.mBitmap;
            mChangingConfigurations = bitmapState.mChangingConfigurations;
            mGravity = bitmapState.mGravity;
            mTileModeX = bitmapState.mTileModeX;
+21 −6
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@ import java.util.Arrays;
 *     through the {@link Callback} interface.  All clients should support this
 *     interface (via {@link #setCallback}) so that animations will work.  A
 *     simple way to do this is through the system facilities such as
 *     {@link android.view.View#setBackgroundDrawable(Drawable)} and
 *     {@link android.view.View#setBackground(Drawable)} and
 *     {@link android.widget.ImageView}.
 * </ul>
 *
@@ -438,8 +438,8 @@ public abstract class Drawable {
    }

    /**
     * Specify an optional colorFilter for the drawable. Pass null to remove
     * any filters.
     * Specify an optional color filter for the drawable. Pass null to remove
     * any existing color filter.
     */
    public abstract void setColorFilter(ColorFilter cf);

@@ -453,13 +453,16 @@ public abstract class Drawable {
    }

    /**
     * Specify a color and porterduff mode to be the colorfilter for this
     * Specify a color and Porter-Duff mode to be the color filter for this
     * drawable.
     */
    public void setColorFilter(int color, PorterDuff.Mode mode) {
        setColorFilter(new PorterDuffColorFilter(color, mode));
    }

    /**
     * Removes the color filter for this drawable.
     */
    public void clearColorFilter() {
        setColorFilter(null);
    }
@@ -1089,7 +1092,7 @@ public abstract class Drawable {

    /**
     * Return a {@link ConstantState} instance that holds the shared state of this Drawable.
     *q
     *
     * @return The ConstantState associated to that Drawable.
     * @see ConstantState
     * @see Drawable#mutate()
@@ -1107,5 +1110,17 @@ public abstract class Drawable {

        return new BitmapDrawable(res, bm);
    }

    /**
     * Parses a {@link android.graphics.PorterDuff.Mode} from a colorFilterMode
     * attribute's enum value.
     */
    static PorterDuff.Mode parseColorFilterMode(int value) {
        final PorterDuff.Mode[] modes = PorterDuff.Mode.values();
        if (value >= 0 && value < modes.length) {
            return modes[value];
        }
        return null;
    }
}
Loading