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

Commit 09ceac29 authored by Alan Viverette's avatar Alan Viverette
Browse files

Clean up tint API

Change-Id: I56056e7596840c563f5332922d63e8893a0cac17
parent 25739837
Loading
Loading
Loading
Loading
+23 −24
Original line number Diff line number Diff line
@@ -27,10 +27,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.PorterDuffColorFilter;
import android.graphics.drawable.GradientDrawable.GradientState;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Xfermode;
@@ -573,19 +571,12 @@ public class BitmapDrawable extends Drawable {
     *            clear the tint
     */
    public void setTint(ColorStateList tint) {
        if (mBitmapState.mTint != tint) {
            mBitmapState.mTint = tint;
        if (mTintFilter == null) {
            if (tint != null) {
                final int color = tint.getColorForState(getState(), 0);
                mTintFilter = new PorterDuffColorFilter(color, mBitmapState.mTintMode);
            }
        } else {
            if (tint == null) {
                mTintFilter = null;
            }
        }
            updateTintFilter();
            invalidateSelf();
        }
    }

    /**
     * Returns the tint color for this drawable.
@@ -604,21 +595,29 @@ public class BitmapDrawable extends Drawable {
     * @hide Pending finalization of supported Modes
     */
    public void setTintMode(Mode tintMode) {
        if (mBitmapState.mTintMode != tintMode) {
            mBitmapState.mTintMode = tintMode;
        if (mTintFilter != null) {
            mTintFilter.setMode(tintMode);
        }
            updateTintFilter();
            invalidateSelf();
        }
    }

    /**
     * Returns the blending mode used to apply tint.
     *
     * @return The Porter-Duff blending mode used to apply tint.
     * @hide Pending finalization of supported Modes
     * Ensures the tint filter is consistent with the current tint color and
     * mode.
     */
    public Mode getTintMode() {
        return mBitmapState.mTintMode;
    private void updateTintFilter() {
        final ColorStateList tint = mBitmapState.mTint;
        final Mode tintMode = mBitmapState.mTintMode;
        if (tint != null && tintMode != null) {
            if (mTintFilter == null) {
                mTintFilter = new PorterDuffColorFilter(0, tintMode);
            } else {
                mTintFilter.setMode(tintMode);
            }
        } else {
            mTintFilter = null;
        }
    }

    /**
@@ -756,7 +755,7 @@ public class BitmapDrawable extends Drawable {
    final static class BitmapState extends ConstantState {
        Bitmap mBitmap;
        ColorStateList mTint;
        Mode mTintMode;
        Mode mTintMode = Mode.SRC_IN;
        int mChangingConfigurations;
        int mGravity = Gravity.FILL;
        Paint mPaint = new Paint(DEFAULT_PAINT_FLAGS);
+24 −23
Original line number Diff line number Diff line
@@ -27,10 +27,10 @@ import android.graphics.Insets;
import android.graphics.NinePatch;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.PorterDuff.Mode;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.LayoutDirection;
@@ -326,19 +326,12 @@ public class NinePatchDrawable extends Drawable {
     *            clear the tint
     */
    public void setTint(ColorStateList tint) {
        if (mNinePatchState.mTint != tint) {
            mNinePatchState.mTint = tint;
        if (mTintFilter == null) {
            if (tint != null) {
                final int color = tint.getColorForState(getState(), 0);
                mTintFilter = new PorterDuffColorFilter(color, mNinePatchState.mTintMode);
            }
        } else {
            if (tint == null) {
                mTintFilter = null;
            }
        }
            updateTintFilter();
            invalidateSelf();
        }
    }

    /**
     * Returns the tint color for this drawable.
@@ -357,21 +350,29 @@ public class NinePatchDrawable extends Drawable {
     * @hide Pending finalization of supported Modes
     */
    public void setTintMode(Mode tintMode) {
        if (mNinePatchState.mTintMode != tintMode) {
            mNinePatchState.mTintMode = tintMode;
        if (mTintFilter != null) {
            mTintFilter.setMode(tintMode);
        }
            updateTintFilter();
            invalidateSelf();
        }
    }

    /**
     * Returns the blending mode used to apply tint.
     *
     * @return The Porter-Duff blending mode used to apply tint.
     * @hide Pending finalization of supported Modes
     * Ensures the tint filter is consistent with the current tint color and
     * mode.
     */
    public Mode getTintMode() {
        return mNinePatchState.mTintMode;
    private void updateTintFilter() {
        final ColorStateList tint = mNinePatchState.mTint;
        final Mode tintMode = mNinePatchState.mTintMode;
        if (tint != null && tintMode != null) {
            if (mTintFilter == null) {
                mTintFilter = new PorterDuffColorFilter(0, tintMode);
            } else {
                mTintFilter.setMode(tintMode);
            }
        } else {
            mTintFilter = null;
        }
    }

    @Override
@@ -563,7 +564,7 @@ public class NinePatchDrawable extends Drawable {
    final static class NinePatchState extends ConstantState {
        NinePatch mNinePatch;
        ColorStateList mTint;
        Mode mTintMode;
        Mode mTintMode = Mode.SRC_IN;
        Rect mPadding;
        Insets mOpticalInsets;
        boolean mDither;
+115 −98
Original line number Diff line number Diff line
@@ -16,13 +16,18 @@

package android.graphics.drawable;

import android.graphics.*;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable.BitmapState;
import android.graphics.drawable.shapes.Shape;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.shapes.Shape;
import android.util.AttributeSet;

import org.xmlpull.v1.XmlPullParser;
@@ -31,22 +36,24 @@ import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;

/**
 * A Drawable object that draws primitive shapes. 
 * A ShapeDrawable takes a {@link android.graphics.drawable.shapes.Shape}
 * object and manages its presence on the screen. If no Shape is given, then
 * the ShapeDrawable will default to a 
 * {@link android.graphics.drawable.shapes.RectShape}.
 *
 * <p>This object can be defined in an XML file with the <code>&lt;shape></code> element.</p>
 *
 * <div class="special reference">
 * <h3>Developer Guides</h3>
 * <p>For more information about how to use ShapeDrawable, read the
 * <a href="{@docRoot}guide/topics/graphics/2d-graphics.html#shape-drawable">
 * Canvas and Drawables</a> document. For more information about defining a ShapeDrawable in
 * XML, read the
 * <a href="{@docRoot}guide/topics/resources/drawable-resource.html#Shape">Drawable Resources</a>
 * document.</p></div>
 * A Drawable object that draws primitive shapes. A ShapeDrawable takes a
 * {@link android.graphics.drawable.shapes.Shape} object and manages its
 * presence on the screen. If no Shape is given, then the ShapeDrawable will
 * default to a {@link android.graphics.drawable.shapes.RectShape}.
 * <p>
 * This object can be defined in an XML file with the <code>&lt;shape></code>
 * element.
 * </p>
 * <div class="special reference"> <h3>Developer Guides</h3>
 * <p>
 * For more information about how to use ShapeDrawable, read the <a
 * href="{@docRoot}guide/topics/graphics/2d-graphics.html#shape-drawable">
 * Canvas and Drawables</a> document. For more information about defining a
 * ShapeDrawable in XML, read the <a href="{@docRoot}
 * guide/topics/resources/drawable-resource.html#Shape">Drawable Resources</a>
 * document.
 * </p>
 * </div>
 *
 * @attr ref android.R.styleable#ShapeDrawablePadding_left
 * @attr ref android.R.styleable#ShapeDrawablePadding_top
@@ -149,8 +156,8 @@ public class ShapeDrawable extends Drawable {
    }

    /**
     * Sets padding for this shape, defined by a Rect object.
     * Define the padding in the Rect object as: left, top, right, bottom.
     * Sets padding for this shape, defined by a Rect object. Define the padding
     * in the Rect object as: left, top, right, bottom.
     */
    public void setPadding(Rect padding) {
        if (padding == null) {
@@ -210,9 +217,9 @@ public class ShapeDrawable extends Drawable {
    }

    /**
     * Called from the drawable's draw() method after the canvas has been set
     * to draw the shape at (0,0). Subclasses can override for special effects
     * such as multiple layers, stroking, etc.
     * Called from the drawable's draw() method after the canvas has been set to
     * draw the shape at (0,0). Subclasses can override for special effects such
     * as multiple layers, stroking, etc.
     */
    protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
        shape.draw(canvas, paint);
@@ -238,7 +245,8 @@ public class ShapeDrawable extends Drawable {
            }

            if (state.mShape != null) {
                // need the save both for the translate, and for the (unknown) Shape
                // need the save both for the translate, and for the (unknown)
                // Shape
                final int count = canvas.save();
                canvas.translate(r.left, r.top);
                onDraw(state.mShape, canvas, paint);
@@ -267,10 +275,11 @@ public class ShapeDrawable extends Drawable {
     * also has a color in its paint, which has an alpha as well. These two
     * values are automatically combined during drawing. Thus if the color's
     * alpha is 75% (i.e. 192) and the drawable's alpha is 50% (i.e. 128), then
     * the combined alpha that will be used during drawing will be 37.5%
     * (i.e. 96).
     * the combined alpha that will be used during drawing will be 37.5% (i.e.
     * 96).
     */
    @Override public void setAlpha(int alpha) {
    @Override
    public void setAlpha(int alpha) {
        mShapeState.mAlpha = alpha;
        invalidateSelf();
    }
@@ -290,19 +299,12 @@ public class ShapeDrawable extends Drawable {
     *            clear the tint
     */
    public void setTint(ColorStateList tint) {
        if (mShapeState.mTint != tint) {
            mShapeState.mTint = tint;
        if (mTintFilter == null) {
            if (tint != null) {
                final int color = tint.getColorForState(getState(), 0);
                mTintFilter = new PorterDuffColorFilter(color, mShapeState.mTintMode);
            }
        } else {
            if (tint == null) {
                mTintFilter = null;
            }
        }
            updateTintFilter();
            invalidateSelf();
        }
    }

    /**
     * Returns the tint color for this drawable.
@@ -321,11 +323,29 @@ public class ShapeDrawable extends Drawable {
     * @hide Pending finalization of supported Modes
     */
    public void setTintMode(Mode tintMode) {
        if (mShapeState.mTintMode != tintMode) {
            mShapeState.mTintMode = tintMode;
        if (mTintFilter != null) {
            updateTintFilter();
            invalidateSelf();
        }
    }

    /**
     * Ensures the tint filter is consistent with the current tint color and
     * mode.
     */
    private void updateTintFilter() {
        final ColorStateList tint = mShapeState.mTint;
        final Mode tintMode = mShapeState.mTintMode;
        if (tint != null && tintMode != null) {
            if (mTintFilter == null) {
                mTintFilter = new PorterDuffColorFilter(0, tintMode);
            } else {
                mTintFilter.setMode(tintMode);
            }
        invalidateSelf();
        } else {
            mTintFilter = null;
        }
    }

    /**
@@ -397,8 +417,8 @@ public class ShapeDrawable extends Drawable {
    }

    /**
     * Subclasses override this to parse custom subelements.
     * If you handle it, return true, else return <em>super.inflateTag(...)</em>.
     * Subclasses override this to parse custom subelements. If you handle it,
     * return true, else return <em>super.inflateTag(...)</em>.
     */
    protected boolean inflateTag(String name, Resources r, XmlPullParser parser,
            AttributeSet attrs) {
@@ -511,7 +531,7 @@ public class ShapeDrawable extends Drawable {
        Paint mPaint;
        Shape mShape;
        ColorStateList mTint;
        Mode mTintMode;
        Mode mTintMode = Mode.SRC_IN;
        Rect mPadding;
        int mIntrinsicWidth;
        int mIntrinsicHeight;
@@ -553,18 +573,16 @@ public class ShapeDrawable extends Drawable {
    /**
     * Base class defines a factory object that is called each time the drawable
     * is resized (has a new width or height). Its resize() method returns a
     * corresponding shader, or null.
     * Implement this class if you'd like your ShapeDrawable to use a special
     * {@link android.graphics.Shader}, such as a 
     * corresponding shader, or null. Implement this class if you'd like your
     * ShapeDrawable to use a special {@link android.graphics.Shader}, such as a
     * {@link android.graphics.LinearGradient}.
     * 
     */
    public static abstract class ShaderFactory {
        /**
         * Returns the Shader to be drawn when a Drawable is drawn.
         * The dimensions of the Drawable are passed because they may be needed
         * to adjust how the Shader is configured for drawing.
         * This is called by ShapeDrawable.setShape().
         * Returns the Shader to be drawn when a Drawable is drawn. The
         * dimensions of the Drawable are passed because they may be needed to
         * adjust how the Shader is configured for drawing. This is called by
         * ShapeDrawable.setShape().
         *
         * @param width the width of the Drawable being drawn
         * @param height the heigh of the Drawable being drawn
@@ -577,4 +595,3 @@ public class ShapeDrawable extends Drawable {
    // resize params (e.g. scaletofit, etc.). This could be used to scale
    // a bitmap to fill the bounds without needing any other special casing.
}