The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 19 | import android.graphics.Bitmap; |
| 20 | import android.graphics.Canvas; |
| 21 | import android.graphics.ColorFilter; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 22 | import android.graphics.Paint; |
| 23 | import android.graphics.PixelFormat; |
| 24 | import android.graphics.Rect; |
| 25 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | |
| 27 | class FastBitmapDrawable extends Drawable { |
| 28 | private Bitmap mBitmap; |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 29 | private int mAlpha; |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 30 | private int mWidth; |
| 31 | private int mHeight; |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 32 | private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 33 | |
| 34 | FastBitmapDrawable(Bitmap b) { |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 35 | mAlpha = 255; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | mBitmap = b; |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 37 | if (b != null) { |
| 38 | mWidth = mBitmap.getWidth(); |
| 39 | mHeight = mBitmap.getHeight(); |
| 40 | } else { |
| 41 | mWidth = mHeight = 0; |
| 42 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void draw(Canvas canvas) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 47 | final Rect r = getBounds(); |
Winson Chung | 8a19635 | 2013-05-28 16:12:55 -0700 | [diff] [blame] | 48 | // Draw the bitmap into the bounding rect |
| 49 | canvas.drawBitmap(mBitmap, null, r, mPaint); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 53 | public void setColorFilter(ColorFilter cf) { |
| 54 | mPaint.setColorFilter(cf); |
| 55 | } |
| 56 | |
| 57 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 58 | public int getOpacity() { |
| 59 | return PixelFormat.TRANSLUCENT; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void setAlpha(int alpha) { |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 64 | mAlpha = alpha; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 65 | mPaint.setAlpha(alpha); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 68 | public void setFilterBitmap(boolean filterBitmap) { |
| 69 | mPaint.setFilterBitmap(filterBitmap); |
| 70 | } |
| 71 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 72 | public int getAlpha() { |
| 73 | return mAlpha; |
| 74 | } |
| 75 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 76 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 77 | public int getIntrinsicWidth() { |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 78 | return mWidth; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public int getIntrinsicHeight() { |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 83 | return mHeight; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int getMinimumWidth() { |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 88 | return mWidth; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public int getMinimumHeight() { |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 93 | return mHeight; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 96 | public void setBitmap(Bitmap b) { |
| 97 | mBitmap = b; |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 98 | if (b != null) { |
| 99 | mWidth = mBitmap.getWidth(); |
| 100 | mHeight = mBitmap.getHeight(); |
| 101 | } else { |
| 102 | mWidth = mHeight = 0; |
| 103 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 104 | } |
| 105 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 106 | public Bitmap getBitmap() { |
| 107 | return mBitmap; |
| 108 | } |
| 109 | } |