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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import android.graphics.Bitmap; |
| 20 | import android.graphics.BlurMaskFilter; |
| 21 | import android.graphics.Canvas; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 22 | import android.graphics.MaskFilter; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 23 | import android.graphics.Paint; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 24 | import android.graphics.PorterDuff; |
| 25 | import android.graphics.PorterDuffXfermode; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 26 | import android.graphics.TableMaskFilter; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 27 | |
| 28 | public class HolographicOutlineHelper { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 29 | private final Paint mHolographicPaint = new Paint(); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 30 | private final Paint mBlurPaint = new Paint(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 31 | private final Paint mErasePaint = new Paint(); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 32 | private final Paint mAlphaClipPaint = new Paint(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 33 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 34 | public static final int MAX_OUTER_BLUR_RADIUS; |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 35 | public static final int MIN_OUTER_BLUR_RADIUS; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 36 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 37 | private static final BlurMaskFilter sExtraThickOuterBlurMaskFilter; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 38 | private static final BlurMaskFilter sThickOuterBlurMaskFilter; |
| 39 | private static final BlurMaskFilter sMediumOuterBlurMaskFilter; |
| 40 | private static final BlurMaskFilter sThinOuterBlurMaskFilter; |
| 41 | private static final BlurMaskFilter sThickInnerBlurMaskFilter; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 42 | private static final BlurMaskFilter sExtraThickInnerBlurMaskFilter; |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 43 | private static final BlurMaskFilter sMediumInnerBlurMaskFilter; |
| 44 | |
| 45 | private static final int THICK = 0; |
| 46 | private static final int MEDIUM = 1; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 47 | private static final int EXTRA_THICK = 2; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 48 | |
| 49 | static { |
| 50 | final float scale = LauncherApplication.getScreenDensity(); |
| 51 | |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 52 | MIN_OUTER_BLUR_RADIUS = (int) (scale * 1.0f); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 53 | MAX_OUTER_BLUR_RADIUS = (int) (scale * 12.0f); |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 54 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 55 | sExtraThickOuterBlurMaskFilter = new BlurMaskFilter(scale * 12.0f, BlurMaskFilter.Blur.OUTER); |
| 56 | sThickOuterBlurMaskFilter = new BlurMaskFilter(scale * 6.0f, BlurMaskFilter.Blur.OUTER); |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 57 | sMediumOuterBlurMaskFilter = new BlurMaskFilter(scale * 2.0f, BlurMaskFilter.Blur.OUTER); |
| 58 | sThinOuterBlurMaskFilter = new BlurMaskFilter(scale * 1.0f, BlurMaskFilter.Blur.OUTER); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 59 | sExtraThickInnerBlurMaskFilter = new BlurMaskFilter(scale * 6.0f, BlurMaskFilter.Blur.NORMAL); |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 60 | sThickInnerBlurMaskFilter = new BlurMaskFilter(scale * 4.0f, BlurMaskFilter.Blur.NORMAL); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 61 | sMediumInnerBlurMaskFilter = new BlurMaskFilter(scale * 2.0f, BlurMaskFilter.Blur.NORMAL); |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 62 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 63 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 64 | private static final MaskFilter sCoarseClipTable = TableMaskFilter.CreateClipTable(0, 200); |
| 65 | |
| 66 | private int[] mTempOffset = new int[2]; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 67 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 68 | HolographicOutlineHelper() { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 69 | mHolographicPaint.setFilterBitmap(true); |
| 70 | mHolographicPaint.setAntiAlias(true); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 71 | mBlurPaint.setFilterBitmap(true); |
| 72 | mBlurPaint.setAntiAlias(true); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 73 | mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); |
| 74 | mErasePaint.setFilterBitmap(true); |
| 75 | mErasePaint.setAntiAlias(true); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 76 | MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(180, 255); |
| 77 | mAlphaClipPaint.setMaskFilter(alphaClipTable); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Returns the interpolated holographic highlight alpha for the effect we want when scrolling |
| 82 | * pages. |
| 83 | */ |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 84 | public static float highlightAlphaInterpolator(float r) { |
| 85 | float maxAlpha = 0.6f; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 86 | return (float) Math.pow(maxAlpha * (1.0f - r), 1.5f); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the interpolated view alpha for the effect we want when scrolling pages. |
| 91 | */ |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 92 | public static float viewAlphaInterpolator(float r) { |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 93 | final float pivot = 0.95f; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 94 | if (r < pivot) { |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 95 | return (float) Math.pow(r / pivot, 1.5f); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 96 | } else { |
| 97 | return 1.0f; |
| 98 | } |
| 99 | } |
| 100 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Apply an outer blur to the given bitmap. |
| 103 | * You should use OUTER_BLUR_RADIUS to ensure that the bitmap is big enough to draw |
| 104 | * the blur without clipping. |
| 105 | */ |
| 106 | void applyOuterBlur(Bitmap bitmap, Canvas canvas, int color) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 107 | mBlurPaint.setMaskFilter(sThickOuterBlurMaskFilter); |
| 108 | Bitmap glow = bitmap.extractAlpha(mBlurPaint, mTempOffset); |
| 109 | |
| 110 | // Use the clip table to make the glow heavier closer to the outline |
| 111 | mHolographicPaint.setMaskFilter(sCoarseClipTable); |
| 112 | mHolographicPaint.setAlpha(150); |
| 113 | mHolographicPaint.setColor(color); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 114 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 115 | canvas.drawBitmap(glow, mTempOffset[0], mTempOffset[1], mHolographicPaint); |
| 116 | glow.recycle(); |
| 117 | } |
| 118 | |
| 119 | /** |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 120 | * Applies a more expensive and accurate outline to whatever is currently drawn in a specified |
| 121 | * bitmap. |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 122 | */ |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 123 | void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 124 | int outlineColor, int thickness) { |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame^] | 125 | applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, mAlphaClipPaint, |
| 126 | thickness); |
| 127 | } |
| 128 | void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
| 129 | int outlineColor, Paint alphaClipPaint, int thickness) { |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 130 | |
| 131 | // We start by removing most of the alpha channel so as to ignore shadows, and |
| 132 | // other types of partial transparency when defining the shape of the object |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame^] | 133 | if (alphaClipPaint == null) { |
| 134 | alphaClipPaint = mAlphaClipPaint; |
| 135 | } |
| 136 | Bitmap glowShape = srcDst.extractAlpha(alphaClipPaint, mTempOffset); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 137 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 138 | // calculate the outer blur first |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 139 | BlurMaskFilter outerBlurMaskFilter; |
| 140 | switch (thickness) { |
| 141 | case EXTRA_THICK: |
| 142 | outerBlurMaskFilter = sExtraThickOuterBlurMaskFilter; |
| 143 | break; |
| 144 | case THICK: |
| 145 | outerBlurMaskFilter = sThickOuterBlurMaskFilter; |
| 146 | break; |
| 147 | case MEDIUM: |
| 148 | outerBlurMaskFilter = sMediumOuterBlurMaskFilter; |
| 149 | break; |
| 150 | default: |
| 151 | throw new RuntimeException("Invalid blur thickness"); |
| 152 | } |
| 153 | mBlurPaint.setMaskFilter(outerBlurMaskFilter); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 154 | int[] outerBlurOffset = new int[2]; |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 155 | Bitmap thickOuterBlur = glowShape.extractAlpha(mBlurPaint, outerBlurOffset); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 156 | if (thickness == EXTRA_THICK) { |
| 157 | mBlurPaint.setMaskFilter(sMediumOuterBlurMaskFilter); |
| 158 | } else { |
| 159 | mBlurPaint.setMaskFilter(sThinOuterBlurMaskFilter); |
| 160 | } |
| 161 | |
| 162 | int[] brightOutlineOffset = new int[2]; |
| 163 | Bitmap brightOutline = glowShape.extractAlpha(mBlurPaint, brightOutlineOffset); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 164 | |
| 165 | // calculate the inner blur |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 166 | srcDstCanvas.setBitmap(glowShape); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 167 | srcDstCanvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 168 | BlurMaskFilter innerBlurMaskFilter; |
| 169 | switch (thickness) { |
| 170 | case EXTRA_THICK: |
| 171 | innerBlurMaskFilter = sExtraThickInnerBlurMaskFilter; |
| 172 | break; |
| 173 | case THICK: |
| 174 | innerBlurMaskFilter = sThickInnerBlurMaskFilter; |
| 175 | break; |
| 176 | case MEDIUM: |
| 177 | innerBlurMaskFilter = sMediumInnerBlurMaskFilter; |
| 178 | break; |
| 179 | default: |
| 180 | throw new RuntimeException("Invalid blur thickness"); |
| 181 | } |
| 182 | mBlurPaint.setMaskFilter(innerBlurMaskFilter); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 183 | int[] thickInnerBlurOffset = new int[2]; |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 184 | Bitmap thickInnerBlur = glowShape.extractAlpha(mBlurPaint, thickInnerBlurOffset); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 185 | |
| 186 | // mask out the inner blur |
| 187 | srcDstCanvas.setBitmap(thickInnerBlur); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 188 | srcDstCanvas.drawBitmap(glowShape, -thickInnerBlurOffset[0], |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 189 | -thickInnerBlurOffset[1], mErasePaint); |
| 190 | srcDstCanvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(), |
| 191 | mErasePaint); |
| 192 | srcDstCanvas.drawRect(0, 0, thickInnerBlur.getWidth(), -thickInnerBlurOffset[1], |
| 193 | mErasePaint); |
| 194 | |
| 195 | // draw the inner and outer blur |
| 196 | srcDstCanvas.setBitmap(srcDst); |
Michael Jurka | 2a9e706 | 2011-01-14 15:48:04 -0800 | [diff] [blame] | 197 | srcDstCanvas.drawColor(0, PorterDuff.Mode.CLEAR); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 198 | mHolographicPaint.setColor(color); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 199 | srcDstCanvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1], |
| 200 | mHolographicPaint); |
| 201 | srcDstCanvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1], |
| 202 | mHolographicPaint); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 203 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 204 | // draw the bright outline |
| 205 | mHolographicPaint.setColor(outlineColor); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 206 | srcDstCanvas.drawBitmap(brightOutline, brightOutlineOffset[0], brightOutlineOffset[1], |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 207 | mHolographicPaint); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 208 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 209 | // cleanup |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 210 | srcDstCanvas.setBitmap(null); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 211 | brightOutline.recycle(); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 212 | thickOuterBlur.recycle(); |
| 213 | thickInnerBlur.recycle(); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 214 | glowShape.recycle(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 215 | } |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 216 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 217 | void applyExtraThickExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
| 218 | int outlineColor) { |
| 219 | applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, EXTRA_THICK); |
| 220 | } |
| 221 | |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 222 | void applyThickExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
| 223 | int outlineColor) { |
| 224 | applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, THICK); |
| 225 | } |
| 226 | |
| 227 | void applyMediumExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame^] | 228 | int outlineColor, Paint alphaClipPaint) { |
| 229 | applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, alphaClipPaint, |
| 230 | MEDIUM); |
| 231 | } |
| 232 | |
| 233 | void applyMediumExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 234 | int outlineColor) { |
| 235 | applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, MEDIUM); |
| 236 | } |
| 237 | |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 238 | } |