1/* 2 * Copyright (C) 2008 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 17package com.android.launcher2; 18 19import android.graphics.Bitmap; 20import android.graphics.Canvas; 21import android.graphics.ColorFilter; 22import android.graphics.Paint; 23import android.graphics.PixelFormat; 24import android.graphics.Rect; 25import android.graphics.drawable.Drawable; 26 27class FastBitmapDrawable extends Drawable { 28 private Bitmap mBitmap; 29 private int mAlpha; 30 private int mWidth; 31 private int mHeight; 32 private final Paint mPaint = new Paint(); 33 34 FastBitmapDrawable(Bitmap b) { 35 mAlpha = 255; 36 mBitmap = b; 37 if (b != null) { 38 mWidth = mBitmap.getWidth(); 39 mHeight = mBitmap.getHeight(); 40 } else { 41 mWidth = mHeight = 0; 42 } 43 } 44 45 @Override 46 public void draw(Canvas canvas) { 47 final Rect r = getBounds(); 48 canvas.drawBitmap(mBitmap, r.left, r.top, mPaint); 49 } 50 51 @Override 52 public void setColorFilter(ColorFilter cf) { 53 mPaint.setColorFilter(cf); 54 } 55 56 @Override 57 public int getOpacity() { 58 return PixelFormat.TRANSLUCENT; 59 } 60 61 @Override 62 public void setAlpha(int alpha) { 63 mAlpha = alpha; 64 mPaint.setAlpha(alpha); 65 } 66 67 public void setFilterBitmap(boolean filterBitmap) { 68 mPaint.setFilterBitmap(filterBitmap); 69 } 70 71 public int getAlpha() { 72 return mAlpha; 73 } 74 75 @Override 76 public int getIntrinsicWidth() { 77 return mWidth; 78 } 79 80 @Override 81 public int getIntrinsicHeight() { 82 return mHeight; 83 } 84 85 @Override 86 public int getMinimumWidth() { 87 return mWidth; 88 } 89 90 @Override 91 public int getMinimumHeight() { 92 return mHeight; 93 } 94 95 public void setBitmap(Bitmap b) { 96 mBitmap = b; 97 if (b != null) { 98 mWidth = mBitmap.getWidth(); 99 mHeight = mBitmap.getHeight(); 100 } else { 101 mWidth = mHeight = 0; 102 } 103 } 104 105 public Bitmap getBitmap() { 106 return mBitmap; 107 } 108} 109