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; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.BlurMaskFilter; |
| 22 | import android.graphics.Canvas; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 23 | import android.graphics.Color; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 24 | import android.graphics.Paint; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 25 | import android.graphics.PorterDuff; |
| 26 | import android.graphics.PorterDuffXfermode; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 27 | import android.graphics.Rect; |
| 28 | import android.graphics.Region.Op; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 29 | |
| 30 | public class HolographicOutlineHelper { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 31 | |
| 32 | private static final Rect sTempRect = new Rect(); |
| 33 | |
| 34 | private final Canvas mCanvas = new Canvas(); |
| 35 | private final Paint mDrawPaint = new Paint(); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 36 | private final Paint mBlurPaint = new Paint(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 37 | private final Paint mErasePaint = new Paint(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 38 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 39 | private final BlurMaskFilter mMediumOuterBlurMaskFilter; |
| 40 | private final BlurMaskFilter mThinOuterBlurMaskFilter; |
| 41 | private final BlurMaskFilter mMediumInnerBlurMaskFilter; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 42 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 43 | private final BlurMaskFilter mShaowBlurMaskFilter; |
| 44 | private final int mShadowOffset; |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 45 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Padding used when creating shadow bitmap; |
| 48 | */ |
| 49 | final int shadowBitmapPadding; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 50 | |
Daniel Sandler | e572fe4 | 2013-06-12 22:46:02 -0400 | [diff] [blame] | 51 | static HolographicOutlineHelper INSTANCE; |
| 52 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 53 | private HolographicOutlineHelper(Context context) { |
| 54 | final float scale = LauncherAppState.getInstance().getScreenDensity(); |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 55 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 56 | mMediumOuterBlurMaskFilter = new BlurMaskFilter(scale * 2.0f, BlurMaskFilter.Blur.OUTER); |
| 57 | mThinOuterBlurMaskFilter = new BlurMaskFilter(scale * 1.0f, BlurMaskFilter.Blur.OUTER); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 58 | mMediumInnerBlurMaskFilter = new BlurMaskFilter(scale * 2.0f, BlurMaskFilter.Blur.NORMAL); |
Daniel Sandler | e572fe4 | 2013-06-12 22:46:02 -0400 | [diff] [blame] | 59 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 60 | mShaowBlurMaskFilter = new BlurMaskFilter(scale * 4.0f, BlurMaskFilter.Blur.NORMAL); |
| 61 | mShadowOffset = (int) (scale * 2.0f); |
| 62 | shadowBitmapPadding = (int) (scale * 4.0f); |
| 63 | |
| 64 | mDrawPaint.setFilterBitmap(true); |
| 65 | mDrawPaint.setAntiAlias(true); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 66 | mBlurPaint.setFilterBitmap(true); |
| 67 | mBlurPaint.setAntiAlias(true); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 68 | mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); |
| 69 | mErasePaint.setFilterBitmap(true); |
| 70 | mErasePaint.setAntiAlias(true); |
| 71 | } |
| 72 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 73 | public static HolographicOutlineHelper obtain(Context context) { |
| 74 | if (INSTANCE == null) { |
| 75 | INSTANCE = new HolographicOutlineHelper(context); |
| 76 | } |
Daniel Sandler | e572fe4 | 2013-06-12 22:46:02 -0400 | [diff] [blame] | 77 | return INSTANCE; |
| 78 | } |
| 79 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 80 | /** |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 81 | * Applies a more expensive and accurate outline to whatever is currently drawn in a specified |
| 82 | * bitmap. |
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 | void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 85 | int outlineColor) { |
| 86 | applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, true); |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 87 | } |
| 88 | void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 89 | int outlineColor, boolean clipAlpha) { |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 90 | |
| 91 | // We start by removing most of the alpha channel so as to ignore shadows, and |
| 92 | // other types of partial transparency when defining the shape of the object |
Michael Jurka | 8c3339b | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 93 | if (clipAlpha) { |
| 94 | int[] srcBuffer = new int[srcDst.getWidth() * srcDst.getHeight()]; |
| 95 | srcDst.getPixels(srcBuffer, |
| 96 | 0, srcDst.getWidth(), 0, 0, srcDst.getWidth(), srcDst.getHeight()); |
| 97 | for (int i = 0; i < srcBuffer.length; i++) { |
| 98 | final int alpha = srcBuffer[i] >>> 24; |
| 99 | if (alpha < 188) { |
| 100 | srcBuffer[i] = 0; |
| 101 | } |
| 102 | } |
| 103 | srcDst.setPixels(srcBuffer, |
| 104 | 0, srcDst.getWidth(), 0, 0, srcDst.getWidth(), srcDst.getHeight()); |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 105 | } |
Michael Jurka | 8c3339b | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 106 | Bitmap glowShape = srcDst.extractAlpha(); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 107 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 108 | // calculate the outer blur first |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 109 | mBlurPaint.setMaskFilter(mMediumOuterBlurMaskFilter); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 110 | int[] outerBlurOffset = new int[2]; |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 111 | Bitmap thickOuterBlur = glowShape.extractAlpha(mBlurPaint, outerBlurOffset); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 112 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 113 | mBlurPaint.setMaskFilter(mThinOuterBlurMaskFilter); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 114 | int[] brightOutlineOffset = new int[2]; |
| 115 | Bitmap brightOutline = glowShape.extractAlpha(mBlurPaint, brightOutlineOffset); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 116 | |
| 117 | // calculate the inner blur |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 118 | srcDstCanvas.setBitmap(glowShape); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 119 | srcDstCanvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 120 | mBlurPaint.setMaskFilter(mMediumInnerBlurMaskFilter); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 121 | int[] thickInnerBlurOffset = new int[2]; |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 122 | Bitmap thickInnerBlur = glowShape.extractAlpha(mBlurPaint, thickInnerBlurOffset); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 123 | |
| 124 | // mask out the inner blur |
| 125 | srcDstCanvas.setBitmap(thickInnerBlur); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 126 | srcDstCanvas.drawBitmap(glowShape, -thickInnerBlurOffset[0], |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 127 | -thickInnerBlurOffset[1], mErasePaint); |
| 128 | srcDstCanvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(), |
| 129 | mErasePaint); |
| 130 | srcDstCanvas.drawRect(0, 0, thickInnerBlur.getWidth(), -thickInnerBlurOffset[1], |
| 131 | mErasePaint); |
| 132 | |
| 133 | // draw the inner and outer blur |
| 134 | srcDstCanvas.setBitmap(srcDst); |
Michael Jurka | 2a9e706 | 2011-01-14 15:48:04 -0800 | [diff] [blame] | 135 | srcDstCanvas.drawColor(0, PorterDuff.Mode.CLEAR); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 136 | mDrawPaint.setColor(color); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 137 | srcDstCanvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1], |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 138 | mDrawPaint); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 139 | srcDstCanvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1], |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 140 | mDrawPaint); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 141 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 142 | // draw the bright outline |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 143 | mDrawPaint.setColor(outlineColor); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 144 | srcDstCanvas.drawBitmap(brightOutline, brightOutlineOffset[0], brightOutlineOffset[1], |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 145 | mDrawPaint); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 146 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 147 | // cleanup |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 148 | srcDstCanvas.setBitmap(null); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 149 | brightOutline.recycle(); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 150 | thickOuterBlur.recycle(); |
| 151 | thickInnerBlur.recycle(); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 152 | glowShape.recycle(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 153 | } |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 154 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 155 | Bitmap createMediumDropShadow(BubbleTextView view) { |
| 156 | final Bitmap result = Bitmap.createBitmap( |
| 157 | view.getWidth() + shadowBitmapPadding + shadowBitmapPadding, |
| 158 | view.getHeight() + shadowBitmapPadding + shadowBitmapPadding + mShadowOffset, |
| 159 | Bitmap.Config.ARGB_8888); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 160 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 161 | mCanvas.setBitmap(result); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 162 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 163 | final Rect clipRect = sTempRect; |
| 164 | view.getDrawingRect(sTempRect); |
| 165 | // adjust the clip rect so that we don't include the text label |
| 166 | clipRect.bottom = view.getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V |
| 167 | + view.getLayout().getLineTop(0); |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 168 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 169 | // Draw the View into the bitmap. |
| 170 | // The translate of scrollX and scrollY is necessary when drawing TextViews, because |
| 171 | // they set scrollX and scrollY to large values to achieve centered text |
| 172 | mCanvas.save(); |
| 173 | mCanvas.scale(view.getScaleX(), view.getScaleY(), |
| 174 | view.getWidth() / 2 + shadowBitmapPadding, |
| 175 | view.getHeight() / 2 + shadowBitmapPadding); |
| 176 | mCanvas.translate(-view.getScrollX() + shadowBitmapPadding, |
| 177 | -view.getScrollY() + shadowBitmapPadding); |
| 178 | mCanvas.clipRect(clipRect, Op.REPLACE); |
| 179 | view.draw(mCanvas); |
| 180 | mCanvas.restore(); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 181 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 182 | int[] blurOffst = new int[2]; |
| 183 | mBlurPaint.setMaskFilter(mShaowBlurMaskFilter); |
| 184 | Bitmap blurBitmap = result.extractAlpha(mBlurPaint, blurOffst); |
| 185 | |
| 186 | mCanvas.save(); |
| 187 | mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); |
| 188 | mCanvas.translate(blurOffst[0], blurOffst[1]); |
| 189 | |
| 190 | mDrawPaint.setColor(Color.BLACK); |
| 191 | mDrawPaint.setAlpha(30); |
| 192 | mCanvas.drawBitmap(blurBitmap, 0, 0, mDrawPaint); |
| 193 | |
| 194 | mDrawPaint.setAlpha(60); |
| 195 | mCanvas.drawBitmap(blurBitmap, 0, mShadowOffset, mDrawPaint); |
| 196 | mCanvas.restore(); |
| 197 | |
| 198 | mCanvas.setBitmap(null); |
| 199 | blurBitmap.recycle(); |
| 200 | |
| 201 | return result; |
| 202 | } |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 203 | } |