Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [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; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 18 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 19 | import android.content.Context; |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 20 | import android.content.res.Resources; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
| 22 | import android.graphics.BlurMaskFilter; |
| 23 | import android.graphics.Canvas; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 24 | import android.graphics.Color; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 25 | import android.graphics.Paint; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 26 | import android.graphics.PorterDuff; |
| 27 | import android.graphics.PorterDuffXfermode; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 28 | import android.graphics.Rect; |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
| 30 | import android.util.SparseArray; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 31 | |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Utility class to generate shadow and outline effect, which are used for click feedback |
| 34 | * and drag-n-drop respectively. |
| 35 | */ |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 36 | public class HolographicOutlineHelper { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 37 | |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 38 | private static HolographicOutlineHelper sInstance; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 39 | |
| 40 | private final Canvas mCanvas = new Canvas(); |
| 41 | private final Paint mDrawPaint = new Paint(); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 42 | private final Paint mBlurPaint = new Paint(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 43 | private final Paint mErasePaint = new Paint(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 44 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 45 | private final BlurMaskFilter mMediumOuterBlurMaskFilter; |
| 46 | private final BlurMaskFilter mThinOuterBlurMaskFilter; |
| 47 | private final BlurMaskFilter mMediumInnerBlurMaskFilter; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 48 | |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 49 | private final BlurMaskFilter mShadowBlurMaskFilter; |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 50 | |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 51 | // We have 4 different icon sizes: homescreen, hotseat, folder & all-apps |
| 52 | private final SparseArray<Bitmap> mBitmapCache = new SparseArray<>(4); |
Daniel Sandler | e572fe4 | 2013-06-12 22:46:02 -0400 | [diff] [blame] | 53 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 54 | private HolographicOutlineHelper(Context context) { |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 55 | Resources res = context.getResources(); |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 56 | |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 57 | float mediumBlur = res.getDimension(R.dimen.blur_size_medium_outline); |
| 58 | mMediumOuterBlurMaskFilter = new BlurMaskFilter(mediumBlur, BlurMaskFilter.Blur.OUTER); |
| 59 | mMediumInnerBlurMaskFilter = new BlurMaskFilter(mediumBlur, BlurMaskFilter.Blur.NORMAL); |
Daniel Sandler | e572fe4 | 2013-06-12 22:46:02 -0400 | [diff] [blame] | 60 | |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 61 | mThinOuterBlurMaskFilter = new BlurMaskFilter( |
| 62 | res.getDimension(R.dimen.blur_size_thin_outline), BlurMaskFilter.Blur.OUTER); |
| 63 | |
| 64 | mShadowBlurMaskFilter = new BlurMaskFilter( |
| 65 | res.getDimension(R.dimen.blur_size_click_shadow), BlurMaskFilter.Blur.NORMAL); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 66 | |
| 67 | mDrawPaint.setFilterBitmap(true); |
| 68 | mDrawPaint.setAntiAlias(true); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 69 | mBlurPaint.setFilterBitmap(true); |
| 70 | mBlurPaint.setAntiAlias(true); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 71 | mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); |
| 72 | mErasePaint.setFilterBitmap(true); |
| 73 | mErasePaint.setAntiAlias(true); |
| 74 | } |
| 75 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 76 | public static HolographicOutlineHelper obtain(Context context) { |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 77 | if (sInstance == null) { |
| 78 | sInstance = new HolographicOutlineHelper(context); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 79 | } |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 80 | return sInstance; |
Daniel Sandler | e572fe4 | 2013-06-12 22:46:02 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 83 | /** |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 84 | * Applies a more expensive and accurate outline to whatever is currently drawn in a specified |
| 85 | * bitmap. |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 86 | */ |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 87 | void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 88 | int outlineColor) { |
| 89 | applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, true); |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 90 | } |
| 91 | void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 92 | int outlineColor, boolean clipAlpha) { |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 93 | |
| 94 | // We start by removing most of the alpha channel so as to ignore shadows, and |
| 95 | // other types of partial transparency when defining the shape of the object |
Michael Jurka | 8c3339b | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 96 | if (clipAlpha) { |
| 97 | int[] srcBuffer = new int[srcDst.getWidth() * srcDst.getHeight()]; |
| 98 | srcDst.getPixels(srcBuffer, |
| 99 | 0, srcDst.getWidth(), 0, 0, srcDst.getWidth(), srcDst.getHeight()); |
| 100 | for (int i = 0; i < srcBuffer.length; i++) { |
| 101 | final int alpha = srcBuffer[i] >>> 24; |
| 102 | if (alpha < 188) { |
| 103 | srcBuffer[i] = 0; |
| 104 | } |
| 105 | } |
| 106 | srcDst.setPixels(srcBuffer, |
| 107 | 0, srcDst.getWidth(), 0, 0, srcDst.getWidth(), srcDst.getHeight()); |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 108 | } |
Michael Jurka | 8c3339b | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 109 | Bitmap glowShape = srcDst.extractAlpha(); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 110 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 111 | // calculate the outer blur first |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 112 | mBlurPaint.setMaskFilter(mMediumOuterBlurMaskFilter); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 113 | int[] outerBlurOffset = new int[2]; |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 114 | Bitmap thickOuterBlur = glowShape.extractAlpha(mBlurPaint, outerBlurOffset); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 115 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 116 | mBlurPaint.setMaskFilter(mThinOuterBlurMaskFilter); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 117 | int[] brightOutlineOffset = new int[2]; |
| 118 | Bitmap brightOutline = glowShape.extractAlpha(mBlurPaint, brightOutlineOffset); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 119 | |
| 120 | // calculate the inner blur |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 121 | srcDstCanvas.setBitmap(glowShape); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 122 | srcDstCanvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 123 | mBlurPaint.setMaskFilter(mMediumInnerBlurMaskFilter); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 124 | int[] thickInnerBlurOffset = new int[2]; |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 125 | Bitmap thickInnerBlur = glowShape.extractAlpha(mBlurPaint, thickInnerBlurOffset); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 126 | |
| 127 | // mask out the inner blur |
| 128 | srcDstCanvas.setBitmap(thickInnerBlur); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 129 | srcDstCanvas.drawBitmap(glowShape, -thickInnerBlurOffset[0], |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 130 | -thickInnerBlurOffset[1], mErasePaint); |
| 131 | srcDstCanvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(), |
| 132 | mErasePaint); |
| 133 | srcDstCanvas.drawRect(0, 0, thickInnerBlur.getWidth(), -thickInnerBlurOffset[1], |
| 134 | mErasePaint); |
| 135 | |
| 136 | // draw the inner and outer blur |
| 137 | srcDstCanvas.setBitmap(srcDst); |
Michael Jurka | 2a9e706 | 2011-01-14 15:48:04 -0800 | [diff] [blame] | 138 | srcDstCanvas.drawColor(0, PorterDuff.Mode.CLEAR); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 139 | mDrawPaint.setColor(color); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 140 | srcDstCanvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1], |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 141 | mDrawPaint); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 142 | srcDstCanvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1], |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 143 | mDrawPaint); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 144 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 145 | // draw the bright outline |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 146 | mDrawPaint.setColor(outlineColor); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 147 | srcDstCanvas.drawBitmap(brightOutline, brightOutlineOffset[0], brightOutlineOffset[1], |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 148 | mDrawPaint); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 149 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 150 | // cleanup |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 151 | srcDstCanvas.setBitmap(null); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 152 | brightOutline.recycle(); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 153 | thickOuterBlur.recycle(); |
| 154 | thickInnerBlur.recycle(); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 155 | glowShape.recycle(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 156 | } |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 157 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 158 | Bitmap createMediumDropShadow(BubbleTextView view) { |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 159 | Drawable icon = view.getIcon(); |
Sunny Goyal | 5537faa | 2015-05-19 17:35:07 -0700 | [diff] [blame] | 160 | if (icon == null) { |
| 161 | return null; |
| 162 | } |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 163 | Rect rect = icon.getBounds(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 164 | |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 165 | int bitmapWidth = (int) (rect.width() * view.getScaleX()); |
| 166 | int bitmapHeight = (int) (rect.height() * view.getScaleY()); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 167 | |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 168 | int key = (bitmapWidth << 16) | bitmapHeight; |
| 169 | Bitmap cache = mBitmapCache.get(key); |
| 170 | if (cache == null) { |
| 171 | cache = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888); |
| 172 | mCanvas.setBitmap(cache); |
| 173 | mBitmapCache.put(key, cache); |
| 174 | } else { |
| 175 | mCanvas.setBitmap(cache); |
| 176 | mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); |
| 177 | } |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 178 | |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 179 | mCanvas.save(Canvas.MATRIX_SAVE_FLAG); |
| 180 | mCanvas.scale(view.getScaleX(), view.getScaleY()); |
| 181 | mCanvas.translate(-rect.left, -rect.top); |
| 182 | icon.draw(mCanvas); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 183 | mCanvas.restore(); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 184 | mCanvas.setBitmap(null); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 185 | |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame] | 186 | mBlurPaint.setMaskFilter(mShadowBlurMaskFilter); |
| 187 | return cache.extractAlpha(mBlurPaint, null); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 188 | } |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 189 | } |