1 /* 2 * Copyright (C) 2011 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.launcher3.widget; 18 19 import android.content.Context; 20 import android.graphics.Canvas; 21 import android.graphics.Rect; 22 import android.graphics.RectF; 23 import android.graphics.drawable.Drawable; 24 import android.util.AttributeSet; 25 import android.view.View; 26 27 import com.android.launcher3.R; 28 29 /** 30 * View that draws a bitmap horizontally centered. If the image width is greater than the view 31 * width, the image is scaled down appropriately. 32 */ 33 public class WidgetImageView extends View { 34 35 private final RectF mDstRectF = new RectF(); 36 private final int mBadgeMargin; 37 38 private Drawable mDrawable; 39 WidgetImageView(Context context)40 public WidgetImageView(Context context) { 41 this(context, null); 42 } 43 WidgetImageView(Context context, AttributeSet attrs)44 public WidgetImageView(Context context, AttributeSet attrs) { 45 this(context, attrs, 0); 46 } 47 WidgetImageView(Context context, AttributeSet attrs, int defStyle)48 public WidgetImageView(Context context, AttributeSet attrs, int defStyle) { 49 super(context, attrs, defStyle); 50 51 mBadgeMargin = context.getResources() 52 .getDimensionPixelSize(R.dimen.profile_badge_margin); 53 } 54 55 /** Set the drawable to use for this view. */ setDrawable(Drawable drawable)56 public void setDrawable(Drawable drawable) { 57 mDrawable = drawable; 58 invalidate(); 59 } 60 getDrawable()61 public Drawable getDrawable() { 62 return mDrawable; 63 } 64 65 @Override onDraw(Canvas canvas)66 protected void onDraw(Canvas canvas) { 67 if (mDrawable != null) { 68 updateDstRectF(); 69 mDrawable.setBounds(getBitmapBounds()); 70 mDrawable.draw(canvas); 71 } 72 } 73 74 /** 75 * Prevents the inefficient alpha view rendering. 76 */ 77 @Override hasOverlappingRendering()78 public boolean hasOverlappingRendering() { 79 return false; 80 } 81 updateDstRectF()82 private void updateDstRectF() { 83 float myWidth = getWidth(); 84 float myHeight = getHeight(); 85 float bitmapWidth = mDrawable.getIntrinsicWidth(); 86 87 final float scale = bitmapWidth > myWidth ? myWidth / bitmapWidth : 1; 88 float scaledWidth = bitmapWidth * scale; 89 float scaledHeight = mDrawable.getIntrinsicHeight() * scale; 90 91 mDstRectF.left = (myWidth - scaledWidth) / 2; 92 mDstRectF.right = (myWidth + scaledWidth) / 2; 93 94 if (scaledHeight > myHeight) { 95 mDstRectF.top = 0; 96 mDstRectF.bottom = scaledHeight; 97 } else { 98 mDstRectF.top = (myHeight - scaledHeight) / 2; 99 mDstRectF.bottom = (myHeight + scaledHeight) / 2; 100 } 101 } 102 103 /** 104 * @return the bounds where the image was drawn. 105 */ getBitmapBounds()106 public Rect getBitmapBounds() { 107 updateDstRectF(); 108 Rect rect = new Rect(); 109 mDstRectF.round(rect); 110 return rect; 111 } 112 } 113