ClipDrawable.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 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 setTint(ColorStateList tint, Mode tintMode) {
178        mClipState.mDrawable.setTint(tint, tintMode);
179    }
180
181    @Override
182    public int getOpacity() {
183        return mClipState.mDrawable.getOpacity();
184    }
185
186    @Override
187    public boolean isStateful() {
188        return mClipState.mDrawable.isStateful();
189    }
190
191    @Override
192    protected boolean onStateChange(int[] state) {
193        return mClipState.mDrawable.setState(state);
194    }
195
196    @Override
197    protected boolean onLevelChange(int level) {
198        mClipState.mDrawable.setLevel(level);
199        invalidateSelf();
200        return true;
201    }
202
203    @Override
204    protected void onBoundsChange(Rect bounds) {
205        mClipState.mDrawable.setBounds(bounds);
206    }
207
208    @Override
209    public void draw(Canvas canvas) {
210
211        if (mClipState.mDrawable.getLevel() == 0) {
212            return;
213        }
214
215        final Rect r = mTmpRect;
216        final Rect bounds = getBounds();
217        int level = getLevel();
218        int w = bounds.width();
219        final int iw = 0; //mClipState.mDrawable.getIntrinsicWidth();
220        if ((mClipState.mOrientation & HORIZONTAL) != 0) {
221            w -= (w - iw) * (10000 - level) / 10000;
222        }
223        int h = bounds.height();
224        final int ih = 0; //mClipState.mDrawable.getIntrinsicHeight();
225        if ((mClipState.mOrientation & VERTICAL) != 0) {
226            h -= (h - ih) * (10000 - level) / 10000;
227        }
228        final int layoutDirection = getLayoutDirection();
229        Gravity.apply(mClipState.mGravity, w, h, bounds, r, layoutDirection);
230
231        if (w > 0 && h > 0) {
232            canvas.save();
233            canvas.clipRect(r);
234            mClipState.mDrawable.draw(canvas);
235            canvas.restore();
236        }
237    }
238
239    @Override
240    public int getIntrinsicWidth() {
241        return mClipState.mDrawable.getIntrinsicWidth();
242    }
243
244    @Override
245    public int getIntrinsicHeight() {
246        return mClipState.mDrawable.getIntrinsicHeight();
247    }
248
249    @Override
250    public ConstantState getConstantState() {
251        if (mClipState.canConstantState()) {
252            mClipState.mChangingConfigurations = getChangingConfigurations();
253            return mClipState;
254        }
255        return null;
256    }
257
258    /** @hide */
259    @Override
260    public void setLayoutDirection(int layoutDirection) {
261        mClipState.mDrawable.setLayoutDirection(layoutDirection);
262        super.setLayoutDirection(layoutDirection);
263    }
264
265    final static class ClipState extends ConstantState {
266        Drawable mDrawable;
267        int mChangingConfigurations;
268        int mOrientation;
269        int mGravity;
270
271        private boolean mCheckedConstantState;
272        private boolean mCanConstantState;
273
274        ClipState(ClipState orig, ClipDrawable owner, Resources res) {
275            if (orig != null) {
276                if (res != null) {
277                    mDrawable = orig.mDrawable.getConstantState().newDrawable(res);
278                } else {
279                    mDrawable = orig.mDrawable.getConstantState().newDrawable();
280                }
281                mDrawable.setCallback(owner);
282                mDrawable.setLayoutDirection(orig.mDrawable.getLayoutDirection());
283                mOrientation = orig.mOrientation;
284                mGravity = orig.mGravity;
285                mCheckedConstantState = mCanConstantState = true;
286            }
287        }
288
289        @Override
290        public Drawable newDrawable() {
291            return new ClipDrawable(this, null);
292        }
293
294        @Override
295        public Drawable newDrawable(Resources res) {
296            return new ClipDrawable(this, res);
297        }
298
299        @Override
300        public int getChangingConfigurations() {
301            return mChangingConfigurations;
302        }
303
304        boolean canConstantState() {
305            if (!mCheckedConstantState) {
306                mCanConstantState = mDrawable.getConstantState() != null;
307                mCheckedConstantState = true;
308            }
309
310            return mCanConstantState;
311        }
312    }
313
314    private ClipDrawable(ClipState state, Resources res) {
315        mClipState = new ClipState(state, this, res);
316    }
317}
318
319