ScaleDrawable.java revision b3c56086d802ae28888dd97ba1f49bd6cee0b673
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics.drawable;
18
19import org.xmlpull.v1.XmlPullParser;
20import org.xmlpull.v1.XmlPullParserException;
21
22import android.content.res.ColorStateList;
23import android.content.res.Resources;
24import android.content.res.TypedArray;
25import android.content.res.Resources.Theme;
26import android.graphics.*;
27import android.graphics.PorterDuff.Mode;
28import android.view.Gravity;
29import android.util.AttributeSet;
30
31import java.io.IOException;
32
33/**
34 * A Drawable that changes the size of another Drawable based on its current
35 * level value.  You can control how much the child Drawable changes in width
36 * and height based on the level, as well as a gravity to control where it is
37 * placed in its overall container.  Most often used to implement things like
38 * progress bars.
39 *
40 * <p>It can be defined in an XML file with the <code>&lt;scale></code> element. For more
41 * information, see the guide to <a
42 * href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a>.</p>
43 *
44 * @attr ref android.R.styleable#ScaleDrawable_scaleWidth
45 * @attr ref android.R.styleable#ScaleDrawable_scaleHeight
46 * @attr ref android.R.styleable#ScaleDrawable_scaleGravity
47 * @attr ref android.R.styleable#ScaleDrawable_drawable
48 */
49public class ScaleDrawable extends Drawable implements Drawable.Callback {
50    private ScaleState mScaleState;
51    private boolean mMutated;
52    private final Rect mTmpRect = new Rect();
53
54    ScaleDrawable() {
55        this(null, null);
56    }
57
58    public ScaleDrawable(Drawable drawable, int gravity, float scaleWidth, float scaleHeight) {
59        this(null, null);
60
61        mScaleState.mDrawable = drawable;
62        mScaleState.mGravity = gravity;
63        mScaleState.mScaleWidth = scaleWidth;
64        mScaleState.mScaleHeight = scaleHeight;
65
66        if (drawable != null) {
67            drawable.setCallback(this);
68        }
69    }
70
71    /**
72     * Returns the drawable scaled by this ScaleDrawable.
73     */
74    public Drawable getDrawable() {
75        return mScaleState.mDrawable;
76    }
77
78    private static float getPercent(TypedArray a, int name) {
79        String s = a.getString(name);
80        if (s != null) {
81            if (s.endsWith("%")) {
82                String f = s.substring(0, s.length() - 1);
83                return Float.parseFloat(f) / 100.0f;
84            }
85        }
86        return -1;
87    }
88
89    @Override
90    public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme)
91            throws XmlPullParserException, IOException {
92        super.inflate(r, parser, attrs, theme);
93
94        int type;
95
96        TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.ScaleDrawable);
97
98        float sw = getPercent(a, com.android.internal.R.styleable.ScaleDrawable_scaleWidth);
99        float sh = getPercent(a, com.android.internal.R.styleable.ScaleDrawable_scaleHeight);
100        int g = a.getInt(com.android.internal.R.styleable.ScaleDrawable_scaleGravity, Gravity.LEFT);
101        boolean min = a.getBoolean(
102                com.android.internal.R.styleable.ScaleDrawable_useIntrinsicSizeAsMinimum, false);
103        Drawable dr = a.getDrawable(com.android.internal.R.styleable.ScaleDrawable_drawable);
104
105        a.recycle();
106
107        final int outerDepth = parser.getDepth();
108        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
109                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
110            if (type != XmlPullParser.START_TAG) {
111                continue;
112            }
113            dr = Drawable.createFromXmlInner(r, parser, attrs, theme);
114        }
115
116        if (dr == null) {
117            throw new IllegalArgumentException("No drawable specified for <scale>");
118        }
119
120        mScaleState.mDrawable = dr;
121        mScaleState.mScaleWidth = sw;
122        mScaleState.mScaleHeight = sh;
123        mScaleState.mGravity = g;
124        mScaleState.mUseIntrinsicSizeAsMin = min;
125        if (dr != null) {
126            dr.setCallback(this);
127        }
128    }
129
130    // overrides from Drawable.Callback
131
132    public void invalidateDrawable(Drawable who) {
133        if (getCallback() != null) {
134            getCallback().invalidateDrawable(this);
135        }
136    }
137
138    public void scheduleDrawable(Drawable who, Runnable what, long when) {
139        if (getCallback() != null) {
140            getCallback().scheduleDrawable(this, what, when);
141        }
142    }
143
144    public void unscheduleDrawable(Drawable who, Runnable what) {
145        if (getCallback() != null) {
146            getCallback().unscheduleDrawable(this, what);
147        }
148    }
149
150    // overrides from Drawable
151
152    @Override
153    public void draw(Canvas canvas) {
154        if (mScaleState.mDrawable.getLevel() != 0)
155            mScaleState.mDrawable.draw(canvas);
156    }
157
158    @Override
159    public int getChangingConfigurations() {
160        return super.getChangingConfigurations()
161                | mScaleState.mChangingConfigurations
162                | mScaleState.mDrawable.getChangingConfigurations();
163    }
164
165    @Override
166    public boolean getPadding(Rect padding) {
167        // XXX need to adjust padding!
168        return mScaleState.mDrawable.getPadding(padding);
169    }
170
171    @Override
172    public boolean setVisible(boolean visible, boolean restart) {
173        mScaleState.mDrawable.setVisible(visible, restart);
174        return super.setVisible(visible, restart);
175    }
176
177    @Override
178    public void setAlpha(int alpha) {
179        mScaleState.mDrawable.setAlpha(alpha);
180    }
181
182    @Override
183    public int getAlpha() {
184        return mScaleState.mDrawable.getAlpha();
185    }
186
187    @Override
188    public void setColorFilter(ColorFilter cf) {
189        mScaleState.mDrawable.setColorFilter(cf);
190    }
191
192    @Override
193    public void setTint(ColorStateList tint, Mode tintMode) {
194        mScaleState.mDrawable.setTint(tint, tintMode);
195    }
196
197    @Override
198    public int getOpacity() {
199        return mScaleState.mDrawable.getOpacity();
200    }
201
202    @Override
203    public boolean isStateful() {
204        return mScaleState.mDrawable.isStateful();
205    }
206
207    @Override
208    protected boolean onStateChange(int[] state) {
209        boolean changed = mScaleState.mDrawable.setState(state);
210        onBoundsChange(getBounds());
211        return changed;
212    }
213
214    @Override
215    protected boolean onLevelChange(int level) {
216        mScaleState.mDrawable.setLevel(level);
217        onBoundsChange(getBounds());
218        invalidateSelf();
219        return true;
220    }
221
222    @Override
223    protected void onBoundsChange(Rect bounds) {
224        final Rect r = mTmpRect;
225        final boolean min = mScaleState.mUseIntrinsicSizeAsMin;
226        int level = getLevel();
227        int w = bounds.width();
228        if (mScaleState.mScaleWidth > 0) {
229            final int iw = min ? mScaleState.mDrawable.getIntrinsicWidth() : 0;
230            w -= (int) ((w - iw) * (10000 - level) * mScaleState.mScaleWidth / 10000);
231        }
232        int h = bounds.height();
233        if (mScaleState.mScaleHeight > 0) {
234            final int ih = min ? mScaleState.mDrawable.getIntrinsicHeight() : 0;
235            h -= (int) ((h - ih) * (10000 - level) * mScaleState.mScaleHeight / 10000);
236        }
237        final int layoutDirection = getLayoutDirection();
238        Gravity.apply(mScaleState.mGravity, w, h, bounds, r, layoutDirection);
239
240        if (w > 0 && h > 0) {
241            mScaleState.mDrawable.setBounds(r.left, r.top, r.right, r.bottom);
242        }
243    }
244
245    @Override
246    public int getIntrinsicWidth() {
247        return mScaleState.mDrawable.getIntrinsicWidth();
248    }
249
250    @Override
251    public int getIntrinsicHeight() {
252        return mScaleState.mDrawable.getIntrinsicHeight();
253    }
254
255    @Override
256    public ConstantState getConstantState() {
257        if (mScaleState.canConstantState()) {
258            mScaleState.mChangingConfigurations = getChangingConfigurations();
259            return mScaleState;
260        }
261        return null;
262    }
263
264    @Override
265    public Drawable mutate() {
266        if (!mMutated && super.mutate() == this) {
267            mScaleState.mDrawable.mutate();
268            mMutated = true;
269        }
270        return this;
271    }
272
273    final static class ScaleState extends ConstantState {
274        Drawable mDrawable;
275        int mChangingConfigurations;
276        float mScaleWidth;
277        float mScaleHeight;
278        int mGravity;
279        boolean mUseIntrinsicSizeAsMin;
280
281        private boolean mCheckedConstantState;
282        private boolean mCanConstantState;
283
284        ScaleState(ScaleState orig, ScaleDrawable owner, Resources res) {
285            if (orig != null) {
286                if (res != null) {
287                    mDrawable = orig.mDrawable.getConstantState().newDrawable(res);
288                } else {
289                    mDrawable = orig.mDrawable.getConstantState().newDrawable();
290                }
291                mDrawable.setCallback(owner);
292                mDrawable.setLayoutDirection(orig.mDrawable.getLayoutDirection());
293                mScaleWidth = orig.mScaleWidth;
294                mScaleHeight = orig.mScaleHeight;
295                mGravity = orig.mGravity;
296                mUseIntrinsicSizeAsMin = orig.mUseIntrinsicSizeAsMin;
297                mCheckedConstantState = mCanConstantState = true;
298            }
299        }
300
301        @Override
302        public Drawable newDrawable() {
303            return new ScaleDrawable(this, null);
304        }
305
306        @Override
307        public Drawable newDrawable(Resources res) {
308            return new ScaleDrawable(this, res);
309        }
310
311        @Override
312        public int getChangingConfigurations() {
313            return mChangingConfigurations;
314        }
315
316        boolean canConstantState() {
317            if (!mCheckedConstantState) {
318                mCanConstantState = mDrawable.getConstantState() != null;
319                mCheckedConstantState = true;
320            }
321
322            return mCanConstantState;
323        }
324    }
325
326    private ScaleDrawable(ScaleState state, Resources res) {
327        mScaleState = new ScaleState(state, this, res);
328    }
329}
330
331