Merge "Refactoring some shadow generator logic into common methods" into ub-launcher3-dorval-polish
This commit is contained in:
committed by
Android (Google) Code Review
commit
d2d43ef9be
@@ -27,7 +27,6 @@ import android.os.CancellationSignal;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.graphics.ColorUtils;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.LongSparseArray;
|
import android.util.LongSparseArray;
|
||||||
|
|
||||||
@@ -388,10 +387,10 @@ public class WidgetPreviewLoader {
|
|||||||
drawable.setBounds(x, 0, x + previewWidth, previewHeight);
|
drawable.setBounds(x, 0, x + previewWidth, previewHeight);
|
||||||
drawable.draw(c);
|
drawable.draw(c);
|
||||||
} else {
|
} else {
|
||||||
final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
|
RectF boxRect = drawBoxWithShadow(c, previewWidth, previewHeight);
|
||||||
RectF boxRect = drawBoxWithShadow(c, p, previewWidth, previewHeight);
|
|
||||||
|
|
||||||
// Draw horizontal and vertical lines to represent individual columns.
|
// Draw horizontal and vertical lines to represent individual columns.
|
||||||
|
final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||||
p.setStyle(Paint.Style.STROKE);
|
p.setStyle(Paint.Style.STROKE);
|
||||||
p.setStrokeWidth(mContext.getResources()
|
p.setStrokeWidth(mContext.getResources()
|
||||||
.getDimension(R.dimen.widget_preview_cell_divider_width));
|
.getDimension(R.dimen.widget_preview_cell_divider_width));
|
||||||
@@ -431,7 +430,7 @@ public class WidgetPreviewLoader {
|
|||||||
return preview;
|
return preview;
|
||||||
}
|
}
|
||||||
|
|
||||||
private RectF drawBoxWithShadow(Canvas c, Paint p, int width, int height) {
|
private RectF drawBoxWithShadow(Canvas c, int width, int height) {
|
||||||
Resources res = mContext.getResources();
|
Resources res = mContext.getResources();
|
||||||
float shadowBlur = res.getDimension(R.dimen.widget_preview_shadow_blur);
|
float shadowBlur = res.getDimension(R.dimen.widget_preview_shadow_blur);
|
||||||
float keyShadowDistance = res.getDimension(R.dimen.widget_preview_key_shadow_distance);
|
float keyShadowDistance = res.getDimension(R.dimen.widget_preview_key_shadow_distance);
|
||||||
@@ -439,19 +438,7 @@ public class WidgetPreviewLoader {
|
|||||||
|
|
||||||
RectF bounds = new RectF(shadowBlur, shadowBlur,
|
RectF bounds = new RectF(shadowBlur, shadowBlur,
|
||||||
width - shadowBlur, height - shadowBlur - keyShadowDistance);
|
width - shadowBlur, height - shadowBlur - keyShadowDistance);
|
||||||
p.setColor(Color.WHITE);
|
ShadowGenerator.drawShadow(c, bounds, Color.WHITE, shadowBlur, keyShadowDistance, corner);
|
||||||
|
|
||||||
// Key shadow
|
|
||||||
p.setShadowLayer(shadowBlur, 0, keyShadowDistance,
|
|
||||||
ShadowGenerator.KEY_SHADOW_ALPHA << 24);
|
|
||||||
c.drawRoundRect(bounds, corner, corner, p);
|
|
||||||
|
|
||||||
// Ambient shadow
|
|
||||||
p.setShadowLayer(shadowBlur, 0, 0,
|
|
||||||
ColorUtils.setAlphaComponent(Color.BLACK, ShadowGenerator.AMBIENT_SHADOW_ALPHA));
|
|
||||||
c.drawRoundRect(bounds, corner, corner, p);
|
|
||||||
|
|
||||||
p.clearShadowLayer();
|
|
||||||
return bounds;
|
return bounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -478,8 +465,7 @@ public class WidgetPreviewLoader {
|
|||||||
c.setBitmap(preview);
|
c.setBitmap(preview);
|
||||||
c.drawColor(0, PorterDuff.Mode.CLEAR);
|
c.drawColor(0, PorterDuff.Mode.CLEAR);
|
||||||
}
|
}
|
||||||
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
|
RectF boxRect = drawBoxWithShadow(c, size, size);
|
||||||
RectF boxRect = drawBoxWithShadow(c, p, size, size);
|
|
||||||
|
|
||||||
Bitmap icon = LauncherIcons.createScaledBitmapWithoutShadow(
|
Bitmap icon = LauncherIcons.createScaledBitmapWithoutShadow(
|
||||||
mutateOnMainThread(info.getFullResIcon(mIconCache)), mContext, Build.VERSION_CODES.O);
|
mutateOnMainThread(info.getFullResIcon(mIconCache)), mContext, Build.VERSION_CODES.O);
|
||||||
@@ -487,7 +473,8 @@ public class WidgetPreviewLoader {
|
|||||||
|
|
||||||
boxRect.set(0, 0, iconSize, iconSize);
|
boxRect.set(0, 0, iconSize, iconSize);
|
||||||
boxRect.offset(padding, padding);
|
boxRect.offset(padding, padding);
|
||||||
c.drawBitmap(icon, src, boxRect, p);
|
c.drawBitmap(icon, src, boxRect,
|
||||||
|
new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
|
||||||
c.setBitmap(null);
|
c.setBitmap(null);
|
||||||
return preview;
|
return preview;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,10 @@ import android.graphics.Bitmap.Config;
|
|||||||
import android.graphics.BlurMaskFilter;
|
import android.graphics.BlurMaskFilter;
|
||||||
import android.graphics.BlurMaskFilter.Blur;
|
import android.graphics.BlurMaskFilter.Blur;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
import android.graphics.RectF;
|
import android.graphics.RectF;
|
||||||
|
import android.support.v4.graphics.ColorUtils;
|
||||||
|
|
||||||
import com.android.launcher3.LauncherAppState;
|
import com.android.launcher3.LauncherAppState;
|
||||||
import com.android.launcher3.util.Preconditions;
|
import com.android.launcher3.util.Preconditions;
|
||||||
@@ -39,9 +41,9 @@ public class ShadowGenerator {
|
|||||||
|
|
||||||
// Percent of actual icon size
|
// Percent of actual icon size
|
||||||
private static final float KEY_SHADOW_DISTANCE = 1f/48;
|
private static final float KEY_SHADOW_DISTANCE = 1f/48;
|
||||||
public static final int KEY_SHADOW_ALPHA = 61;
|
private static final int KEY_SHADOW_ALPHA = 61;
|
||||||
|
|
||||||
public static final int AMBIENT_SHADOW_ALPHA = 30;
|
private static final int AMBIENT_SHADOW_ALPHA = 30;
|
||||||
|
|
||||||
private static final Object LOCK = new Object();
|
private static final Object LOCK = new Object();
|
||||||
// Singleton object guarded by {@link #LOCK}
|
// Singleton object guarded by {@link #LOCK}
|
||||||
@@ -84,45 +86,45 @@ public class ShadowGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Bitmap createPillWithShadow(int rectColor, int width, int height) {
|
public static Bitmap createPillWithShadow(int rectColor, int width, int height) {
|
||||||
|
|
||||||
float shadowRadius = height * 1f / 32;
|
float shadowRadius = height * 1f / 32;
|
||||||
float shadowYOffset = height * 1f / 16;
|
float shadowYOffset = height * 1f / 16;
|
||||||
|
return createPillWithShadow(rectColor, width, height, shadowRadius, shadowYOffset,
|
||||||
|
new RectF());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Bitmap createPillWithShadow(int rectColor, int width, int height,
|
||||||
|
float shadowRadius, float shadowYOffset, RectF outRect) {
|
||||||
int radius = height / 2;
|
int radius = height / 2;
|
||||||
|
|
||||||
Canvas canvas = new Canvas();
|
|
||||||
Paint blurPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
|
|
||||||
blurPaint.setMaskFilter(new BlurMaskFilter(shadowRadius, Blur.NORMAL));
|
|
||||||
|
|
||||||
int centerX = Math.round(width / 2 + shadowRadius);
|
int centerX = Math.round(width / 2 + shadowRadius);
|
||||||
int centerY = Math.round(radius + shadowRadius + shadowYOffset);
|
int centerY = Math.round(radius + shadowRadius + shadowYOffset);
|
||||||
int center = Math.max(centerX, centerY);
|
int center = Math.max(centerX, centerY);
|
||||||
int size = center * 2;
|
int size = center * 2;
|
||||||
Bitmap result = Bitmap.createBitmap(size, size, Config.ARGB_8888);
|
Bitmap result = Bitmap.createBitmap(size, size, Config.ARGB_8888);
|
||||||
canvas.setBitmap(result);
|
|
||||||
|
|
||||||
int left = center - width / 2;
|
outRect.set(0, 0, width, height);
|
||||||
int top = center - height / 2;
|
outRect.offsetTo(center - width / 2, center - height / 2);
|
||||||
int right = center + width / 2;
|
|
||||||
int bottom = center + height / 2;
|
|
||||||
|
|
||||||
// Draw ambient shadow, center aligned within size
|
|
||||||
blurPaint.setAlpha(AMBIENT_SHADOW_ALPHA);
|
|
||||||
canvas.drawRoundRect(left, top, right, bottom, radius, radius, blurPaint);
|
|
||||||
|
|
||||||
// Draw key shadow, bottom aligned within size
|
|
||||||
blurPaint.setAlpha(KEY_SHADOW_ALPHA);
|
|
||||||
canvas.drawRoundRect(left, top + shadowYOffset, right, bottom + shadowYOffset,
|
|
||||||
radius, radius, blurPaint);
|
|
||||||
|
|
||||||
// Draw the circle
|
|
||||||
Paint drawPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
|
|
||||||
drawPaint.setColor(rectColor);
|
|
||||||
canvas.drawRoundRect(left, top, right, bottom, radius, radius, drawPaint);
|
|
||||||
|
|
||||||
|
drawShadow(new Canvas(result), outRect, rectColor, shadowRadius, shadowYOffset, radius);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
// Key shadow
|
||||||
|
p.setShadowLayer(shadowBlur, 0, keyShadowDistance,
|
||||||
|
ColorUtils.setAlphaComponent(Color.BLACK, KEY_SHADOW_ALPHA));
|
||||||
|
c.drawRoundRect(bounds, radius, radius, p);
|
||||||
|
|
||||||
|
// Ambient shadow
|
||||||
|
p.setShadowLayer(shadowBlur, 0, 0,
|
||||||
|
ColorUtils.setAlphaComponent(Color.BLACK, AMBIENT_SHADOW_ALPHA));
|
||||||
|
c.drawRoundRect(bounds, radius, radius, p);
|
||||||
|
}
|
||||||
|
|
||||||
public static ShadowGenerator getInstance(Context context) {
|
public static ShadowGenerator getInstance(Context context) {
|
||||||
Preconditions.assertNonUiThread();
|
Preconditions.assertNonUiThread();
|
||||||
synchronized (LOCK) {
|
synchronized (LOCK) {
|
||||||
|
|||||||
Reference in New Issue
Block a user