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.Rect; |
| 27 | import android.graphics.TableMaskFilter; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 28 | |
| 29 | public class HolographicOutlineHelper { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 30 | private final Paint mHolographicPaint = new Paint(); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 31 | private final Paint mBlurPaint = new Paint(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 32 | private final Paint mErasePaint = new Paint(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 33 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 34 | private static final BlurMaskFilter sLargeGlowBlurMaskFilter = new BlurMaskFilter( |
| 35 | 10.0f, BlurMaskFilter.Blur.OUTER); |
| 36 | private static final BlurMaskFilter sThickOuterBlurMaskFilter = new BlurMaskFilter( |
| 37 | 6.0f, BlurMaskFilter.Blur.OUTER); |
| 38 | private static final BlurMaskFilter sMediumOuterBlurMaskFilter = new BlurMaskFilter( |
| 39 | 2.0f, BlurMaskFilter.Blur.OUTER); |
| 40 | private static final BlurMaskFilter sThinOuterBlurMaskFilter = new BlurMaskFilter( |
| 41 | 1.0f, BlurMaskFilter.Blur.OUTER); |
| 42 | |
| 43 | private static final BlurMaskFilter sThickInnerBlurMaskFilter = new BlurMaskFilter( |
| 44 | 4.0f, BlurMaskFilter.Blur.NORMAL); |
| 45 | private static final BlurMaskFilter sThinInnerBlurMaskFilter = new BlurMaskFilter( |
| 46 | 1.0f, BlurMaskFilter.Blur.INNER); |
| 47 | |
| 48 | private static final MaskFilter sFineClipTable = TableMaskFilter.CreateClipTable(0, 20); |
| 49 | private static final MaskFilter sCoarseClipTable = TableMaskFilter.CreateClipTable(0, 200); |
| 50 | |
| 51 | private int[] mTempOffset = new int[2]; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 52 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 53 | HolographicOutlineHelper() { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 54 | mHolographicPaint.setFilterBitmap(true); |
| 55 | mHolographicPaint.setAntiAlias(true); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 56 | mBlurPaint.setFilterBitmap(true); |
| 57 | mBlurPaint.setAntiAlias(true); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 58 | mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); |
| 59 | mErasePaint.setFilterBitmap(true); |
| 60 | mErasePaint.setAntiAlias(true); |
| 61 | } |
| 62 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Returns the interpolated holographic highlight alpha for the effect we want when scrolling |
| 65 | * pages. |
| 66 | */ |
| 67 | public float highlightAlphaInterpolator(float r) { |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 68 | float maxAlpha = 0.8f; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 69 | return (float) Math.pow(maxAlpha * (1.0f - r), 1.5f); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the interpolated view alpha for the effect we want when scrolling pages. |
| 74 | */ |
| 75 | public float viewAlphaInterpolator(float r) { |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 76 | final float pivot = 0.95f; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 77 | if (r < pivot) { |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 78 | return (float) Math.pow(r / pivot, 1.5f); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 79 | } else { |
| 80 | return 1.0f; |
| 81 | } |
| 82 | } |
| 83 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 84 | void applyGlow(Bitmap bitmap, Canvas canvas, int color) { |
| 85 | mBlurPaint.setMaskFilter(sThickOuterBlurMaskFilter); |
| 86 | Bitmap glow = bitmap.extractAlpha(mBlurPaint, mTempOffset); |
| 87 | |
| 88 | // Use the clip table to make the glow heavier closer to the outline |
| 89 | mHolographicPaint.setMaskFilter(sCoarseClipTable); |
| 90 | mHolographicPaint.setAlpha(150); |
| 91 | mHolographicPaint.setColor(color); |
| 92 | canvas.drawBitmap(glow, mTempOffset[0], mTempOffset[1], mHolographicPaint); |
| 93 | glow.recycle(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Draws a solid outline around a bitmap, erasing the original pixels. |
| 98 | * |
| 99 | * @param bitmap The bitmap to modify |
| 100 | * @param canvas A canvas on the bitmap |
| 101 | * @param color The color to draw the outline and glow in |
| 102 | * @param removeOrig If true, punch out the original pixels to just leave the outline |
| 103 | */ |
| 104 | void applyExpensiveOuterOutline(Bitmap bitmap, Canvas canvas, int color, boolean removeOrig) { |
| 105 | Bitmap originalImage = null; |
| 106 | if (removeOrig) { |
| 107 | originalImage = bitmap.extractAlpha(); |
| 108 | } |
| 109 | |
| 110 | // Compute an outer blur on the original bitmap |
| 111 | mBlurPaint.setMaskFilter(sMediumOuterBlurMaskFilter); |
| 112 | Bitmap outline = bitmap.extractAlpha(mBlurPaint, mTempOffset); |
| 113 | |
| 114 | // Paint the blurred bitmap back into the canvas. Using the clip table causes any alpha |
| 115 | // pixels above a certain threshold to be rounded up to be fully opaque. This gives the |
| 116 | // effect of a thick outline, with a slight blur on the edge |
| 117 | mHolographicPaint.setColor(color); |
| 118 | mHolographicPaint.setMaskFilter(sFineClipTable); |
| 119 | canvas.drawBitmap(outline, mTempOffset[0], mTempOffset[1], mHolographicPaint); |
| 120 | outline.recycle(); |
| 121 | |
| 122 | if (removeOrig) { |
| 123 | // Finally, punch out the original pixels, leaving just the outline |
| 124 | canvas.drawBitmap(originalImage, 0, 0, mErasePaint); |
| 125 | originalImage.recycle(); |
| 126 | } |
| 127 | } |
| 128 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 129 | /** |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 130 | * Applies a more expensive and accurate outline to whatever is currently drawn in a specified |
| 131 | * bitmap. |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 132 | */ |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 133 | void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
| 134 | int outlineColor) { |
| 135 | // calculate the outer blur first |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 136 | mBlurPaint.setMaskFilter(sThickOuterBlurMaskFilter); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 137 | int[] outerBlurOffset = new int[2]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 138 | Bitmap thickOuterBlur = srcDst.extractAlpha(mBlurPaint, outerBlurOffset); |
| 139 | mBlurPaint.setMaskFilter(sThinOuterBlurMaskFilter); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 140 | int[] thinOuterBlurOffset = new int[2]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 141 | Bitmap thinOuterBlur = srcDst.extractAlpha(mBlurPaint, thinOuterBlurOffset); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 142 | |
| 143 | // calculate the inner blur |
| 144 | srcDstCanvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 145 | mBlurPaint.setMaskFilter(sThickInnerBlurMaskFilter); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 146 | int[] thickInnerBlurOffset = new int[2]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 147 | Bitmap thickInnerBlur = srcDst.extractAlpha(mBlurPaint, thickInnerBlurOffset); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 148 | |
| 149 | // mask out the inner blur |
| 150 | srcDstCanvas.setBitmap(thickInnerBlur); |
| 151 | srcDstCanvas.drawBitmap(srcDst, -thickInnerBlurOffset[0], |
| 152 | -thickInnerBlurOffset[1], mErasePaint); |
| 153 | srcDstCanvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(), |
| 154 | mErasePaint); |
| 155 | srcDstCanvas.drawRect(0, 0, thickInnerBlur.getWidth(), -thickInnerBlurOffset[1], |
| 156 | mErasePaint); |
| 157 | |
| 158 | // draw the inner and outer blur |
| 159 | srcDstCanvas.setBitmap(srcDst); |
| 160 | srcDstCanvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 161 | mHolographicPaint.setColor(color); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 162 | srcDstCanvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1], |
| 163 | mHolographicPaint); |
| 164 | srcDstCanvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1], |
| 165 | mHolographicPaint); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 166 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 167 | // draw the bright outline |
| 168 | mHolographicPaint.setColor(outlineColor); |
| 169 | srcDstCanvas.drawBitmap(thinOuterBlur, thinOuterBlurOffset[0], thinOuterBlurOffset[1], |
| 170 | mHolographicPaint); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 171 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 172 | // cleanup |
| 173 | thinOuterBlur.recycle(); |
| 174 | thickOuterBlur.recycle(); |
| 175 | thickInnerBlur.recycle(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 176 | } |
| 177 | } |