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 | */ |
| 84 | public float highlightAlphaInterpolator(float r) { |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 85 | float maxAlpha = 0.8f; |
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 | */ |
| 92 | public 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) { |
| 125 | |
| 126 | // We start by removing most of the alpha channel so as to ignore shadows, and |
| 127 | // other types of partial transparency when defining the shape of the object |
| 128 | Bitmap glowShape = srcDst.extractAlpha(mAlphaClipPaint, mTempOffset); |
| 129 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 130 | // calculate the outer blur first |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 131 | BlurMaskFilter outerBlurMaskFilter; |
| 132 | switch (thickness) { |
| 133 | case EXTRA_THICK: |
| 134 | outerBlurMaskFilter = sExtraThickOuterBlurMaskFilter; |
| 135 | break; |
| 136 | case THICK: |
| 137 | outerBlurMaskFilter = sThickOuterBlurMaskFilter; |
| 138 | break; |
| 139 | case MEDIUM: |
| 140 | outerBlurMaskFilter = sMediumOuterBlurMaskFilter; |
| 141 | break; |
| 142 | default: |
| 143 | throw new RuntimeException("Invalid blur thickness"); |
| 144 | } |
| 145 | mBlurPaint.setMaskFilter(outerBlurMaskFilter); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 146 | int[] outerBlurOffset = new int[2]; |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 147 | Bitmap thickOuterBlur = glowShape.extractAlpha(mBlurPaint, outerBlurOffset); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 148 | if (thickness == EXTRA_THICK) { |
| 149 | mBlurPaint.setMaskFilter(sMediumOuterBlurMaskFilter); |
| 150 | } else { |
| 151 | mBlurPaint.setMaskFilter(sThinOuterBlurMaskFilter); |
| 152 | } |
| 153 | |
| 154 | int[] brightOutlineOffset = new int[2]; |
| 155 | Bitmap brightOutline = glowShape.extractAlpha(mBlurPaint, brightOutlineOffset); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 156 | |
| 157 | // calculate the inner blur |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 158 | srcDstCanvas.setBitmap(glowShape); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 159 | srcDstCanvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 160 | BlurMaskFilter innerBlurMaskFilter; |
| 161 | switch (thickness) { |
| 162 | case EXTRA_THICK: |
| 163 | innerBlurMaskFilter = sExtraThickInnerBlurMaskFilter; |
| 164 | break; |
| 165 | case THICK: |
| 166 | innerBlurMaskFilter = sThickInnerBlurMaskFilter; |
| 167 | break; |
| 168 | case MEDIUM: |
| 169 | innerBlurMaskFilter = sMediumInnerBlurMaskFilter; |
| 170 | break; |
| 171 | default: |
| 172 | throw new RuntimeException("Invalid blur thickness"); |
| 173 | } |
| 174 | mBlurPaint.setMaskFilter(innerBlurMaskFilter); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 175 | int[] thickInnerBlurOffset = new int[2]; |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 176 | Bitmap thickInnerBlur = glowShape.extractAlpha(mBlurPaint, thickInnerBlurOffset); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 177 | |
| 178 | // mask out the inner blur |
| 179 | srcDstCanvas.setBitmap(thickInnerBlur); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 180 | srcDstCanvas.drawBitmap(glowShape, -thickInnerBlurOffset[0], |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 181 | -thickInnerBlurOffset[1], mErasePaint); |
| 182 | srcDstCanvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(), |
| 183 | mErasePaint); |
| 184 | srcDstCanvas.drawRect(0, 0, thickInnerBlur.getWidth(), -thickInnerBlurOffset[1], |
| 185 | mErasePaint); |
| 186 | |
| 187 | // draw the inner and outer blur |
| 188 | srcDstCanvas.setBitmap(srcDst); |
Michael Jurka | 2a9e706 | 2011-01-14 15:48:04 -0800 | [diff] [blame] | 189 | srcDstCanvas.drawColor(0, PorterDuff.Mode.CLEAR); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 190 | mHolographicPaint.setColor(color); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 191 | srcDstCanvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1], |
| 192 | mHolographicPaint); |
| 193 | srcDstCanvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1], |
| 194 | mHolographicPaint); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 195 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 196 | // draw the bright outline |
| 197 | mHolographicPaint.setColor(outlineColor); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 198 | srcDstCanvas.drawBitmap(brightOutline, brightOutlineOffset[0], brightOutlineOffset[1], |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 199 | mHolographicPaint); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 200 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 201 | // cleanup |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 202 | brightOutline.recycle(); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 203 | thickOuterBlur.recycle(); |
| 204 | thickInnerBlur.recycle(); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 205 | glowShape.recycle(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 206 | } |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 207 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 208 | void applyExtraThickExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
| 209 | int outlineColor) { |
| 210 | applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, EXTRA_THICK); |
| 211 | } |
| 212 | |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 213 | void applyThickExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
| 214 | int outlineColor) { |
| 215 | applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, THICK); |
| 216 | } |
| 217 | |
| 218 | void applyMediumExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
| 219 | int outlineColor) { |
| 220 | applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, MEDIUM); |
| 221 | } |
| 222 | |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 223 | } |