1 /*
2  * Copyright (C) 2012 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.dialer.util;
18 
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Canvas;
22 import android.graphics.drawable.BitmapDrawable;
23 import android.graphics.drawable.Drawable;
24 import android.support.annotation.NonNull;
25 import android.support.annotation.Nullable;
26 import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
27 import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
28 import com.android.dialer.common.LogUtil;
29 
30 /** Provides utilities for bitmaps and drawables. */
31 public class DrawableConverter {
32 
DrawableConverter()33   private DrawableConverter() {}
34 
35   /** Converts the provided drawable to a bitmap using the drawable's intrinsic width and height. */
36   @Nullable
drawableToBitmap(@ullable Drawable drawable)37   public static Bitmap drawableToBitmap(@Nullable Drawable drawable) {
38     return drawableToBitmap(drawable, 0, 0);
39   }
40 
41   /**
42    * Converts the provided drawable to a bitmap with the specified width and height.
43    *
44    * <p>If both width and height are 0, the drawable's intrinsic width and height are used (but in
45    * that case {@link #drawableToBitmap(Drawable)} should be used).
46    */
47   @Nullable
drawableToBitmap(@ullable Drawable drawable, int width, int height)48   public static Bitmap drawableToBitmap(@Nullable Drawable drawable, int width, int height) {
49     if (drawable == null) {
50       return null;
51     }
52 
53     Bitmap bitmap;
54     if (drawable instanceof BitmapDrawable) {
55       bitmap = ((BitmapDrawable) drawable).getBitmap();
56     } else {
57       if (width > 0 || height > 0) {
58         bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
59       } else if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
60         // Needed for drawables that are just a colour.
61         bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
62       } else {
63         bitmap =
64             Bitmap.createBitmap(
65                 drawable.getIntrinsicWidth(),
66                 drawable.getIntrinsicHeight(),
67                 Bitmap.Config.ARGB_8888);
68       }
69 
70       LogUtil.i(
71           "DrawableConverter.drawableToBitmap",
72           "created bitmap with width: %d, height: %d",
73           bitmap.getWidth(),
74           bitmap.getHeight());
75 
76       Canvas canvas = new Canvas(bitmap);
77       drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
78       drawable.draw(canvas);
79     }
80     return bitmap;
81   }
82 
83   @Nullable
getRoundedDrawable( @onNull Context context, @Nullable Drawable photo, int width, int height)84   public static Drawable getRoundedDrawable(
85       @NonNull Context context, @Nullable Drawable photo, int width, int height) {
86     Bitmap bitmap = drawableToBitmap(photo);
87     if (bitmap != null) {
88       Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
89       RoundedBitmapDrawable drawable =
90           RoundedBitmapDrawableFactory.create(context.getResources(), scaledBitmap);
91       drawable.setAntiAlias(true);
92       drawable.setCornerRadius(drawable.getIntrinsicHeight() / 2);
93       return drawable;
94     }
95     return null;
96   }
97 }
98