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