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; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame^] | 22 | import android.graphics.ColorMatrix; |
| 23 | import android.graphics.ColorMatrixColorFilter; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 24 | import android.graphics.Paint; |
| 25 | import android.graphics.PixelFormat; |
| 26 | import android.graphics.Rect; |
| 27 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 28 | |
| 29 | class FastBitmapDrawable extends Drawable { |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame^] | 30 | |
| 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 35 | private Bitmap mBitmap; |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 36 | private int mAlpha; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame^] | 37 | |
| 38 | private float mSatutation = 1; |
| 39 | private int mBrightness = 0; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 40 | |
| 41 | FastBitmapDrawable(Bitmap b) { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 42 | mAlpha = 255; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | mBitmap = b; |
Winson Chung | 268f1c5 | 2013-11-18 14:04:41 -0800 | [diff] [blame] | 44 | setBounds(0, 0, b.getWidth(), b.getHeight()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public void draw(Canvas canvas) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 49 | final Rect r = getBounds(); |
Winson Chung | 8a19635 | 2013-05-28 16:12:55 -0700 | [diff] [blame] | 50 | // Draw the bitmap into the bounding rect |
| 51 | canvas.drawBitmap(mBitmap, null, r, mPaint); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 55 | public void setColorFilter(ColorFilter cf) { |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame^] | 56 | // No op |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 60 | public int getOpacity() { |
| 61 | return PixelFormat.TRANSLUCENT; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void setAlpha(int alpha) { |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 66 | mAlpha = alpha; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 67 | mPaint.setAlpha(alpha); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame^] | 70 | @Override |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 71 | public void setFilterBitmap(boolean filterBitmap) { |
| 72 | mPaint.setFilterBitmap(filterBitmap); |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 73 | mPaint.setAntiAlias(filterBitmap); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 76 | public int getAlpha() { |
| 77 | return mAlpha; |
| 78 | } |
| 79 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 80 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 81 | public int getIntrinsicWidth() { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 82 | return getBounds().width(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public int getIntrinsicHeight() { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 87 | return getBounds().height(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int getMinimumWidth() { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 92 | return getBounds().width(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public int getMinimumHeight() { |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 97 | return getBounds().height(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 98 | } |
| 99 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 100 | public Bitmap getBitmap() { |
| 101 | return mBitmap; |
| 102 | } |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame^] | 103 | |
| 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 150 | } |