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