diff options
author | Sunny Goyal <sunnygoyal@google.com> | 2017-06-20 13:34:09 -0700 |
---|---|---|
committer | Sunny Goyal <sunnygoyal@google.com> | 2017-06-20 13:37:05 -0700 |
commit | e463c8f191ad8494509d96acaf247d4b943864cd (patch) | |
tree | c0169428864cd5e1ac1499534aa83af4028a3e9e /src/com/android/launcher3/graphics/ShadowGenerator.java | |
parent | 50b4ae3c72ccce76684809ca3e1043d19f324068 (diff) | |
download | android_packages_apps_Trebuchet-e463c8f191ad8494509d96acaf247d4b943864cd.tar.gz android_packages_apps_Trebuchet-e463c8f191ad8494509d96acaf247d4b943864cd.tar.bz2 android_packages_apps_Trebuchet-e463c8f191ad8494509d96acaf247d4b943864cd.zip |
Rafactoring shadow generator logic to allow customizing various parameters
Change-Id: I38f8ac4352cb9e23d377832ffe2f4923a4df7a70
Diffstat (limited to 'src/com/android/launcher3/graphics/ShadowGenerator.java')
-rw-r--r-- | src/com/android/launcher3/graphics/ShadowGenerator.java | 94 |
1 files changed, 54 insertions, 40 deletions
diff --git a/src/com/android/launcher3/graphics/ShadowGenerator.java b/src/com/android/launcher3/graphics/ShadowGenerator.java index 9ea11a7b2..7270eafa9 100644 --- a/src/com/android/launcher3/graphics/ShadowGenerator.java +++ b/src/com/android/launcher3/graphics/ShadowGenerator.java @@ -85,46 +85,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) { Preconditions.assertNonUiThread(); synchronized (LOCK) { @@ -154,4 +114,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); + } + } } |