1 /*
2  * Copyright (C) 2008 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 com.android.intentresolver.widget;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.Paint;
24 import android.graphics.Path;
25 import android.util.AttributeSet;
26 import android.widget.ImageView;
27 
28 import com.android.intentresolver.R;
29 
30 /**
31  * {@link ImageView} that rounds the corners around the presented image while obeying view padding.
32  */
33 public class RoundedRectImageView extends ImageView {
34     private int mRadius = 0;
35     private Path mPath = new Path();
36     private Paint mOverlayPaint = new Paint(0);
37     private Paint mRoundRectPaint = new Paint(0);
38     private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
39     private String mExtraImageCount = null;
40 
RoundedRectImageView(Context context)41     public RoundedRectImageView(Context context) {
42         super(context);
43     }
44 
RoundedRectImageView(Context context, AttributeSet attrs)45     public RoundedRectImageView(Context context, AttributeSet attrs) {
46         this(context, attrs, 0);
47     }
48 
RoundedRectImageView(Context context, AttributeSet attrs, int defStyleAttr)49     public RoundedRectImageView(Context context, AttributeSet attrs, int defStyleAttr) {
50         this(context, attrs, defStyleAttr, 0);
51     }
52 
RoundedRectImageView( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)53     public RoundedRectImageView(
54             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
55         super(context, attrs, defStyleAttr, defStyleRes);
56 
57         final TypedArray a = context.obtainStyledAttributes(
58                 attrs,
59                 R.styleable.RoundedRectImageView,
60                 defStyleAttr,
61                 0);
62         mRadius = a.getDimensionPixelSize(R.styleable.RoundedRectImageView_radius, -1);
63         if (mRadius < 0) {
64             mRadius = context.getResources().getDimensionPixelSize(R.dimen.chooser_corner_radius);
65         }
66         a.recycle();
67 
68         mOverlayPaint.setColor(0x99000000);
69         mOverlayPaint.setStyle(Paint.Style.FILL);
70 
71         mRoundRectPaint.setColor(context.getResources().getColor(R.color.chooser_row_divider));
72         mRoundRectPaint.setStyle(Paint.Style.STROKE);
73         mRoundRectPaint.setStrokeWidth(context.getResources()
74                 .getDimensionPixelSize(R.dimen.chooser_preview_image_border));
75 
76         mTextPaint.setColor(Color.WHITE);
77         mTextPaint.setTextSize(context.getResources()
78                 .getDimensionPixelSize(R.dimen.chooser_preview_image_font_size));
79         mTextPaint.setTextAlign(Paint.Align.CENTER);
80     }
81 
updatePath(int width, int height)82     private void updatePath(int width, int height) {
83         mPath.reset();
84 
85         int imageWidth = width - getPaddingRight() - getPaddingLeft();
86         int imageHeight = height - getPaddingBottom() - getPaddingTop();
87         mPath.addRoundRect(getPaddingLeft(), getPaddingTop(), imageWidth, imageHeight, mRadius,
88                 mRadius, Path.Direction.CW);
89     }
90 
91     /**
92       * Sets the corner radius on all corners
93       *
94       * param radius 0 for no radius, &gt; 0 for a visible corner radius
95       */
setRadius(int radius)96     public void setRadius(int radius) {
97         mRadius = radius;
98         updatePath(getWidth(), getHeight());
99     }
100 
101     /**
102       * Display an overlay with extra image count on 3rd image
103       */
setExtraImageCount(int count)104     public void setExtraImageCount(int count) {
105         if (count > 0) {
106             this.mExtraImageCount = "+" + count;
107         } else {
108             this.mExtraImageCount = null;
109         }
110         invalidate();
111     }
112 
113     @Override
onSizeChanged(int width, int height, int oldWidth, int oldHeight)114     protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
115         super.onSizeChanged(width, height, oldWidth, oldHeight);
116         updatePath(width, height);
117     }
118 
119     @Override
onDraw(Canvas canvas)120     protected void onDraw(Canvas canvas) {
121         if (mRadius != 0) {
122             canvas.clipPath(mPath);
123         }
124 
125         super.onDraw(canvas);
126 
127         int x = getPaddingLeft();
128         int y = getPaddingRight();
129         int width = getWidth() - getPaddingRight() - getPaddingLeft();
130         int height = getHeight() - getPaddingBottom() - getPaddingTop();
131         if (mExtraImageCount != null) {
132             canvas.drawRect(x, y, width, height, mOverlayPaint);
133 
134             int xPos = canvas.getWidth() / 2;
135             int yPos = (int) ((canvas.getHeight() / 2.0f)
136                     - ((mTextPaint.descent() + mTextPaint.ascent()) / 2.0f));
137 
138             canvas.drawText(mExtraImageCount, xPos, yPos, mTextPaint);
139         }
140 
141         canvas.drawRoundRect(x, y, width, height, mRadius, mRadius, mRoundRectPaint);
142     }
143 }
144