Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b3648e08 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Refactoring some shadow generator logic into common methods

Change-Id: I25fc3248be5148fb1c14944eb4ddeec5bd4125b2
parent 9d02a3dd
Loading
Loading
Loading
Loading
+7 −20
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ import android.os.CancellationSignal;
import android.os.Handler;
import android.os.UserHandle;
import android.support.annotation.Nullable;
import android.support.v4.graphics.ColorUtils;
import android.util.Log;
import android.util.LongSparseArray;

@@ -388,10 +387,10 @@ public class WidgetPreviewLoader {
            drawable.setBounds(x, 0, x + previewWidth, previewHeight);
            drawable.draw(c);
        } else {
            final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
            RectF boxRect = drawBoxWithShadow(c, p, previewWidth, previewHeight);
            RectF boxRect = drawBoxWithShadow(c, previewWidth, previewHeight);

            // Draw horizontal and vertical lines to represent individual columns.
            final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
            p.setStyle(Paint.Style.STROKE);
            p.setStrokeWidth(mContext.getResources()
                    .getDimension(R.dimen.widget_preview_cell_divider_width));
@@ -431,7 +430,7 @@ public class WidgetPreviewLoader {
        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();
        float shadowBlur = res.getDimension(R.dimen.widget_preview_shadow_blur);
        float keyShadowDistance = res.getDimension(R.dimen.widget_preview_key_shadow_distance);
@@ -439,19 +438,7 @@ public class WidgetPreviewLoader {

        RectF bounds = new RectF(shadowBlur, shadowBlur,
                width - shadowBlur, height - shadowBlur - keyShadowDistance);
        p.setColor(Color.WHITE);

        // 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();
        ShadowGenerator.drawShadow(c, bounds, Color.WHITE, shadowBlur, keyShadowDistance, corner);
        return bounds;
    }

@@ -478,8 +465,7 @@ public class WidgetPreviewLoader {
            c.setBitmap(preview);
            c.drawColor(0, PorterDuff.Mode.CLEAR);
        }
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
        RectF boxRect = drawBoxWithShadow(c, p, size, size);
        RectF boxRect = drawBoxWithShadow(c, size, size);

        Bitmap icon = LauncherIcons.createScaledBitmapWithoutShadow(
                mutateOnMainThread(info.getFullResIcon(mIconCache)), mContext, Build.VERSION_CODES.O);
@@ -487,7 +473,8 @@ public class WidgetPreviewLoader {

        boxRect.set(0, 0, iconSize, iconSize);
        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);
        return preview;
    }
+26 −24
Original line number Diff line number Diff line
@@ -22,8 +22,10 @@ import android.graphics.Bitmap.Config;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.graphics.ColorUtils;

import com.android.launcher3.LauncherAppState;
import com.android.launcher3.util.Preconditions;
@@ -39,9 +41,9 @@ public class ShadowGenerator {

    // Percent of actual icon size
    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();
    // Singleton object guarded by {@link #LOCK}
@@ -84,43 +86,43 @@ public class ShadowGenerator {
    }

    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;

        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 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);
        canvas.setBitmap(result);

        int left = center - width / 2;
        int top = center - height / 2;
        int right = center + width / 2;
        int bottom = center + height / 2;
        outRect.set(0, 0, width, height);
        outRect.offsetTo(center - width / 2, center - height / 2);

        // Draw ambient shadow, center aligned within size
        blurPaint.setAlpha(AMBIENT_SHADOW_ALPHA);
        canvas.drawRoundRect(left, top, right, bottom, radius, radius, blurPaint);
        drawShadow(new Canvas(result), outRect, rectColor, shadowRadius, shadowYOffset, radius);
        return result;
    }

        // Draw key shadow, bottom aligned within size
        blurPaint.setAlpha(KEY_SHADOW_ALPHA);
        canvas.drawRoundRect(left, top + shadowYOffset, right, bottom + shadowYOffset,
                radius, radius, blurPaint);
    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);

        // 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);
        // Key shadow
        p.setShadowLayer(shadowBlur, 0, keyShadowDistance,
                ColorUtils.setAlphaComponent(Color.BLACK, KEY_SHADOW_ALPHA));
        c.drawRoundRect(bounds, radius, radius, p);

        return result;
        // 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) {