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

Commit b1af7f3d authored by Chet Haase's avatar Chet Haase
Browse files

add getAlpha() to Drawable

Drawable has setAlpha(int), but no getAlpha() (although some subclasses have added the
method). This makes it more tedious to use the property. For example, animations that wish to
animate this property must explicitly give it a start value since this value cannot be queried
from the object.

The trick is that setAlpha(int) is abstract, only implemented by subclasses. We cannot take this
approach for getAlpha(), as we would break all subclasses of Drawable until they implemented the
method. Instead, we'll add a default method which returns an invalid value, making it easier for
clients of the method to detect whether the value is valid.

All subclasses of Drawble in frameworks have been changed to add an override of getAlpha() when
appropriate.

Issue #7485875 Drawables is missing getAlpha()

Change-Id: I06b6e35f1a56d202838eca44759c85c82595020a
parent 1ccdf0ee
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -9442,7 +9442,6 @@ package android.graphics.drawable {
    ctor public ColorDrawable();
    ctor public ColorDrawable(int);
    method public void draw(android.graphics.Canvas);
    method public int getAlpha();
    method public int getColor();
    method public int getOpacity();
    method public void setAlpha(int);
@@ -9462,6 +9461,7 @@ package android.graphics.drawable {
    method public static android.graphics.drawable.Drawable createFromXml(android.content.res.Resources, org.xmlpull.v1.XmlPullParser) throws java.io.IOException, org.xmlpull.v1.XmlPullParserException;
    method public static android.graphics.drawable.Drawable createFromXmlInner(android.content.res.Resources, org.xmlpull.v1.XmlPullParser, android.util.AttributeSet) throws java.io.IOException, org.xmlpull.v1.XmlPullParserException;
    method public abstract void draw(android.graphics.Canvas);
    method public int getAlpha();
    method public final android.graphics.Rect getBounds();
    method public android.graphics.drawable.Drawable.Callback getCallback();
    method public int getChangingConfigurations();
+6 −0
Original line number Diff line number Diff line
@@ -225,6 +225,12 @@ public class ScrollBarDrawable extends Drawable {
        mHorizontalThumb.setAlpha(alpha);
    }

    @Override
    public int getAlpha() {
        // All elements should have same alpha, just return one of them
        return mVerticalThumb.getAlpha();
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        if (mVerticalTrack != null) {
+0 −30
Original line number Diff line number Diff line
@@ -46,36 +46,6 @@ public class TargetDrawable {
    private boolean mEnabled = true;
    private final int mResourceId;

    /* package */ static class DrawableWithAlpha extends Drawable {
        private float mAlpha = 1.0f;
        private Drawable mRealDrawable;
        public DrawableWithAlpha(Drawable realDrawable) {
            mRealDrawable = realDrawable;
        }
        public void setAlpha(float alpha) {
            mAlpha = alpha;
        }
        public float getAlpha() {
            return mAlpha;
        }
        public void draw(Canvas canvas) {
            mRealDrawable.setAlpha((int) Math.round(mAlpha * 255f));
            mRealDrawable.draw(canvas);
        }
        @Override
        public void setAlpha(int alpha) {
            mRealDrawable.setAlpha(alpha);
        }
        @Override
        public void setColorFilter(ColorFilter cf) {
            mRealDrawable.setColorFilter(cf);
        }
        @Override
        public int getOpacity() {
            return mRealDrawable.getOpacity();
        }
    }

    public TargetDrawable(Resources res, int resId) {
        mResourceId = resId;
        setDrawable(res, resId);
+5 −0
Original line number Diff line number Diff line
@@ -152,6 +152,11 @@ public class AnimatedRotateDrawable extends Drawable implements Drawable.Callbac
        mState.mDrawable.setAlpha(alpha);
    }

    @Override
    public int getAlpha() {
        return mState.mDrawable.getAlpha();
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mState.mDrawable.setColorFilter(cf);
+5 −0
Original line number Diff line number Diff line
@@ -455,6 +455,11 @@ public class BitmapDrawable extends Drawable {
        }
    }

    @Override
    public int getAlpha() {
        return mBitmapState.mPaint.getAlpha();
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mBitmapState.mPaint.setColorFilter(cf);
Loading