blob: 14760c7b696ad0f8c806250e27ed12d11c2f7ff2 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019import android.graphics.Bitmap;
20import android.graphics.Canvas;
21import android.graphics.ColorFilter;
Winson Chung45e1d6e2010-11-09 17:19:49 -080022import android.graphics.Paint;
23import android.graphics.PixelFormat;
24import android.graphics.Rect;
25import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026
27class FastBitmapDrawable extends Drawable {
28 private Bitmap mBitmap;
Winson Chung29d6fea2010-12-01 15:47:31 -080029 private int mAlpha;
Romain Guya28fd3f2010-03-15 14:44:42 -070030 private int mWidth;
31 private int mHeight;
Winson Chungeecf02d2012-03-02 17:14:58 -080032 private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033
34 FastBitmapDrawable(Bitmap b) {
Winson Chung29d6fea2010-12-01 15:47:31 -080035 mAlpha = 255;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036 mBitmap = b;
Romain Guya28fd3f2010-03-15 14:44:42 -070037 if (b != null) {
38 mWidth = mBitmap.getWidth();
39 mHeight = mBitmap.getHeight();
40 } else {
41 mWidth = mHeight = 0;
42 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043 }
44
45 @Override
46 public void draw(Canvas canvas) {
Winson Chung45e1d6e2010-11-09 17:19:49 -080047 final Rect r = getBounds();
Winson Chung8a196352013-05-28 16:12:55 -070048 // Draw the bitmap into the bounding rect
49 canvas.drawBitmap(mBitmap, null, r, mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 }
51
52 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -070053 public void setColorFilter(ColorFilter cf) {
54 mPaint.setColorFilter(cf);
55 }
56
57 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058 public int getOpacity() {
59 return PixelFormat.TRANSLUCENT;
60 }
61
62 @Override
63 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -080064 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -070065 mPaint.setAlpha(alpha);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066 }
67
Adam Cohen76fc0852011-06-17 13:26:23 -070068 public void setFilterBitmap(boolean filterBitmap) {
69 mPaint.setFilterBitmap(filterBitmap);
70 }
71
Winson Chung29d6fea2010-12-01 15:47:31 -080072 public int getAlpha() {
73 return mAlpha;
74 }
75
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077 public int getIntrinsicWidth() {
Romain Guya28fd3f2010-03-15 14:44:42 -070078 return mWidth;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 }
80
81 @Override
82 public int getIntrinsicHeight() {
Romain Guya28fd3f2010-03-15 14:44:42 -070083 return mHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 }
85
86 @Override
87 public int getMinimumWidth() {
Romain Guya28fd3f2010-03-15 14:44:42 -070088 return mWidth;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 }
90
91 @Override
92 public int getMinimumHeight() {
Romain Guya28fd3f2010-03-15 14:44:42 -070093 return mHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 }
95
Joe Onorato0589f0f2010-02-08 13:44:00 -080096 public void setBitmap(Bitmap b) {
97 mBitmap = b;
Romain Guya28fd3f2010-03-15 14:44:42 -070098 if (b != null) {
99 mWidth = mBitmap.getWidth();
100 mHeight = mBitmap.getHeight();
101 } else {
102 mWidth = mHeight = 0;
103 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800104 }
105
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 public Bitmap getBitmap() {
107 return mBitmap;
108 }
109}