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