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