summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/util/DrawableConverter.java
blob: 5670315c996c30bea64d483be09b7ce34360d0ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import com.android.dialer.common.LogUtil;

/** Provides utilities for bitmaps and drawables. */
public class DrawableConverter {

  private DrawableConverter() {}

  /** Converts the provided drawable to a bitmap using the drawable's intrinsic width and height. */
  @Nullable
  public static Bitmap drawableToBitmap(@Nullable Drawable drawable) {
    return drawableToBitmap(drawable, 0, 0);
  }

  /**
   * Converts the provided drawable to a bitmap with the specified width and height.
   *
   * <p>If both width and height are 0, the drawable's intrinsic width and height are used (but in
   * that case {@link #drawableToBitmap(Drawable)} should be used).
   */
  @Nullable
  public static Bitmap drawableToBitmap(@Nullable Drawable drawable, int width, int height) {
    if (drawable == null) {
      return null;
    }

    Bitmap bitmap;
    if (drawable instanceof BitmapDrawable) {
      bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
      if (width > 0 || height > 0) {
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      } else if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        // Needed for drawables that are just a colour.
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
      } else {
        bitmap =
            Bitmap.createBitmap(
                drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
      }

      LogUtil.i(
          "DrawableConverter.drawableToBitmap",
          "created bitmap with width: %d, height: %d",
          bitmap.getWidth(),
          bitmap.getHeight());

      Canvas canvas = new Canvas(bitmap);
      drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
      drawable.draw(canvas);
    }
    return bitmap;
  }

  @Nullable
  public static Drawable getRoundedDrawable(
      @NonNull Context context, @Nullable Drawable photo, int width, int height) {
    Bitmap bitmap = drawableToBitmap(photo);
    if (bitmap != null) {
      Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
      RoundedBitmapDrawable drawable =
          RoundedBitmapDrawableFactory.create(context.getResources(), scaledBitmap);
      drawable.setAntiAlias(true);
      drawable.setCornerRadius(drawable.getIntrinsicHeight() / 2);
      return drawable;
    }
    return null;
  }
}