You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by gu...@apache.org on 2017/09/27 14:10:30 UTC
[12/18] incubator-weex git commit: * [android] better solution for
draw box-shadow
* [android] better solution for draw box-shadow
Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/0839ee18
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/0839ee18
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/0839ee18
Branch: refs/heads/release
Commit: 0839ee18c27c52a9030e06c3e2cd5142ee6db616
Parents: fa4726a
Author: misakuo <mi...@apache.org>
Authored: Wed Sep 27 15:22:02 2017 +0800
Committer: misakuo <mi...@apache.org>
Committed: Wed Sep 27 15:22:02 2017 +0800
----------------------------------------------------------------------
.../com/taobao/weex/utils/BoxShadowUtil.java | 47 ++++++++------------
1 file changed, 19 insertions(+), 28 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/0839ee18/android/sdk/src/main/java/com/taobao/weex/utils/BoxShadowUtil.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/main/java/com/taobao/weex/utils/BoxShadowUtil.java b/android/sdk/src/main/java/com/taobao/weex/utils/BoxShadowUtil.java
index b1ebb63..c1cc693 100644
--- a/android/sdk/src/main/java/com/taobao/weex/utils/BoxShadowUtil.java
+++ b/android/sdk/src/main/java/com/taobao/weex/utils/BoxShadowUtil.java
@@ -20,6 +20,7 @@ package com.taobao.weex.utils;
import android.content.res.Resources;
import android.graphics.Bitmap;
+import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
@@ -40,6 +41,8 @@ import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
+import com.taobao.weex.WXEnvironment;
+
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -103,18 +106,21 @@ public class BoxShadowUtil {
float[] radii, float shadowRadius,
float shadowSpread,
float dx, float dy, int shadowColor) {
-
- if (shadowRadius == 0) {
- // 0 can not draw shadow layer
- shadowRadius = 0.01f;
- }
-
int canvasWidth = viewWidth + 2 * (int) (shadowRadius + shadowSpread + Math.abs(dx));
int canvasHeight = viewHeight + 2 * (int) (shadowRadius + shadowSpread + Math.abs(dy));
Bitmap output = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
+ if (false && WXEnvironment.isApkDebugable()) {
+ // Using for debug
+ Paint strokePaint = new Paint();
+ strokePaint.setColor(Color.BLACK);
+ strokePaint.setStrokeWidth(2);
+ strokePaint.setStyle(Paint.Style.STROKE);
+ canvas.drawRect(canvas.getClipBounds(), strokePaint);
+ }
+
float offsetX = shadowRadius + shadowSpread + Math.abs(dx);
float offsetY = shadowRadius + shadowSpread + Math.abs(dy);
RectF selfRect = new RectF(
@@ -127,36 +133,21 @@ public class BoxShadowUtil {
// can not antialias
canvas.clipPath(contentPath, Region.Op.DIFFERENCE);
- float shadowLeft, shadowTop;
- if (shadowSpread == 0f) {
- shadowLeft = shadowRadius;
- shadowTop = shadowRadius;
- } else {
- shadowLeft = shadowRadius + dx - shadowSpread;
- shadowTop = shadowRadius + dy - shadowSpread;
- }
RectF shadowRect = new RectF(
- shadowLeft,
- shadowTop,
- canvasWidth - shadowRadius + shadowSpread,
- canvasHeight - shadowRadius + shadowSpread);
+ 0f, 0f,
+ viewWidth + 2f * shadowSpread, viewHeight + 2f * shadowSpread
+ );
- shadowRect.top += Math.abs(dy);
- shadowRect.bottom -= Math.abs(dy);
- shadowRect.left += Math.abs(dx);
- shadowRect.right -= Math.abs(dx);
+ float shadowDx = 2f * dx + shadowRadius;
+ float shadowDy = 2f * dy + shadowRadius;
+ shadowRect.offset(shadowDx, shadowDy);
Paint shadowPaint = new Paint();
shadowPaint.setAntiAlias(true);
shadowPaint.setColor(shadowColor);
shadowPaint.setStyle(Paint.Style.FILL);
- float shadowDx = 0f, shadowDy = 0f;
- if (shadowSpread == 0f) {
- shadowDx = dx;
- shadowDy = dy;
- }
- shadowPaint.setShadowLayer(shadowRadius, shadowDx, shadowDy, shadowColor);
+ shadowPaint.setMaskFilter(new BlurMaskFilter(shadowRadius, BlurMaskFilter.Blur.NORMAL));
Path shadowPath = new Path();
float[] shadowRadii = new float[8];