PaintDrawable.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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 android.graphics.drawable.shapes.RoundRectShape;
20import android.content.res.Resources;
21import android.content.res.TypedArray;
22import android.util.AttributeSet;
23
24import org.xmlpull.v1.XmlPullParser;
25
26/**
27 * Drawable that draws its bounds in the given paint, with optional
28 * rounded corners.
29*/
30public class PaintDrawable extends ShapeDrawable {
31
32    public PaintDrawable() {
33    }
34
35    public PaintDrawable(int color) {
36        getPaint().setColor(color);
37    }
38
39    /**
40     * Specify radius for the corners of the rectangle. If this is > 0, then the
41     * drawable is drawn in a round-rectangle, rather than a rectangle.
42     * @param radius the radius for the corners of the rectangle
43     */
44    public void setCornerRadius(float radius) {
45        float[] radii = null;
46        if (radius > 0) {
47            radii = new float[8];
48            for (int i = 0; i < 8; i++) {
49                radii[i] = radius;
50            }
51        }
52        setCornerRadii(radii);
53    }
54
55    /**
56     * Specify radii for each of the 4 corners. For each corner, the array
57     * contains 2 values, [X_radius, Y_radius]. The corners are ordered
58     * top-left, top-right, bottom-right, bottom-left
59     * @param radii the x and y radii of the corners
60     */
61    public void setCornerRadii(float[] radii) {
62        if (radii == null) {
63            if (getShape() != null) {
64                setShape(null);
65            }
66        } else {
67            setShape(new RoundRectShape(radii, null, null));
68        }
69    }
70
71    @Override
72    protected boolean inflateTag(String name, Resources r, XmlPullParser parser,
73                                 AttributeSet attrs) {
74        if (name.equals("corners")) {
75            TypedArray a = r.obtainAttributes(attrs,
76                                        com.android.internal.R.styleable.DrawableCorners);
77            int radius = a.getDimensionPixelSize(
78                                com.android.internal.R.styleable.DrawableCorners_radius, 0);
79            setCornerRadius(radius);
80
81            // now check of they have any per-corner radii
82
83            int topLeftRadius = a.getDimensionPixelSize(
84                    com.android.internal.R.styleable.DrawableCorners_topLeftRadius, radius);
85            int topRightRadius = a.getDimensionPixelSize(
86                    com.android.internal.R.styleable.DrawableCorners_topRightRadius, radius);
87            int bottomLeftRadius = a.getDimensionPixelSize(
88                com.android.internal.R.styleable.DrawableCorners_bottomLeftRadius, radius);
89            int bottomRightRadius = a.getDimensionPixelSize(
90                com.android.internal.R.styleable.DrawableCorners_bottomRightRadius, radius);
91
92            if (topLeftRadius != radius || topRightRadius != radius ||
93                    bottomLeftRadius != radius || bottomRightRadius != radius) {
94                setCornerRadii(new float[] {
95                               topLeftRadius, topLeftRadius,
96                               topRightRadius, topRightRadius,
97                               bottomLeftRadius, bottomLeftRadius,
98                               bottomRightRadius, bottomRightRadius
99                               });
100            }
101            a.recycle();
102            return true;
103        }
104        return super.inflateTag(name, r, parser, attrs);
105    }
106}
107
108