2010-09-03 14:15:02 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher2;
|
|
|
|
|
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.BlurMaskFilter;
|
|
|
|
|
import android.graphics.Canvas;
|
2010-10-10 11:26:02 -07:00
|
|
|
import android.graphics.MaskFilter;
|
2010-09-03 14:15:02 -07:00
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
|
import android.graphics.PorterDuffXfermode;
|
2010-10-10 11:26:02 -07:00
|
|
|
import android.graphics.TableMaskFilter;
|
2010-09-03 14:15:02 -07:00
|
|
|
|
|
|
|
|
public class HolographicOutlineHelper {
|
|
|
|
|
private final Paint mHolographicPaint = new Paint();
|
2010-10-10 11:26:02 -07:00
|
|
|
private final Paint mBlurPaint = new Paint();
|
2010-09-03 14:15:02 -07:00
|
|
|
private final Paint mErasePaint = new Paint();
|
|
|
|
|
|
2010-10-14 13:21:48 -07:00
|
|
|
public static final int OUTER_BLUR_RADIUS;
|
2010-10-10 11:26:02 -07:00
|
|
|
|
2010-10-14 13:21:48 -07:00
|
|
|
private static final BlurMaskFilter sThickOuterBlurMaskFilter;
|
|
|
|
|
private static final BlurMaskFilter sMediumOuterBlurMaskFilter;
|
|
|
|
|
private static final BlurMaskFilter sThinOuterBlurMaskFilter;
|
|
|
|
|
private static final BlurMaskFilter sThickInnerBlurMaskFilter;
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
final float scale = LauncherApplication.getScreenDensity();
|
|
|
|
|
|
|
|
|
|
OUTER_BLUR_RADIUS = (int) (scale * 6.0f);
|
|
|
|
|
|
|
|
|
|
sThickOuterBlurMaskFilter = new BlurMaskFilter(OUTER_BLUR_RADIUS,
|
|
|
|
|
BlurMaskFilter.Blur.OUTER);
|
|
|
|
|
sMediumOuterBlurMaskFilter = new BlurMaskFilter(scale * 2.0f, BlurMaskFilter.Blur.OUTER);
|
|
|
|
|
sThinOuterBlurMaskFilter = new BlurMaskFilter(scale * 1.0f, BlurMaskFilter.Blur.OUTER);
|
|
|
|
|
sThickInnerBlurMaskFilter = new BlurMaskFilter(scale * 4.0f, BlurMaskFilter.Blur.NORMAL);
|
|
|
|
|
}
|
2010-10-10 11:26:02 -07:00
|
|
|
|
|
|
|
|
private static final MaskFilter sFineClipTable = TableMaskFilter.CreateClipTable(0, 20);
|
|
|
|
|
private static final MaskFilter sCoarseClipTable = TableMaskFilter.CreateClipTable(0, 200);
|
|
|
|
|
|
|
|
|
|
private int[] mTempOffset = new int[2];
|
2010-09-03 14:15:02 -07:00
|
|
|
|
2010-09-17 16:47:33 -07:00
|
|
|
HolographicOutlineHelper() {
|
2010-09-03 14:15:02 -07:00
|
|
|
mHolographicPaint.setFilterBitmap(true);
|
|
|
|
|
mHolographicPaint.setAntiAlias(true);
|
2010-10-10 11:26:02 -07:00
|
|
|
mBlurPaint.setFilterBitmap(true);
|
|
|
|
|
mBlurPaint.setAntiAlias(true);
|
2010-09-03 14:15:02 -07:00
|
|
|
mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
|
|
|
|
|
mErasePaint.setFilterBitmap(true);
|
|
|
|
|
mErasePaint.setAntiAlias(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the interpolated holographic highlight alpha for the effect we want when scrolling
|
|
|
|
|
* pages.
|
|
|
|
|
*/
|
|
|
|
|
public float highlightAlphaInterpolator(float r) {
|
2010-09-17 16:47:33 -07:00
|
|
|
float maxAlpha = 0.8f;
|
2010-09-15 20:37:09 -07:00
|
|
|
return (float) Math.pow(maxAlpha * (1.0f - r), 1.5f);
|
2010-09-03 14:15:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the interpolated view alpha for the effect we want when scrolling pages.
|
|
|
|
|
*/
|
|
|
|
|
public float viewAlphaInterpolator(float r) {
|
2010-09-15 20:37:09 -07:00
|
|
|
final float pivot = 0.95f;
|
2010-09-03 14:15:02 -07:00
|
|
|
if (r < pivot) {
|
2010-09-15 20:37:09 -07:00
|
|
|
return (float) Math.pow(r / pivot, 1.5f);
|
2010-09-03 14:15:02 -07:00
|
|
|
} else {
|
|
|
|
|
return 1.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-14 13:21:48 -07:00
|
|
|
/**
|
|
|
|
|
* Apply an outer blur to the given bitmap.
|
|
|
|
|
* You should use OUTER_BLUR_RADIUS to ensure that the bitmap is big enough to draw
|
|
|
|
|
* the blur without clipping.
|
|
|
|
|
*/
|
|
|
|
|
void applyOuterBlur(Bitmap bitmap, Canvas canvas, int color) {
|
2010-10-10 11:26:02 -07:00
|
|
|
mBlurPaint.setMaskFilter(sThickOuterBlurMaskFilter);
|
|
|
|
|
Bitmap glow = bitmap.extractAlpha(mBlurPaint, mTempOffset);
|
|
|
|
|
|
|
|
|
|
// Use the clip table to make the glow heavier closer to the outline
|
|
|
|
|
mHolographicPaint.setMaskFilter(sCoarseClipTable);
|
|
|
|
|
mHolographicPaint.setAlpha(150);
|
|
|
|
|
mHolographicPaint.setColor(color);
|
|
|
|
|
canvas.drawBitmap(glow, mTempOffset[0], mTempOffset[1], mHolographicPaint);
|
|
|
|
|
glow.recycle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Draws a solid outline around a bitmap, erasing the original pixels.
|
|
|
|
|
*
|
|
|
|
|
* @param bitmap The bitmap to modify
|
|
|
|
|
* @param canvas A canvas on the bitmap
|
|
|
|
|
* @param color The color to draw the outline and glow in
|
|
|
|
|
* @param removeOrig If true, punch out the original pixels to just leave the outline
|
|
|
|
|
*/
|
|
|
|
|
void applyExpensiveOuterOutline(Bitmap bitmap, Canvas canvas, int color, boolean removeOrig) {
|
|
|
|
|
Bitmap originalImage = null;
|
|
|
|
|
if (removeOrig) {
|
|
|
|
|
originalImage = bitmap.extractAlpha();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compute an outer blur on the original bitmap
|
|
|
|
|
mBlurPaint.setMaskFilter(sMediumOuterBlurMaskFilter);
|
|
|
|
|
Bitmap outline = bitmap.extractAlpha(mBlurPaint, mTempOffset);
|
|
|
|
|
|
|
|
|
|
// Paint the blurred bitmap back into the canvas. Using the clip table causes any alpha
|
|
|
|
|
// pixels above a certain threshold to be rounded up to be fully opaque. This gives the
|
|
|
|
|
// effect of a thick outline, with a slight blur on the edge
|
|
|
|
|
mHolographicPaint.setColor(color);
|
|
|
|
|
mHolographicPaint.setMaskFilter(sFineClipTable);
|
|
|
|
|
canvas.drawBitmap(outline, mTempOffset[0], mTempOffset[1], mHolographicPaint);
|
|
|
|
|
outline.recycle();
|
|
|
|
|
|
|
|
|
|
if (removeOrig) {
|
|
|
|
|
// Finally, punch out the original pixels, leaving just the outline
|
|
|
|
|
canvas.drawBitmap(originalImage, 0, 0, mErasePaint);
|
|
|
|
|
originalImage.recycle();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-03 14:15:02 -07:00
|
|
|
/**
|
2010-09-17 16:47:33 -07:00
|
|
|
* Applies a more expensive and accurate outline to whatever is currently drawn in a specified
|
|
|
|
|
* bitmap.
|
2010-09-03 14:15:02 -07:00
|
|
|
*/
|
2010-09-17 16:47:33 -07:00
|
|
|
void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color,
|
|
|
|
|
int outlineColor) {
|
|
|
|
|
// calculate the outer blur first
|
2010-10-10 11:26:02 -07:00
|
|
|
mBlurPaint.setMaskFilter(sThickOuterBlurMaskFilter);
|
2010-09-17 16:47:33 -07:00
|
|
|
int[] outerBlurOffset = new int[2];
|
2010-10-10 11:26:02 -07:00
|
|
|
Bitmap thickOuterBlur = srcDst.extractAlpha(mBlurPaint, outerBlurOffset);
|
|
|
|
|
mBlurPaint.setMaskFilter(sThinOuterBlurMaskFilter);
|
2010-09-17 16:47:33 -07:00
|
|
|
int[] thinOuterBlurOffset = new int[2];
|
2010-10-10 11:26:02 -07:00
|
|
|
Bitmap thinOuterBlur = srcDst.extractAlpha(mBlurPaint, thinOuterBlurOffset);
|
2010-09-03 14:15:02 -07:00
|
|
|
|
2010-09-17 16:47:33 -07:00
|
|
|
// calculate the inner blur
|
|
|
|
|
srcDstCanvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT);
|
2010-10-10 11:26:02 -07:00
|
|
|
mBlurPaint.setMaskFilter(sThickInnerBlurMaskFilter);
|
2010-09-17 16:47:33 -07:00
|
|
|
int[] thickInnerBlurOffset = new int[2];
|
2010-10-10 11:26:02 -07:00
|
|
|
Bitmap thickInnerBlur = srcDst.extractAlpha(mBlurPaint, thickInnerBlurOffset);
|
2010-09-03 14:15:02 -07:00
|
|
|
|
2010-09-17 16:47:33 -07:00
|
|
|
// mask out the inner blur
|
|
|
|
|
srcDstCanvas.setBitmap(thickInnerBlur);
|
|
|
|
|
srcDstCanvas.drawBitmap(srcDst, -thickInnerBlurOffset[0],
|
|
|
|
|
-thickInnerBlurOffset[1], mErasePaint);
|
|
|
|
|
srcDstCanvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(),
|
|
|
|
|
mErasePaint);
|
|
|
|
|
srcDstCanvas.drawRect(0, 0, thickInnerBlur.getWidth(), -thickInnerBlurOffset[1],
|
|
|
|
|
mErasePaint);
|
2010-09-03 14:15:02 -07:00
|
|
|
|
2010-09-17 16:47:33 -07:00
|
|
|
// draw the inner and outer blur
|
|
|
|
|
srcDstCanvas.setBitmap(srcDst);
|
|
|
|
|
srcDstCanvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
|
|
|
|
|
mHolographicPaint.setColor(color);
|
|
|
|
|
srcDstCanvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1],
|
|
|
|
|
mHolographicPaint);
|
|
|
|
|
srcDstCanvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1],
|
|
|
|
|
mHolographicPaint);
|
|
|
|
|
|
|
|
|
|
// draw the bright outline
|
|
|
|
|
mHolographicPaint.setColor(outlineColor);
|
|
|
|
|
srcDstCanvas.drawBitmap(thinOuterBlur, thinOuterBlurOffset[0], thinOuterBlurOffset[1],
|
|
|
|
|
mHolographicPaint);
|
|
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
|
thinOuterBlur.recycle();
|
|
|
|
|
thickOuterBlur.recycle();
|
|
|
|
|
thickInnerBlur.recycle();
|
2010-09-03 14:15:02 -07:00
|
|
|
}
|
|
|
|
|
}
|