Merge "Rafactoring shadow generator logic to allow customizing various parameters" into ub-launcher3-dorval-polish

This commit is contained in:
TreeHugger Robot
2017-06-20 22:33:24 +00:00
committed by Android (Google) Code Review
3 changed files with 66 additions and 48 deletions
@@ -432,14 +432,17 @@ public class WidgetPreviewLoader {
private RectF drawBoxWithShadow(Canvas c, int width, int height) {
Resources res = mContext.getResources();
float shadowBlur = res.getDimension(R.dimen.widget_preview_shadow_blur);
float keyShadowDistance = res.getDimension(R.dimen.widget_preview_key_shadow_distance);
float corner = res.getDimension(R.dimen.widget_preview_corner_radius);
RectF bounds = new RectF(shadowBlur, shadowBlur,
width - shadowBlur, height - shadowBlur - keyShadowDistance);
ShadowGenerator.drawShadow(c, bounds, Color.WHITE, shadowBlur, keyShadowDistance, corner);
return bounds;
ShadowGenerator.Builder builder = new ShadowGenerator.Builder(Color.WHITE);
builder.shadowBlur = res.getDimension(R.dimen.widget_preview_shadow_blur);
builder.radius = res.getDimension(R.dimen.widget_preview_corner_radius);
builder.keyShadowDistance = res.getDimension(R.dimen.widget_preview_key_shadow_distance);
builder.bounds.set(builder.shadowBlur, builder.shadowBlur,
width - builder.shadowBlur,
height - builder.shadowBlur - builder.keyShadowDistance);
builder.drawShadow(c);
return builder.bounds;
}
private Bitmap generateShortcutPreview(BaseActivity launcher, ShortcutConfigActivityInfo info,
@@ -107,7 +107,8 @@ public class BadgeRenderer {
// Lazily load the background with shadow.
Bitmap backgroundWithShadow = mBackgroundsWithShadow.get(numChars);
if (backgroundWithShadow == null) {
backgroundWithShadow = ShadowGenerator.createPillWithShadow(Color.WHITE, width, mSize);
backgroundWithShadow = new ShadowGenerator.Builder(Color.WHITE)
.setupBlurForSize(mSize).createPill(width, mSize);
mBackgroundsWithShadow.put(numChars, backgroundWithShadow);
}
canvas.save(Canvas.MATRIX_SAVE_FLAG);
@@ -84,46 +84,6 @@ public class ShadowGenerator {
return result;
}
public static Bitmap createPillWithShadow(int rectColor, int width, int height) {
float shadowRadius = height * 1f / 32;
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 centerX = Math.round(width / 2 + shadowRadius);
int centerY = Math.round(radius + shadowRadius + shadowYOffset);
int center = Math.max(centerX, centerY);
int size = center * 2;
Bitmap result = Bitmap.createBitmap(size, size, Config.ARGB_8888);
outRect.set(0, 0, width, height);
outRect.offsetTo(center - width / 2, center - height / 2);
drawShadow(new Canvas(result), outRect, rectColor, shadowRadius, shadowYOffset, radius);
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) {
// TODO: This currently fails as the system default icon also needs a shadow as it
// uses adaptive icon.
@@ -155,4 +115,58 @@ public class ShadowGenerator {
}
return scale;
}
public static class Builder {
public final RectF bounds = new RectF();
public final int color;
public int ambientShadowAlpha = AMBIENT_SHADOW_ALPHA;
public float shadowBlur;
public float keyShadowDistance;
public int keyShadowAlpha = KEY_SHADOW_ALPHA;
public float radius;
public Builder(int color) {
this.color = color;
}
public Builder setupBlurForSize(int height) {
shadowBlur = height * 1f / 32;
keyShadowDistance = height * 1f / 16;
return this;
}
public Bitmap createPill(int width, int height) {
radius = height / 2;
int centerX = Math.round(width / 2 + shadowBlur);
int centerY = Math.round(radius + shadowBlur + keyShadowDistance);
int center = Math.max(centerX, centerY);
bounds.set(0, 0, width, height);
bounds.offsetTo(center - width / 2, center - height / 2);
int size = center * 2;
Bitmap result = Bitmap.createBitmap(size, size, Config.ARGB_8888);
drawShadow(new Canvas(result));
return result;
}
public void drawShadow(Canvas c) {
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, keyShadowAlpha));
c.drawRoundRect(bounds, radius, radius, p);
// Ambient shadow
p.setShadowLayer(shadowBlur, 0, 0,
ColorUtils.setAlphaComponent(Color.BLACK, ambientShadowAlpha));
c.drawRoundRect(bounds, radius, radius, p);
}
}
}