ScaleDrawable.java revision 8d71769b9bc3d0e72b26217d9059ce4473a91fe5
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        Drawable dr = a.getDrawable(com.android.internal.R.styleable.ScaleDrawable_drawable);
99
100        a.recycle();
101
102        final int outerDepth = parser.getDepth();
103        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
104                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
105            if (type != XmlPullParser.START_TAG) {
106                continue;
107            }
108            dr = Drawable.createFromXmlInner(r, parser, attrs);
109        }
110
111        if (dr == null) {
112            throw new IllegalArgumentException("No drawable specified for <scale>");
113        }
114
115        mScaleState.mDrawable = dr;
116        mScaleState.mScaleWidth = sw;
117        mScaleState.mScaleHeight = sh;
118        mScaleState.mGravity = g;
119        if (dr != null) {
120            dr.setCallback(this);
121        }
122    }
123
124    // overrides from Drawable.Callback
125
126    public void invalidateDrawable(Drawable who) {
127        if (mCallback != null) {
128            mCallback.invalidateDrawable(this);
129        }
130    }
131
132    public void scheduleDrawable(Drawable who, Runnable what, long when) {
133        if (mCallback != null) {
134            mCallback.scheduleDrawable(this, what, when);
135        }
136    }
137
138    public void unscheduleDrawable(Drawable who, Runnable what) {
139        if (mCallback != null) {
140            mCallback.unscheduleDrawable(this, what);
141        }
142    }
143
144    // overrides from Drawable
145
146    @Override
147    public void draw(Canvas canvas) {
148        if (mScaleState.mDrawable.getLevel() != 0)
149            mScaleState.mDrawable.draw(canvas);
150    }
151
152    @Override
153    public int getChangingConfigurations() {
154        return super.getChangingConfigurations()
155                | mScaleState.mChangingConfigurations
156                | mScaleState.mDrawable.getChangingConfigurations();
157    }
158
159    @Override
160    public boolean getPadding(Rect padding) {
161        // XXX need to adjust padding!
162        return mScaleState.mDrawable.getPadding(padding);
163    }
164
165    @Override
166    public boolean setVisible(boolean visible, boolean restart) {
167        mScaleState.mDrawable.setVisible(visible, restart);
168        return super.setVisible(visible, restart);
169    }
170
171    @Override
172    public void setAlpha(int alpha) {
173        mScaleState.mDrawable.setAlpha(alpha);
174    }
175
176    @Override
177    public void setColorFilter(ColorFilter cf) {
178        mScaleState.mDrawable.setColorFilter(cf);
179    }
180
181    @Override
182    public int getOpacity() {
183        return mScaleState.mDrawable.getOpacity();
184    }
185
186    @Override
187    public boolean isStateful() {
188        return mScaleState.mDrawable.isStateful();
189    }
190
191    @Override
192    protected boolean onStateChange(int[] state) {
193        boolean changed = mScaleState.mDrawable.setState(state);
194        onBoundsChange(getBounds());
195        return changed;
196    }
197
198    @Override
199    protected boolean onLevelChange(int level) {
200        mScaleState.mDrawable.setLevel(level);
201        onBoundsChange(getBounds());
202        invalidateSelf();
203        return true;
204    }
205
206    @Override
207    protected void onBoundsChange(Rect bounds) {
208        final Rect r = mTmpRect;
209        int level = getLevel();
210        int w = bounds.width();
211        final int iw = 0; //mScaleState.mDrawable.getIntrinsicWidth();
212        if (mScaleState.mScaleWidth > 0) {
213            w -= (int) ((w - iw) * (10000 - level) * mScaleState.mScaleWidth / 10000);
214        }
215        int h = bounds.height();
216        final int ih = 0; //mScaleState.mDrawable.getIntrinsicHeight();
217        if (mScaleState.mScaleHeight > 0) {
218            h -= (int) ((h - ih) * (10000 - level) * mScaleState.mScaleHeight / 10000);
219        }
220        Gravity.apply(mScaleState.mGravity, w, h, bounds, r);
221
222        if (w > 0 && h > 0) {
223            mScaleState.mDrawable.setBounds(r.left, r.top, r.right, r.bottom);
224        }
225    }
226
227    @Override
228    public int getIntrinsicWidth() {
229        return mScaleState.mDrawable.getIntrinsicWidth();
230    }
231
232    @Override
233    public int getIntrinsicHeight() {
234        return mScaleState.mDrawable.getIntrinsicHeight();
235    }
236
237    @Override
238    public ConstantState getConstantState() {
239        if (mScaleState.canConstantState()) {
240            mScaleState.mChangingConfigurations = getChangingConfigurations();
241            return mScaleState;
242        }
243        return null;
244    }
245
246    @Override
247    public Drawable mutate() {
248        if (!mMutated && super.mutate() == this) {
249            mScaleState.mDrawable.mutate();
250            mMutated = true;
251        }
252        return this;
253    }
254
255    final static class ScaleState extends ConstantState {
256        Drawable mDrawable;
257        int mChangingConfigurations;
258        float mScaleWidth;
259        float mScaleHeight;
260        int mGravity;
261
262        private boolean mCheckedConstantState;
263        private boolean mCanConstantState;
264
265        ScaleState(ScaleState orig, ScaleDrawable owner, Resources res) {
266            if (orig != null) {
267                if (res != null) {
268                    mDrawable = orig.mDrawable.getConstantState().newDrawable(res);
269                } else {
270                    mDrawable = orig.mDrawable.getConstantState().newDrawable();
271                }
272                mDrawable.setCallback(owner);
273                mScaleWidth = orig.mScaleWidth;
274                mScaleHeight = orig.mScaleHeight;
275                mGravity = orig.mGravity;
276                mCheckedConstantState = mCanConstantState = true;
277            }
278        }
279
280        @Override
281        public Drawable newDrawable() {
282            return new ScaleDrawable(this, null);
283        }
284
285        @Override
286        public Drawable newDrawable(Resources res) {
287            return new ScaleDrawable(this, res);
288        }
289
290        @Override
291        public int getChangingConfigurations() {
292            return mChangingConfigurations;
293        }
294
295        boolean canConstantState() {
296            if (!mCheckedConstantState) {
297                mCanConstantState = mDrawable.getConstantState() != null;
298                mCheckedConstantState = true;
299            }
300
301            return mCanConstantState;
302        }
303    }
304
305    private ScaleDrawable(ScaleState state, Resources res) {
306        mScaleState = new ScaleState(state, this, res);
307    }
308}
309
310