blob: cf7c22eef62b7e36f3f2f599cc393beeae1af926 [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;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070022import android.graphics.ColorMatrix;
23import android.graphics.ColorMatrixColorFilter;
Winson Chung45e1d6e2010-11-09 17:19:49 -080024import android.graphics.Paint;
25import android.graphics.PixelFormat;
26import android.graphics.Rect;
27import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028
29class FastBitmapDrawable extends Drawable {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070030
Sunny Goyal95abbb32014-08-04 10:53:22 -070031 private static ColorMatrix sGhostModeMatrix;
32 private static final ColorMatrix sTempMatrix = new ColorMatrix();
33
34 private static final int GHOST_MODE_MIN_COLOR_RANGE = 130;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070035
36 private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037 private Bitmap mBitmap;
Winson Chung29d6fea2010-12-01 15:47:31 -080038 private int mAlpha;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070039
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070040 private int mBrightness = 0;
Sunny Goyal95abbb32014-08-04 10:53:22 -070041 private boolean mGhostModeEnabled = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042
43 FastBitmapDrawable(Bitmap b) {
Winson Chungeeb5bbc2013-11-13 15:47:05 -080044 mAlpha = 255;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045 mBitmap = b;
Winson Chung268f1c52013-11-18 14:04:41 -080046 setBounds(0, 0, b.getWidth(), b.getHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047 }
48
49 @Override
50 public void draw(Canvas canvas) {
Winson Chung45e1d6e2010-11-09 17:19:49 -080051 final Rect r = getBounds();
Winson Chung8a196352013-05-28 16:12:55 -070052 // Draw the bitmap into the bounding rect
53 canvas.drawBitmap(mBitmap, null, r, mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 }
55
56 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -070057 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070058 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -070059 }
60
61 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062 public int getOpacity() {
63 return PixelFormat.TRANSLUCENT;
64 }
65
66 @Override
67 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -080068 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -070069 mPaint.setAlpha(alpha);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 }
71
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070072 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -070073 public void setFilterBitmap(boolean filterBitmap) {
74 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -070075 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -070076 }
77
Winson Chung29d6fea2010-12-01 15:47:31 -080078 public int getAlpha() {
79 return mAlpha;
80 }
81
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 public int getIntrinsicWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -080084 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 }
86
87 @Override
88 public int getIntrinsicHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -080089 return getBounds().height();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 }
91
92 @Override
93 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -080094 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 }
96
97 @Override
98 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -080099 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800100 }
101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 public Bitmap getBitmap() {
103 return mBitmap;
104 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700105
Sunny Goyal95abbb32014-08-04 10:53:22 -0700106 /**
107 * When enabled, the icon is grayed out and the contrast is increased to give it a 'ghost'
108 * appearance.
109 */
110 public void setGhostModeEnabled(boolean enabled) {
111 if (mGhostModeEnabled != enabled) {
112 mGhostModeEnabled = enabled;
113 updateFilter();
114 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700115 }
116
Sunny Goyal95abbb32014-08-04 10:53:22 -0700117 public boolean isGhostModeEnabled() {
118 return mGhostModeEnabled;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700119 }
120
121 public int getBrightness() {
122 return mBrightness;
123 }
124
125 public void addBrightness(int amount) {
Sunny Goyal95abbb32014-08-04 10:53:22 -0700126 setBrightness(mBrightness + amount);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700127 }
128
129 public void setBrightness(int brightness) {
Sunny Goyal95abbb32014-08-04 10:53:22 -0700130 if (mBrightness != brightness) {
131 mBrightness = brightness;
132 updateFilter();
133 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700134 }
135
136 private void updateFilter() {
Sunny Goyal95abbb32014-08-04 10:53:22 -0700137 if (mGhostModeEnabled) {
138 if (sGhostModeMatrix == null) {
139 sGhostModeMatrix = new ColorMatrix();
140 sGhostModeMatrix.setSaturation(0);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700141
Sunny Goyal95abbb32014-08-04 10:53:22 -0700142 // For ghost mode, set the color range to [GHOST_MODE_MIN_COLOR_RANGE, 255]
143 float range = (255 - GHOST_MODE_MIN_COLOR_RANGE) / 255.0f;
144 sTempMatrix.set(new float[] {
145 range, 0, 0, 0, GHOST_MODE_MIN_COLOR_RANGE,
146 0, range, 0, 0, GHOST_MODE_MIN_COLOR_RANGE,
147 0, 0, range, 0, GHOST_MODE_MIN_COLOR_RANGE,
148 0, 0, 0, 1, 0 });
149 sGhostModeMatrix.preConcat(sTempMatrix);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700150 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700151
152 if (mBrightness == 0) {
153 mPaint.setColorFilter(new ColorMatrixColorFilter(sGhostModeMatrix));
154 } else {
155 setBrightnessMatrix(sTempMatrix, mBrightness);
156 sTempMatrix.postConcat(sGhostModeMatrix);
157 mPaint.setColorFilter(new ColorMatrixColorFilter(sTempMatrix));
158 }
159 } else if (mBrightness != 0) {
160 setBrightnessMatrix(sTempMatrix, mBrightness);
161 mPaint.setColorFilter(new ColorMatrixColorFilter(sTempMatrix));
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700162 } else {
163 mPaint.setColorFilter(null);
164 }
165 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700166
167 private static void setBrightnessMatrix(ColorMatrix matrix, int brightness) {
168 // Brightness: C-new = C-old*(1-amount) + amount
169 float scale = 1 - brightness / 255.0f;
170 matrix.setScale(scale, scale, scale, 1);
171 float[] array = matrix.getArray();
172
173 // Add the amount to RGB components of the matrix, as per the above formula.
174 // Fifth elements in the array correspond to the constant being added to
175 // red, blue, green, and alpha channel respectively.
176 array[4] = brightness;
177 array[9] = brightness;
178 array[14] = brightness;
179 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180}