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; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 22 | import android.graphics.Paint; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 23 | import android.graphics.PorterDuff; |
| 24 | import android.graphics.PorterDuffXfermode; |
| 25 | |
| 26 | public class HolographicOutlineHelper { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 27 | private final Paint mHolographicPaint = new Paint(); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 28 | private final Paint mExpensiveBlurPaint = new Paint(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 29 | private final Paint mErasePaint = new Paint(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 30 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 31 | private static final BlurMaskFilter mThickOuterBlurMaskFilter = new BlurMaskFilter(6.0f, |
| 32 | BlurMaskFilter.Blur.OUTER); |
| 33 | private static final BlurMaskFilter mThinOuterBlurMaskFilter = new BlurMaskFilter(1.0f, |
| 34 | BlurMaskFilter.Blur.OUTER); |
| 35 | private static final BlurMaskFilter mThickInnerBlurMaskFilter = new BlurMaskFilter(4.0f, |
| 36 | BlurMaskFilter.Blur.NORMAL); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 37 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 38 | HolographicOutlineHelper() { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 39 | mHolographicPaint.setFilterBitmap(true); |
| 40 | mHolographicPaint.setAntiAlias(true); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 41 | mExpensiveBlurPaint.setFilterBitmap(true); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 42 | mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); |
| 43 | mErasePaint.setFilterBitmap(true); |
| 44 | mErasePaint.setAntiAlias(true); |
| 45 | } |
| 46 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Returns the interpolated holographic highlight alpha for the effect we want when scrolling |
| 49 | * pages. |
| 50 | */ |
| 51 | public float highlightAlphaInterpolator(float r) { |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 52 | float maxAlpha = 0.8f; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 53 | return (float) Math.pow(maxAlpha * (1.0f - r), 1.5f); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the interpolated view alpha for the effect we want when scrolling pages. |
| 58 | */ |
| 59 | public float viewAlphaInterpolator(float r) { |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 60 | final float pivot = 0.95f; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 61 | if (r < pivot) { |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 62 | return (float) Math.pow(r / pivot, 1.5f); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 63 | } else { |
| 64 | return 1.0f; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 69 | * Applies a more expensive and accurate outline to whatever is currently drawn in a specified |
| 70 | * bitmap. |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 71 | */ |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 72 | void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color, |
| 73 | int outlineColor) { |
| 74 | // calculate the outer blur first |
| 75 | mExpensiveBlurPaint.setMaskFilter(mThickOuterBlurMaskFilter); |
| 76 | int[] outerBlurOffset = new int[2]; |
| 77 | Bitmap thickOuterBlur = srcDst.extractAlpha(mExpensiveBlurPaint, outerBlurOffset); |
| 78 | mExpensiveBlurPaint.setMaskFilter(mThinOuterBlurMaskFilter); |
| 79 | int[] thinOuterBlurOffset = new int[2]; |
| 80 | Bitmap thinOuterBlur = srcDst.extractAlpha(mExpensiveBlurPaint, thinOuterBlurOffset); |
| 81 | |
| 82 | // calculate the inner blur |
| 83 | srcDstCanvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT); |
| 84 | mExpensiveBlurPaint.setMaskFilter(mThickInnerBlurMaskFilter); |
| 85 | int[] thickInnerBlurOffset = new int[2]; |
| 86 | Bitmap thickInnerBlur = srcDst.extractAlpha(mExpensiveBlurPaint, thickInnerBlurOffset); |
| 87 | |
| 88 | // mask out the inner blur |
| 89 | srcDstCanvas.setBitmap(thickInnerBlur); |
| 90 | srcDstCanvas.drawBitmap(srcDst, -thickInnerBlurOffset[0], |
| 91 | -thickInnerBlurOffset[1], mErasePaint); |
| 92 | srcDstCanvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(), |
| 93 | mErasePaint); |
| 94 | srcDstCanvas.drawRect(0, 0, thickInnerBlur.getWidth(), -thickInnerBlurOffset[1], |
| 95 | mErasePaint); |
| 96 | |
| 97 | // draw the inner and outer blur |
| 98 | srcDstCanvas.setBitmap(srcDst); |
| 99 | srcDstCanvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 100 | mHolographicPaint.setColor(color); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 101 | srcDstCanvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1], |
| 102 | mHolographicPaint); |
| 103 | srcDstCanvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1], |
| 104 | mHolographicPaint); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 105 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 106 | // draw the bright outline |
| 107 | mHolographicPaint.setColor(outlineColor); |
| 108 | srcDstCanvas.drawBitmap(thinOuterBlur, thinOuterBlurOffset[0], thinOuterBlurOffset[1], |
| 109 | mHolographicPaint); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 110 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 111 | // cleanup |
| 112 | thinOuterBlur.recycle(); |
| 113 | thickOuterBlur.recycle(); |
| 114 | thickInnerBlur.recycle(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 115 | } |
| 116 | } |