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