• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 package com.android.car.dialer.ui;
17 
18 import android.content.res.Resources;
19 import android.graphics.Bitmap;
20 import android.graphics.Canvas;
21 import android.graphics.ColorFilter;
22 import android.graphics.PixelFormat;
23 import android.graphics.Rect;
24 import android.graphics.drawable.Drawable;
25 import android.support.annotation.NonNull;
26 import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
27 import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
28 
29 
30 /**
31  * A drawable for displaying a circular bitmap. This is a wrapper over {@link RoundedBitmapDrawable}
32  * since that implementation doesn't behave quite as desired.
33  *
34  * <p>Note that not all drawable functionality is passed to the RoundedBitmapDrawable at this
35  * time. Feel free to add more as necessary.
36  */
37 public class CircleBitmapDrawable extends Drawable {
38     private final Resources mResources;
39 
40     private Bitmap mBitmap;
41     private RoundedBitmapDrawable mDrawable;
42     private int mAlpha = -1;
43     private ColorFilter mCf = null;
44 
CircleBitmapDrawable(@onNull Resources res, @NonNull Bitmap bitmap)45     public CircleBitmapDrawable(@NonNull Resources res, @NonNull Bitmap bitmap) {
46         mBitmap = bitmap;
47         mResources = res;
48     }
49 
50     @Override
onBoundsChange(Rect bounds)51     public void onBoundsChange(Rect bounds) {
52         super.onBoundsChange(bounds);
53         int width = bounds.right - bounds.left;
54         int height = bounds.bottom - bounds.top;
55 
56         Bitmap processed = mBitmap;
57         mDrawable = RoundedBitmapDrawableFactory.create(mResources, processed);
58         mDrawable.setBounds(bounds);
59         mDrawable.setAntiAlias(true);
60         mDrawable.setCornerRadius(Math.min(width, height) / 2f);
61         if (mAlpha != -1) {
62             mDrawable.setAlpha(mAlpha);
63         }
64         if (mCf != null) {
65             mDrawable.setColorFilter(mCf);
66         }
67         invalidateSelf();
68     }
69 
70     @Override
draw(Canvas canvas)71     public void draw(Canvas canvas) {
72         if (mDrawable != null) {
73             mDrawable.draw(canvas);
74         }
75     }
76 
77     @Override
getOpacity()78     public int getOpacity() {
79         return mDrawable != null ? mDrawable.getOpacity() : PixelFormat.TRANSLUCENT;
80     }
81 
82     @Override
setAlpha(int alpha)83     public void setAlpha(int alpha) {
84         mAlpha = alpha;
85         if (mDrawable != null) {
86             mDrawable.setAlpha(alpha);
87             invalidateSelf();
88         }
89     }
90 
91     @Override
setColorFilter(ColorFilter cf)92     public void setColorFilter(ColorFilter cf) {
93         mCf = cf;
94         if (mDrawable != null) {
95             mDrawable.setColorFilter(cf);
96             invalidateSelf();
97         }
98     }
99 
100     /**
101      * Convert the drawable to a bitmap.
102      * @param size The target size of the bitmap in pixels.
103      * @return A bitmap representation of the drawable.
104      */
toBitmap(int size)105     public Bitmap toBitmap(int size) {
106         Bitmap largeIcon = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
107         Canvas canvas = new Canvas(largeIcon);
108         Rect bounds = getBounds();
109         setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
110         draw(canvas);
111         setBounds(bounds);
112         return largeIcon;
113     }
114 }
115 
116