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