blob: ef8d0973d36caf100b94a28def2a64eba5ee6991 [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
31 private static final ColorMatrix sTempSaturationMatrix = new ColorMatrix();
32 private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix();
33
34 private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035 private Bitmap mBitmap;
Winson Chung29d6fea2010-12-01 15:47:31 -080036 private int mAlpha;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070037
38 private float mSatutation = 1;
39 private int mBrightness = 0;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040
41 FastBitmapDrawable(Bitmap b) {
Winson Chungeeb5bbc2013-11-13 15:47:05 -080042 mAlpha = 255;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043 mBitmap = b;
Winson Chung268f1c52013-11-18 14:04:41 -080044 setBounds(0, 0, b.getWidth(), b.getHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045 }
46
47 @Override
48 public void draw(Canvas canvas) {
Winson Chung45e1d6e2010-11-09 17:19:49 -080049 final Rect r = getBounds();
Winson Chung8a196352013-05-28 16:12:55 -070050 // Draw the bitmap into the bounding rect
51 canvas.drawBitmap(mBitmap, null, r, mPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052 }
53
54 @Override
Adam Cohenbadf71e2011-05-26 19:08:29 -070055 public void setColorFilter(ColorFilter cf) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070056 // No op
Adam Cohenbadf71e2011-05-26 19:08:29 -070057 }
58
59 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060 public int getOpacity() {
61 return PixelFormat.TRANSLUCENT;
62 }
63
64 @Override
65 public void setAlpha(int alpha) {
Winson Chung29d6fea2010-12-01 15:47:31 -080066 mAlpha = alpha;
Winson Chungb3347bb2010-08-19 14:51:28 -070067 mPaint.setAlpha(alpha);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068 }
69
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070070 @Override
Adam Cohen76fc0852011-06-17 13:26:23 -070071 public void setFilterBitmap(boolean filterBitmap) {
72 mPaint.setFilterBitmap(filterBitmap);
Winson Chung6e1c0d32013-10-25 15:24:24 -070073 mPaint.setAntiAlias(filterBitmap);
Adam Cohen76fc0852011-06-17 13:26:23 -070074 }
75
Winson Chung29d6fea2010-12-01 15:47:31 -080076 public int getAlpha() {
77 return mAlpha;
78 }
79
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 public int getIntrinsicWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -080082 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 }
84
85 @Override
86 public int getIntrinsicHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -080087 return getBounds().height();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 }
89
90 @Override
91 public int getMinimumWidth() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -080092 return getBounds().width();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 }
94
95 @Override
96 public int getMinimumHeight() {
Winson Chungeeb5bbc2013-11-13 15:47:05 -080097 return getBounds().height();
Joe Onorato0589f0f2010-02-08 13:44:00 -080098 }
99
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 public Bitmap getBitmap() {
101 return mBitmap;
102 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700103
104 public float getSaturation() {
105 return mSatutation;
106 }
107
108 public void setSaturation(float saturation) {
109 mSatutation = saturation;
110 updateFilter();
111 }
112
113 public int getBrightness() {
114 return mBrightness;
115 }
116
117 public void addBrightness(int amount) {
118 mBrightness += amount;
119 updateFilter();
120 }
121
122 public void setBrightness(int brightness) {
123 mBrightness = brightness;
124 updateFilter();
125 }
126
127 private void updateFilter() {
128 if (mSatutation != 1 || mBrightness != 0) {
129 sTempSaturationMatrix.setSaturation(mSatutation);
130
131 if (mBrightness != 0) {
132 // Brightness: C-new = C-old*(1-amount) + amount
133 float scale = 1 - mBrightness / 255.0f;
134 sTempBrightnessMatrix.setScale(scale, scale, scale, 1);
135 float[] array = sTempBrightnessMatrix.getArray();
136
137 // Add the amount to RGB components of the matrix, as per the above formula.
138 // Fifth elements in the array correspond to the constant being added to
139 // red, blue, green, and alpha channel respectively.
140 array[4] = mBrightness;
141 array[9] = mBrightness;
142 array[14] = mBrightness;
143 sTempSaturationMatrix.preConcat(sTempBrightnessMatrix);
144 }
145 mPaint.setColorFilter(new ColorMatrixColorFilter(sTempSaturationMatrix));
146 } else {
147 mPaint.setColorFilter(null);
148 }
149 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150}