Roll android_tools support library to 25.1.0
[android_tools.git] / sdk / sources / android-23 / android / support / v7 / widget / RoundRectDrawable.java
blob347776186685417570f5d62c8af14f0dfc595d2f
1 /*
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;
30 /**
31 * Very simple drawable that draws a rounded rectangle background with arbitrary corners and also
32 * reports proper outline for L.
33 * <p>
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) {
46 mRadius = 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) {
56 return;
58 mPadding = padding;
59 mInsetForPadding = insetForPadding;
60 mInsetForRadius = insetForRadius;
61 updateBounds(null);
62 invalidateSelf();
65 float getPadding() {
66 return mPadding;
69 @Override
70 public void draw(Canvas canvas) {
71 canvas.drawRoundRect(mBoundsF, mRadius, mRadius, mPaint);
74 private void updateBounds(Rect bounds) {
75 if (bounds == null) {
76 bounds = getBounds();
78 mBoundsF.set(bounds.left, bounds.top, bounds.right, bounds.bottom);
79 mBoundsI.set(bounds);
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);
89 @Override
90 protected void onBoundsChange(Rect bounds) {
91 super.onBoundsChange(bounds);
92 updateBounds(bounds);
95 @Override
96 public void getOutline(Outline outline) {
97 outline.setRoundRect(mBoundsI, mRadius);
100 void setRadius(float radius) {
101 if (radius == mRadius) {
102 return;
104 mRadius = radius;
105 updateBounds(null);
106 invalidateSelf();
109 @Override
110 public void setAlpha(int alpha) {
111 // not supported because older versions do not support
114 @Override
115 public void setColorFilter(ColorFilter cf) {
116 // not supported because older versions do not support
119 @Override
120 public int getOpacity() {
121 return PixelFormat.TRANSLUCENT;
124 public float getRadius() {
125 return mRadius;
128 public void setColor(int color) {
129 mPaint.setColor(color);
130 invalidateSelf();