1 /* 2 * Copyright (C) 2015 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 android.support.car.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 RoundedBitmapDrawable, 32 * since that implementation doesn't behave quite as desired. 33 * 34 * 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 /* if (processed.getWidth() != width || processed.getHeight() != height) { 58 processed = BitmapUtils.scaleBitmap(processed, width, height); 59 } 60 // RoundedBitmapDrawable is actually just a rounded rectangle. So it can't turn 61 // rectangular images into circles. 62 if (processed.getWidth() != processed.getHeight()) { 63 int diam = Math.min(width, height); 64 Bitmap cropped = BitmapUtils.cropBitmap(processed, diam, diam); 65 if (processed != mBitmap) { 66 processed.recycle(); 67 } 68 processed = cropped; 69 }*/ 70 mDrawable = RoundedBitmapDrawableFactory.create(mResources, processed); 71 mDrawable.setBounds(bounds); 72 mDrawable.setAntiAlias(true); 73 mDrawable.setCornerRadius(Math.min(width, height) / 2f); 74 if (mAlpha != -1) { 75 mDrawable.setAlpha(mAlpha); 76 } 77 if (mCf != null) { 78 mDrawable.setColorFilter(mCf); 79 } 80 invalidateSelf(); 81 } 82 83 @Override draw(Canvas canvas)84 public void draw(Canvas canvas) { 85 if (mDrawable != null) { 86 mDrawable.draw(canvas); 87 } 88 } 89 90 @Override getOpacity()91 public int getOpacity() { 92 return mDrawable != null ? mDrawable.getOpacity() : PixelFormat.TRANSLUCENT; 93 } 94 95 @Override setAlpha(int alpha)96 public void setAlpha(int alpha) { 97 mAlpha = alpha; 98 if (mDrawable != null) { 99 mDrawable.setAlpha(alpha); 100 invalidateSelf(); 101 } 102 } 103 104 @Override setColorFilter(ColorFilter cf)105 public void setColorFilter(ColorFilter cf) { 106 mCf = cf; 107 if (mDrawable != null) { 108 mDrawable.setColorFilter(cf); 109 invalidateSelf(); 110 } 111 } 112 113 /** 114 * Convert the drawable to a bitmap. 115 * @param size The target size of the bitmap in pixels. 116 * @return A bitmap representation of the drawable. 117 */ toBitmap(int size)118 public Bitmap toBitmap(int size) { 119 Bitmap largeIcon = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); 120 Canvas canvas = new Canvas(largeIcon); 121 Rect bounds = getBounds(); 122 setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 123 draw(canvas); 124 setBounds(bounds); 125 return largeIcon; 126 } 127 } 128 129