• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.graphics.drawable;
18 
19 import android.graphics.drawable.shapes.RoundRectShape;
20 import android.content.res.Resources;
21 import android.content.res.TypedArray;
22 import android.util.AttributeSet;
23 
24 import org.xmlpull.v1.XmlPullParser;
25 
26 /**
27  * Drawable that draws its bounds in the given paint, with optional
28  * rounded corners.
29 */
30 public class PaintDrawable extends ShapeDrawable {
31 
PaintDrawable()32     public PaintDrawable() {
33     }
34 
PaintDrawable(int color)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      */
setCornerRadius(float radius)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      */
setCornerRadii(float[] radii)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         invalidateSelf();
70     }
71 
72     @Override
inflateTag(String name, Resources r, XmlPullParser parser, AttributeSet attrs)73     protected boolean inflateTag(String name, Resources r, XmlPullParser parser,
74                                  AttributeSet attrs) {
75         if (name.equals("corners")) {
76             TypedArray a = r.obtainAttributes(attrs,
77                                         com.android.internal.R.styleable.DrawableCorners);
78             int radius = a.getDimensionPixelSize(
79                                 com.android.internal.R.styleable.DrawableCorners_radius, 0);
80             setCornerRadius(radius);
81 
82             // now check of they have any per-corner radii
83 
84             int topLeftRadius = a.getDimensionPixelSize(
85                     com.android.internal.R.styleable.DrawableCorners_topLeftRadius, radius);
86             int topRightRadius = a.getDimensionPixelSize(
87                     com.android.internal.R.styleable.DrawableCorners_topRightRadius, radius);
88             int bottomLeftRadius = a.getDimensionPixelSize(
89                 com.android.internal.R.styleable.DrawableCorners_bottomLeftRadius, radius);
90             int bottomRightRadius = a.getDimensionPixelSize(
91                 com.android.internal.R.styleable.DrawableCorners_bottomRightRadius, radius);
92 
93             if (topLeftRadius != radius || topRightRadius != radius ||
94                     bottomLeftRadius != radius || bottomRightRadius != radius) {
95                 setCornerRadii(new float[] {
96                                topLeftRadius, topLeftRadius,
97                                topRightRadius, topRightRadius,
98                                bottomLeftRadius, bottomLeftRadius,
99                                bottomRightRadius, bottomRightRadius
100                                });
101             }
102             a.recycle();
103             return true;
104         }
105         return super.inflateTag(name, r, parser, attrs);
106     }
107 }
108 
109