2016-07-19 14:19:53 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2016 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.launcher3.graphics;
|
|
|
|
|
|
2017-01-11 10:48:34 -08:00
|
|
|
import android.content.Context;
|
2016-07-19 14:19:53 -07:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.Bitmap.Config;
|
|
|
|
|
import android.graphics.BlurMaskFilter;
|
|
|
|
|
import android.graphics.BlurMaskFilter.Blur;
|
|
|
|
|
import android.graphics.Canvas;
|
2017-05-02 16:17:10 -07:00
|
|
|
import android.graphics.Color;
|
2016-07-19 14:19:53 -07:00
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.graphics.RectF;
|
2017-05-02 16:17:10 -07:00
|
|
|
import android.support.v4.graphics.ColorUtils;
|
2016-07-19 14:19:53 -07:00
|
|
|
|
|
|
|
|
import com.android.launcher3.LauncherAppState;
|
|
|
|
|
import com.android.launcher3.util.Preconditions;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Utility class to add shadows to bitmaps.
|
|
|
|
|
*/
|
|
|
|
|
public class ShadowGenerator {
|
|
|
|
|
|
|
|
|
|
// Percent of actual icon size
|
|
|
|
|
private static final float HALF_DISTANCE = 0.5f;
|
2017-06-06 12:12:22 -07:00
|
|
|
public static final float BLUR_FACTOR = 0.5f/48;
|
2016-07-19 14:19:53 -07:00
|
|
|
|
|
|
|
|
// Percent of actual icon size
|
2017-06-09 11:24:28 -07:00
|
|
|
public static final float KEY_SHADOW_DISTANCE = 1f/48;
|
2017-05-02 16:17:10 -07:00
|
|
|
private static final int KEY_SHADOW_ALPHA = 61;
|
2016-07-19 14:19:53 -07:00
|
|
|
|
2017-05-02 16:17:10 -07:00
|
|
|
private static final int AMBIENT_SHADOW_ALPHA = 30;
|
2016-07-19 14:19:53 -07:00
|
|
|
|
|
|
|
|
private static final Object LOCK = new Object();
|
|
|
|
|
// Singleton object guarded by {@link #LOCK}
|
|
|
|
|
private static ShadowGenerator sShadowGenerator;
|
|
|
|
|
|
|
|
|
|
private final int mIconSize;
|
|
|
|
|
|
|
|
|
|
private final Canvas mCanvas;
|
|
|
|
|
private final Paint mBlurPaint;
|
|
|
|
|
private final Paint mDrawPaint;
|
|
|
|
|
|
2017-01-11 10:48:34 -08:00
|
|
|
private ShadowGenerator(Context context) {
|
|
|
|
|
mIconSize = LauncherAppState.getIDP(context).iconBitmapSize;
|
2016-07-19 14:19:53 -07:00
|
|
|
mCanvas = new Canvas();
|
|
|
|
|
mBlurPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
|
|
|
|
|
mBlurPaint.setMaskFilter(new BlurMaskFilter(mIconSize * BLUR_FACTOR, Blur.NORMAL));
|
|
|
|
|
mDrawPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized Bitmap recreateIcon(Bitmap icon) {
|
|
|
|
|
int[] offset = new int[2];
|
|
|
|
|
Bitmap shadow = icon.extractAlpha(mBlurPaint, offset);
|
|
|
|
|
Bitmap result = Bitmap.createBitmap(mIconSize, mIconSize, Config.ARGB_8888);
|
|
|
|
|
mCanvas.setBitmap(result);
|
|
|
|
|
|
|
|
|
|
// Draw ambient shadow
|
|
|
|
|
mDrawPaint.setAlpha(AMBIENT_SHADOW_ALPHA);
|
|
|
|
|
mCanvas.drawBitmap(shadow, offset[0], offset[1], mDrawPaint);
|
|
|
|
|
|
|
|
|
|
// Draw key shadow
|
|
|
|
|
mDrawPaint.setAlpha(KEY_SHADOW_ALPHA);
|
|
|
|
|
mCanvas.drawBitmap(shadow, offset[0], offset[1] + KEY_SHADOW_DISTANCE * mIconSize, mDrawPaint);
|
|
|
|
|
|
|
|
|
|
// Draw the icon
|
|
|
|
|
mDrawPaint.setAlpha(255);
|
|
|
|
|
mCanvas.drawBitmap(icon, 0, 0, mDrawPaint);
|
|
|
|
|
|
|
|
|
|
mCanvas.setBitmap(null);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-24 08:59:36 -08:00
|
|
|
public static Bitmap createPillWithShadow(int rectColor, int width, int height) {
|
|
|
|
|
float shadowRadius = height * 1f / 32;
|
|
|
|
|
float shadowYOffset = height * 1f / 16;
|
2017-05-02 16:17:10 -07:00
|
|
|
return createPillWithShadow(rectColor, width, height, shadowRadius, shadowYOffset,
|
|
|
|
|
new RectF());
|
|
|
|
|
}
|
2017-02-27 13:30:20 -08:00
|
|
|
|
2017-05-02 16:17:10 -07:00
|
|
|
public static Bitmap createPillWithShadow(int rectColor, int width, int height,
|
|
|
|
|
float shadowRadius, float shadowYOffset, RectF outRect) {
|
2017-02-24 08:59:36 -08:00
|
|
|
int radius = height / 2;
|
2017-02-27 13:30:20 -08:00
|
|
|
|
2017-02-24 08:59:36 -08:00
|
|
|
int centerX = Math.round(width / 2 + shadowRadius);
|
|
|
|
|
int centerY = Math.round(radius + shadowRadius + shadowYOffset);
|
|
|
|
|
int center = Math.max(centerX, centerY);
|
2017-02-27 13:30:20 -08:00
|
|
|
int size = center * 2;
|
|
|
|
|
Bitmap result = Bitmap.createBitmap(size, size, Config.ARGB_8888);
|
|
|
|
|
|
2017-05-02 16:17:10 -07:00
|
|
|
outRect.set(0, 0, width, height);
|
|
|
|
|
outRect.offsetTo(center - width / 2, center - height / 2);
|
2017-02-24 08:59:36 -08:00
|
|
|
|
2017-05-02 16:17:10 -07:00
|
|
|
drawShadow(new Canvas(result), outRect, rectColor, shadowRadius, shadowYOffset, radius);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2017-02-27 13:30:20 -08:00
|
|
|
|
2017-05-02 16:17:10 -07:00
|
|
|
public static void drawShadow(Canvas c, RectF bounds, int color,
|
|
|
|
|
float shadowBlur, float keyShadowDistance, float radius) {
|
|
|
|
|
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
|
|
|
|
|
p.setColor(color);
|
2017-02-27 13:30:20 -08:00
|
|
|
|
2017-05-02 16:17:10 -07:00
|
|
|
// Key shadow
|
|
|
|
|
p.setShadowLayer(shadowBlur, 0, keyShadowDistance,
|
|
|
|
|
ColorUtils.setAlphaComponent(Color.BLACK, KEY_SHADOW_ALPHA));
|
|
|
|
|
c.drawRoundRect(bounds, radius, radius, p);
|
2017-02-27 13:30:20 -08:00
|
|
|
|
2017-05-02 16:17:10 -07:00
|
|
|
// Ambient shadow
|
|
|
|
|
p.setShadowLayer(shadowBlur, 0, 0,
|
|
|
|
|
ColorUtils.setAlphaComponent(Color.BLACK, AMBIENT_SHADOW_ALPHA));
|
|
|
|
|
c.drawRoundRect(bounds, radius, radius, p);
|
2017-02-27 13:30:20 -08:00
|
|
|
}
|
|
|
|
|
|
2017-01-11 10:48:34 -08:00
|
|
|
public static ShadowGenerator getInstance(Context context) {
|
2016-07-19 14:19:53 -07:00
|
|
|
Preconditions.assertNonUiThread();
|
|
|
|
|
synchronized (LOCK) {
|
|
|
|
|
if (sShadowGenerator == null) {
|
2017-01-11 10:48:34 -08:00
|
|
|
sShadowGenerator = new ShadowGenerator(context);
|
2016-07-19 14:19:53 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return sShadowGenerator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the minimum amount by which an icon with {@param bounds} should be scaled
|
|
|
|
|
* so that the shadows do not get clipped.
|
|
|
|
|
*/
|
|
|
|
|
public static float getScaleForBounds(RectF bounds) {
|
|
|
|
|
float scale = 1;
|
|
|
|
|
|
|
|
|
|
// For top, left & right, we need same space.
|
|
|
|
|
float minSide = Math.min(Math.min(bounds.left, bounds.right), bounds.top);
|
|
|
|
|
if (minSide < BLUR_FACTOR) {
|
|
|
|
|
scale = (HALF_DISTANCE - BLUR_FACTOR) / (HALF_DISTANCE - minSide);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float bottomSpace = BLUR_FACTOR + KEY_SHADOW_DISTANCE;
|
|
|
|
|
if (bounds.bottom < bottomSpace) {
|
|
|
|
|
scale = Math.min(scale, (HALF_DISTANCE - bottomSpace) / (HALF_DISTANCE - bounds.bottom));
|
|
|
|
|
}
|
|
|
|
|
return scale;
|
|
|
|
|
}
|
|
|
|
|
}
|