ShapeDrawable.java revision 6efd2bad954e0e5bd74916a32f036a0f149dcd4d
1/*
2 * Copyright (C) 2007 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 android.graphics.*;
20import android.graphics.drawable.shapes.Shape;
21import android.content.res.Resources;
22import android.content.res.TypedArray;
23import android.util.AttributeSet;
24
25import org.xmlpull.v1.XmlPullParser;
26import org.xmlpull.v1.XmlPullParserException;
27
28import java.io.IOException;
29
30/**
31 * A Drawable object that draws primitive shapes.
32 * A ShapeDrawable takes a {@link android.graphics.drawable.shapes.Shape}
33 * object and manages its presence on the screen. If no Shape is given, then
34 * the ShapeDrawable will default to a
35 * {@link android.graphics.drawable.shapes.RectShape}.
36 *
37 * <p>It can be defined in an XML file with the <code>&lt;shape></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#ShapeDrawablePadding_left
42 * @attr ref android.R.styleable#ShapeDrawablePadding_top
43 * @attr ref android.R.styleable#ShapeDrawablePadding_right
44 * @attr ref android.R.styleable#ShapeDrawablePadding_bottom
45 * @attr ref android.R.styleable#ShapeDrawable_color
46 * @attr ref android.R.styleable#ShapeDrawable_width
47 * @attr ref android.R.styleable#ShapeDrawable_height
48 */
49public class ShapeDrawable extends Drawable {
50    private ShapeState mShapeState;
51    private boolean mMutated;
52
53    /**
54     * ShapeDrawable constructor.
55     */
56    public ShapeDrawable() {
57        this((ShapeState) null);
58    }
59
60    /**
61     * Creates a ShapeDrawable with a specified Shape.
62     *
63     * @param s the Shape that this ShapeDrawable should be
64     */
65    public ShapeDrawable(Shape s) {
66        this((ShapeState) null);
67
68        mShapeState.mShape = s;
69    }
70
71    private ShapeDrawable(ShapeState state) {
72        mShapeState = new ShapeState(state);
73    }
74
75    /**
76     * Returns the Shape of this ShapeDrawable.
77     */
78    public Shape getShape() {
79        return mShapeState.mShape;
80    }
81
82    /**
83     * Sets the Shape of this ShapeDrawable.
84     */
85    public void setShape(Shape s) {
86        mShapeState.mShape = s;
87        updateShape();
88    }
89
90    /**
91     * Sets a ShaderFactory to which requests for a
92     * {@link android.graphics.Shader} object will be made.
93     *
94     * @param fact an instance of your ShaderFactory implementation
95     */
96    public void setShaderFactory(ShaderFactory fact) {
97        mShapeState.mShaderFactory = fact;
98    }
99
100    /**
101     * Returns the ShaderFactory used by this ShapeDrawable for requesting a
102     * {@link android.graphics.Shader}.
103     */
104    public ShaderFactory getShaderFactory() {
105        return mShapeState.mShaderFactory;
106    }
107
108    /**
109     * Returns the Paint used to draw the shape.
110     */
111    public Paint getPaint() {
112        return mShapeState.mPaint;
113    }
114
115    /**
116     * Sets padding for the shape.
117     *
118     * @param left    padding for the left side (in pixels)
119     * @param top     padding for the top (in pixels)
120     * @param right   padding for the right side (in pixels)
121     * @param bottom  padding for the bottom (in pixels)
122     */
123    public void setPadding(int left, int top, int right, int bottom) {
124        if ((left | top | right | bottom) == 0) {
125            mShapeState.mPadding = null;
126        } else {
127            if (mShapeState.mPadding == null) {
128                mShapeState.mPadding = new Rect();
129            }
130            mShapeState.mPadding.set(left, top, right, bottom);
131        }
132        invalidateSelf();
133    }
134
135    /**
136     * Sets padding for this shape, defined by a Rect object.
137     * Define the padding in the Rect object as: left, top, right, bottom.
138     */
139    public void setPadding(Rect padding) {
140        if (padding == null) {
141            mShapeState.mPadding = null;
142        } else {
143            if (mShapeState.mPadding == null) {
144                mShapeState.mPadding = new Rect();
145            }
146            mShapeState.mPadding.set(padding);
147        }
148        invalidateSelf();
149    }
150
151    /**
152     * Sets the intrinsic (default) width for this shape.
153     *
154     * @param width the intrinsic width (in pixels)
155     */
156    public void setIntrinsicWidth(int width) {
157        mShapeState.mIntrinsicWidth = width;
158        invalidateSelf();
159    }
160
161    /**
162     * Sets the intrinsic (default) height for this shape.
163     *
164     * @param height the intrinsic height (in pixels)
165     */
166    public void setIntrinsicHeight(int height) {
167        mShapeState.mIntrinsicHeight = height;
168        invalidateSelf();
169    }
170
171    @Override
172    public int getIntrinsicWidth() {
173        return mShapeState.mIntrinsicWidth;
174    }
175
176    @Override
177    public int getIntrinsicHeight() {
178        return mShapeState.mIntrinsicHeight;
179    }
180
181    @Override
182    public boolean getPadding(Rect padding) {
183        if (mShapeState.mPadding != null) {
184            padding.set(mShapeState.mPadding);
185            return true;
186        } else {
187            return super.getPadding(padding);
188        }
189    }
190
191    private static int modulateAlpha(int paintAlpha, int alpha) {
192        int scale = alpha + (alpha >>> 7);  // convert to 0..256
193        return paintAlpha * scale >>> 8;
194    }
195
196    /**
197     * Called from the drawable's draw() method after the canvas has been set
198     * to draw the shape at (0,0). Subclasses can override for special effects
199     * such as multiple layers, stroking, etc.
200     */
201    protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
202        shape.draw(canvas, paint);
203    }
204
205    @Override
206    public void draw(Canvas canvas) {
207        Rect r = getBounds();
208        Paint paint = mShapeState.mPaint;
209
210        int prevAlpha = paint.getAlpha();
211        paint.setAlpha(modulateAlpha(prevAlpha, mShapeState.mAlpha));
212
213        if (mShapeState.mShape != null) {
214            // need the save both for the translate, and for the (unknown) Shape
215            int count = canvas.save();
216            canvas.translate(r.left, r.top);
217            onDraw(mShapeState.mShape, canvas, paint);
218            canvas.restoreToCount(count);
219        } else {
220            canvas.drawRect(r, paint);
221        }
222
223        // restore
224        paint.setAlpha(prevAlpha);
225    }
226
227    @Override
228    public int getChangingConfigurations() {
229        return super.getChangingConfigurations()
230                | mShapeState.mChangingConfigurations;
231    }
232
233    /**
234     * Set the alpha level for this drawable [0..255]. Note that this drawable
235     * also has a color in its paint, which has an alpha as well. These two
236     * values are automatically combined during drawing. Thus if the color's
237     * alpha is 75% (i.e. 192) and the drawable's alpha is 50% (i.e. 128), then
238     * the combined alpha that will be used during drawing will be 37.5%
239     * (i.e. 96).
240     */
241    @Override public void setAlpha(int alpha) {
242        mShapeState.mAlpha = alpha;
243        invalidateSelf();
244    }
245
246    @Override
247    public void setColorFilter(ColorFilter cf) {
248        mShapeState.mPaint.setColorFilter(cf);
249        invalidateSelf();
250    }
251
252    @Override
253    public int getOpacity() {
254        if (mShapeState.mShape == null) {
255            final Paint p = mShapeState.mPaint;
256            if (p.getXfermode() == null) {
257                final int alpha = p.getAlpha();
258                if (alpha == 0) {
259                    return PixelFormat.TRANSPARENT;
260                }
261                if (alpha == 255) {
262                    return PixelFormat.OPAQUE;
263                }
264            }
265        }
266        // not sure, so be safe
267        return PixelFormat.TRANSLUCENT;
268    }
269
270    @Override
271    public void setDither(boolean dither) {
272        mShapeState.mPaint.setDither(dither);
273        invalidateSelf();
274    }
275
276    @Override
277    protected void onBoundsChange(Rect bounds) {
278        super.onBoundsChange(bounds);
279        updateShape();
280    }
281
282    /**
283     * Subclasses override this to parse custom subelements.
284     * If you handle it, return true, else return <em>super.inflateTag(...)</em>.
285     */
286    protected boolean inflateTag(String name, Resources r, XmlPullParser parser,
287            AttributeSet attrs) {
288
289        if (name.equals("padding")) {
290            TypedArray a = r.obtainAttributes(attrs,
291                    com.android.internal.R.styleable.ShapeDrawablePadding);
292            setPadding(
293                    a.getDimensionPixelOffset(
294                            com.android.internal.R.styleable.ShapeDrawablePadding_left, 0),
295                    a.getDimensionPixelOffset(
296                            com.android.internal.R.styleable.ShapeDrawablePadding_top, 0),
297                    a.getDimensionPixelOffset(
298                            com.android.internal.R.styleable.ShapeDrawablePadding_right, 0),
299                    a.getDimensionPixelOffset(
300                            com.android.internal.R.styleable.ShapeDrawablePadding_bottom, 0));
301            a.recycle();
302            return true;
303        }
304
305        return false;
306    }
307
308    @Override
309    public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
310                        throws XmlPullParserException, IOException {
311        super.inflate(r, parser, attrs);
312
313        TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.ShapeDrawable);
314
315        int color = mShapeState.mPaint.getColor();
316        color = a.getColor(com.android.internal.R.styleable.ShapeDrawable_color, color);
317        mShapeState.mPaint.setColor(color);
318
319        setIntrinsicWidth((int)
320                a.getDimension(com.android.internal.R.styleable.ShapeDrawable_width, 0f));
321        setIntrinsicHeight((int)
322                a.getDimension(com.android.internal.R.styleable.ShapeDrawable_height, 0f));
323
324        a.recycle();
325
326        int type;
327        final int outerDepth = parser.getDepth();
328        while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
329               && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
330            if (type != XmlPullParser.START_TAG) {
331                continue;
332            }
333
334            final String name = parser.getName();
335            // call our subclass
336            if (!inflateTag(name, r, parser, attrs)) {
337                android.util.Log.w("drawable", "Unknown element: " + name +
338                        " for ShapeDrawable " + this);
339            }
340        }
341    }
342
343    private void updateShape() {
344        if (mShapeState.mShape != null) {
345            final Rect r = getBounds();
346            final int w = r.width();
347            final int h = r.height();
348
349            mShapeState.mShape.resize(w, h);
350            if (mShapeState.mShaderFactory != null) {
351                mShapeState.mPaint.setShader(mShapeState.mShaderFactory.resize(w, h));
352            }
353        }
354        invalidateSelf();
355    }
356
357    @Override
358    public ConstantState getConstantState() {
359        mShapeState.mChangingConfigurations = getChangingConfigurations();
360        return mShapeState;
361    }
362
363    @Override
364    public Drawable mutate() {
365        if (!mMutated && super.mutate() == this) {
366            mShapeState.mPaint = new Paint(mShapeState.mPaint);
367            mShapeState.mPadding = new Rect(mShapeState.mPadding);
368            try {
369                mShapeState.mShape = mShapeState.mShape.clone();
370            } catch (CloneNotSupportedException e) {
371                return null;
372            }
373            mMutated = true;
374        }
375        return this;
376    }
377
378    /**
379     * Defines the intrinsic properties of this ShapeDrawable's Shape.
380     */
381    final static class ShapeState extends ConstantState {
382        int mChangingConfigurations;
383        Paint mPaint;
384        Shape mShape;
385        Rect mPadding;
386        int mIntrinsicWidth;
387        int mIntrinsicHeight;
388        int mAlpha = 255;
389        ShaderFactory mShaderFactory;
390
391        ShapeState(ShapeState orig) {
392            if (orig != null) {
393                mPaint = orig.mPaint;
394                mShape = orig.mShape;
395                mPadding = orig.mPadding;
396                mIntrinsicWidth = orig.mIntrinsicWidth;
397                mIntrinsicHeight = orig.mIntrinsicHeight;
398                mAlpha = orig.mAlpha;
399                mShaderFactory = orig.mShaderFactory;
400            } else {
401                mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
402            }
403        }
404
405        @Override
406        public Drawable newDrawable() {
407            return new ShapeDrawable(this);
408        }
409
410        @Override
411        public Drawable newDrawable(Resources res) {
412            return new ShapeDrawable(this);
413        }
414
415        @Override
416        public int getChangingConfigurations() {
417            return mChangingConfigurations;
418        }
419    }
420
421    /**
422     * Base class defines a factory object that is called each time the drawable
423     * is resized (has a new width or height). Its resize() method returns a
424     * corresponding shader, or null.
425     * Implement this class if you'd like your ShapeDrawable to use a special
426     * {@link android.graphics.Shader}, such as a
427     * {@link android.graphics.LinearGradient}.
428     *
429     */
430    public static abstract class ShaderFactory {
431        /**
432         * Returns the Shader to be drawn when a Drawable is drawn.
433         * The dimensions of the Drawable are passed because they may be needed
434         * to adjust how the Shader is configured for drawing.
435         * This is called by ShapeDrawable.setShape().
436         *
437         * @param width  the width of the Drawable being drawn
438         * @param height the heigh of the Drawable being drawn
439         * @return       the Shader to be drawn
440         */
441        public abstract Shader resize(int width, int height);
442    }
443
444    // other subclass could wack the Shader's localmatrix based on the
445    // resize params (e.g. scaletofit, etc.). This could be used to scale
446    // a bitmap to fill the bounds without needing any other special casing.
447}
448
449