2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package android
.support
.v7
.widget
;
18 import android
.graphics
.Canvas
;
19 import android
.graphics
.ColorFilter
;
20 import android
.graphics
.Outline
;
21 import android
.graphics
.Paint
;
22 import android
.graphics
.PixelFormat
;
23 import android
.graphics
.Rect
;
24 import android
.graphics
.RectF
;
25 import android
.graphics
.drawable
.Drawable
;
27 import static android
.support
.v7
.widget
.RoundRectDrawableWithShadow
.calculateVerticalPadding
;
28 import static android
.support
.v7
.widget
.RoundRectDrawableWithShadow
.calculateHorizontalPadding
;
31 * Very simple drawable that draws a rounded rectangle background with arbitrary corners and also
32 * reports proper outline for L.
34 * Simpler and uses less resources compared to GradientDrawable or ShapeDrawable.
36 class RoundRectDrawable
extends Drawable
{
37 private float mRadius
;
38 private final Paint mPaint
;
39 private final RectF mBoundsF
;
40 private final Rect mBoundsI
;
41 private float mPadding
;
42 private boolean mInsetForPadding
= false;
43 private boolean mInsetForRadius
= true;
45 public RoundRectDrawable(int backgroundColor
, float radius
) {
47 mPaint
= new Paint(Paint
.ANTI_ALIAS_FLAG
| Paint
.DITHER_FLAG
);
48 mPaint
.setColor(backgroundColor
);
49 mBoundsF
= new RectF();
50 mBoundsI
= new Rect();
53 void setPadding(float padding
, boolean insetForPadding
, boolean insetForRadius
) {
54 if (padding
== mPadding
&& mInsetForPadding
== insetForPadding
&&
55 mInsetForRadius
== insetForRadius
) {
59 mInsetForPadding
= insetForPadding
;
60 mInsetForRadius
= insetForRadius
;
70 public void draw(Canvas canvas
) {
71 canvas
.drawRoundRect(mBoundsF
, mRadius
, mRadius
, mPaint
);
74 private void updateBounds(Rect bounds
) {
78 mBoundsF
.set(bounds
.left
, bounds
.top
, bounds
.right
, bounds
.bottom
);
80 if (mInsetForPadding
) {
81 float vInset
= calculateVerticalPadding(mPadding
, mRadius
, mInsetForRadius
);
82 float hInset
= calculateHorizontalPadding(mPadding
, mRadius
, mInsetForRadius
);
83 mBoundsI
.inset((int) Math
.ceil(hInset
), (int) Math
.ceil(vInset
));
84 // to make sure they have same bounds.
85 mBoundsF
.set(mBoundsI
);
90 protected void onBoundsChange(Rect bounds
) {
91 super.onBoundsChange(bounds
);
96 public void getOutline(Outline outline
) {
97 outline
.setRoundRect(mBoundsI
, mRadius
);
100 void setRadius(float radius
) {
101 if (radius
== mRadius
) {
110 public void setAlpha(int alpha
) {
111 // not supported because older versions do not support
115 public void setColorFilter(ColorFilter cf
) {
116 // not supported because older versions do not support
120 public int getOpacity() {
121 return PixelFormat
.TRANSLUCENT
;
124 public float getRadius() {
128 public void setColor(int color
) {
129 mPaint
.setColor(color
);