ClipDrawable.java revision a426445dfdab43886dd894f2ba8a1d55bfcbb278
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 clips another Drawable based on this Drawable's current
35 * level value.  You can control how much the child Drawable gets clipped 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, by increasing the drawable's level with {@link
39 * android.graphics.drawable.Drawable#setLevel(int) setLevel()}.
40 * <p class="note"><strong>Note:</strong> The drawable is clipped completely and not visible when
41 * the level is 0 and fully revealed when the level is 10,000.</p>
42 *
43 * <p>It can be defined in an XML file with the <code>&lt;clip></code> element.  For more
44 * information, see the guide to <a
45 * href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a>.</p>
46 *
47 * @attr ref android.R.styleable#ClipDrawable_clipOrientation
48 * @attr ref android.R.styleable#ClipDrawable_gravity
49 * @attr ref android.R.styleable#ClipDrawable_drawable
50 */
51public class ClipDrawable extends Drawable implements Drawable.Callback {
52    private ClipState mClipState;
53    private final Rect mTmpRect = new Rect();
54
55    public static final int HORIZONTAL = 1;
56    public static final int VERTICAL = 2;
57
58    ClipDrawable() {
59        this(null, null);
60    }
61
62    /**
63     * @param orientation Bitwise-or of {@link #HORIZONTAL} and/or {@link #VERTICAL}
64     */
65    public ClipDrawable(Drawable drawable, int gravity, int orientation) {
66        this(null, null);
67
68        mClipState.mDrawable = drawable;
69        mClipState.mGravity = gravity;
70        mClipState.mOrientation = orientation;
71
72        if (drawable != null) {
73            drawable.setCallback(this);
74        }
75    }
76
77    @Override
78    public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme)
79            throws XmlPullParserException, IOException {
80        super.inflate(r, parser, attrs, theme);
81
82        int type;
83
84        TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.ClipDrawable);
85
86        int orientation = a.getInt(
87                com.android.internal.R.styleable.ClipDrawable_clipOrientation,
88                HORIZONTAL);
89        int g = a.getInt(com.android.internal.R.styleable.ClipDrawable_gravity, Gravity.LEFT);
90        Drawable dr = a.getDrawable(com.android.internal.R.styleable.ClipDrawable_drawable);
91
92        a.recycle();
93
94        final int outerDepth = parser.getDepth();
95        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
96                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
97            if (type != XmlPullParser.START_TAG) {
98                continue;
99            }
100            dr = Drawable.createFromXmlInner(r, parser, attrs, theme);
101        }
102
103        if (dr == null) {
104            throw new IllegalArgumentException("No drawable specified for <clip>");
105        }
106
107        mClipState.mDrawable = dr;
108        mClipState.mOrientation = orientation;
109        mClipState.mGravity = g;
110
111        dr.setCallback(this);
112    }
113
114    // overrides from Drawable.Callback
115
116    @Override
117    public void invalidateDrawable(Drawable who) {
118        final Callback callback = getCallback();
119        if (callback != null) {
120            callback.invalidateDrawable(this);
121        }
122    }
123
124    @Override
125    public void scheduleDrawable(Drawable who, Runnable what, long when) {
126        final Callback callback = getCallback();
127        if (callback != null) {
128            callback.scheduleDrawable(this, what, when);
129        }
130    }
131
132    @Override
133    public void unscheduleDrawable(Drawable who, Runnable what) {
134        final Callback callback = getCallback();
135        if (callback != null) {
136            callback.unscheduleDrawable(this, what);
137        }
138    }
139
140    // overrides from Drawable
141
142    @Override
143    public int getChangingConfigurations() {
144        return super.getChangingConfigurations()
145                | mClipState.mChangingConfigurations
146                | mClipState.mDrawable.getChangingConfigurations();
147    }
148
149    @Override
150    public boolean getPadding(Rect padding) {
151        // XXX need to adjust padding!
152        return mClipState.mDrawable.getPadding(padding);
153    }
154
155    @Override
156    public boolean setVisible(boolean visible, boolean restart) {
157        mClipState.mDrawable.setVisible(visible, restart);
158        return super.setVisible(visible, restart);
159    }
160
161    @Override
162    public void setAlpha(int alpha) {
163        mClipState.mDrawable.setAlpha(alpha);
164    }
165
166    @Override
167    public int getAlpha() {
168        return mClipState.mDrawable.getAlpha();
169    }
170
171    @Override
172    public void setColorFilter(ColorFilter cf) {
173        mClipState.mDrawable.setColorFilter(cf);
174    }
175
176    @Override
177    public void setTintList(ColorStateList tint) {
178        mClipState.mDrawable.setTintList(tint);
179    }
180
181    @Override
182    public void setTintMode(Mode tintMode) {
183        mClipState.mDrawable.setTintMode(tintMode);
184    }
185
186    @Override
187    public int getOpacity() {
188        return mClipState.mDrawable.getOpacity();
189    }
190
191    @Override
192    public boolean isStateful() {
193        return mClipState.mDrawable.isStateful();
194    }
195
196    @Override
197    protected boolean onStateChange(int[] state) {
198        return mClipState.mDrawable.setState(state);
199    }
200
201    @Override
202    protected boolean onLevelChange(int level) {
203        mClipState.mDrawable.setLevel(level);
204        invalidateSelf();
205        return true;
206    }
207
208    @Override
209    protected void onBoundsChange(Rect bounds) {
210        mClipState.mDrawable.setBounds(bounds);
211    }
212
213    @Override
214    public void draw(Canvas canvas) {
215
216        if (mClipState.mDrawable.getLevel() == 0) {
217            return;
218        }
219
220        final Rect r = mTmpRect;
221        final Rect bounds = getBounds();
222        int level = getLevel();
223        int w = bounds.width();
224        final int iw = 0; //mClipState.mDrawable.getIntrinsicWidth();
225        if ((mClipState.mOrientation & HORIZONTAL) != 0) {
226            w -= (w - iw) * (10000 - level) / 10000;
227        }
228        int h = bounds.height();
229        final int ih = 0; //mClipState.mDrawable.getIntrinsicHeight();
230        if ((mClipState.mOrientation & VERTICAL) != 0) {
231            h -= (h - ih) * (10000 - level) / 10000;
232        }
233        final int layoutDirection = getLayoutDirection();
234        Gravity.apply(mClipState.mGravity, w, h, bounds, r, layoutDirection);
235
236        if (w > 0 && h > 0) {
237            canvas.save();
238            canvas.clipRect(r);
239            mClipState.mDrawable.draw(canvas);
240            canvas.restore();
241        }
242    }
243
244    @Override
245    public int getIntrinsicWidth() {
246        return mClipState.mDrawable.getIntrinsicWidth();
247    }
248
249    @Override
250    public int getIntrinsicHeight() {
251        return mClipState.mDrawable.getIntrinsicHeight();
252    }
253
254    @Override
255    public ConstantState getConstantState() {
256        if (mClipState.canConstantState()) {
257            mClipState.mChangingConfigurations = getChangingConfigurations();
258            return mClipState;
259        }
260        return null;
261    }
262
263    /** @hide */
264    @Override
265    public void setLayoutDirection(int layoutDirection) {
266        mClipState.mDrawable.setLayoutDirection(layoutDirection);
267        super.setLayoutDirection(layoutDirection);
268    }
269
270    final static class ClipState extends ConstantState {
271        Drawable mDrawable;
272        int mChangingConfigurations;
273        int mOrientation;
274        int mGravity;
275
276        private boolean mCheckedConstantState;
277        private boolean mCanConstantState;
278
279        ClipState(ClipState orig, ClipDrawable owner, Resources res) {
280            if (orig != null) {
281                if (res != null) {
282                    mDrawable = orig.mDrawable.getConstantState().newDrawable(res);
283                } else {
284                    mDrawable = orig.mDrawable.getConstantState().newDrawable();
285                }
286                mDrawable.setCallback(owner);
287                mDrawable.setLayoutDirection(orig.mDrawable.getLayoutDirection());
288                mOrientation = orig.mOrientation;
289                mGravity = orig.mGravity;
290                mCheckedConstantState = mCanConstantState = true;
291            }
292        }
293
294        @Override
295        public Drawable newDrawable() {
296            return new ClipDrawable(this, null);
297        }
298
299        @Override
300        public Drawable newDrawable(Resources res) {
301            return new ClipDrawable(this, res);
302        }
303
304        @Override
305        public int getChangingConfigurations() {
306            return mChangingConfigurations;
307        }
308
309        boolean canConstantState() {
310            if (!mCheckedConstantState) {
311                mCanConstantState = mDrawable.getConstantState() != null;
312                mCheckedConstantState = true;
313            }
314
315            return mCanConstantState;
316        }
317    }
318
319    private ClipDrawable(ClipState state, Resources res) {
320        mClipState = new ClipState(state, this, res);
321    }
322}
323
324