From 9704f955923e263f664530b823f0f5f56a1aa2f1 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 24 Nov 2021 15:51:39 +0800 Subject: [PATCH] android: add effectiveRect for BlurEffect --- .../java/pub/doric/shader/BlurEffectView.java | 62 +++++++++++++------ .../pub/doric/shader/BlurEffectViewNode.java | 17 +++++ .../main/java/pub/doric/shader/RootNode.java | 2 - 3 files changed, 61 insertions(+), 20 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/shader/BlurEffectView.java b/doric-android/doric/src/main/java/pub/doric/shader/BlurEffectView.java index 2519feaf..f84741a2 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/BlurEffectView.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/BlurEffectView.java @@ -34,19 +34,23 @@ import jp.wasabeef.glide.transformations.internal.SupportRSBlur; * @CreateDate: 2021/11/24 */ public class BlurEffectView extends DoricLayer { - private Rect effectiveRect = null; + private Rect mEffectiveRect = null; private int mRadius = 15; - private Bitmap mBitmap = null; - private Canvas mBlurCanvas = null; + private Bitmap mFullBitmap = null; + private Canvas mFullCanvas = null; + private Bitmap mRectBitmap = null; + private Canvas mRectCanvas = null; + private Rect mDstRect = null; public BlurEffectView(@NonNull Context context) { super(context); } public void setEffectiveRect(Rect rect) { - this.effectiveRect = rect; + this.mEffectiveRect = rect; + this.mDstRect = new Rect(0, 0, rect.width(), rect.height()); invalidate(); } @@ -60,31 +64,53 @@ public class BlurEffectView extends DoricLayer { @Override protected void dispatchDraw(Canvas canvas) { - if (mBitmap == null - || mBitmap.getWidth() != canvas.getWidth() - || mBitmap.getHeight() != canvas.getHeight()) { - mBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); - mBlurCanvas = new Canvas(mBitmap); + if (mFullBitmap == null + || mFullBitmap.getWidth() != canvas.getWidth() + || mFullBitmap.getHeight() != canvas.getHeight()) { + mFullBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); + mFullCanvas = new Canvas(mFullBitmap); } - mBlurCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); - super.dispatchDraw(mBlurCanvas); - Bitmap bitmap; + mFullCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); + super.dispatchDraw(mFullCanvas); + Bitmap blurringBitmap; + if (mEffectiveRect != null) { + if (mRectBitmap == null + || mRectBitmap.getWidth() != mEffectiveRect.width() + || mRectBitmap.getHeight() != mEffectiveRect.height()) { + mRectBitmap = Bitmap.createBitmap(mEffectiveRect.width(), mEffectiveRect.height(), Bitmap.Config.ARGB_8888); + mRectCanvas = new Canvas(mRectBitmap); + } + blurringBitmap = mRectBitmap; + mRectCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); + mRectCanvas.drawBitmap(mFullBitmap, mEffectiveRect, mDstRect, null); + } else { + blurringBitmap = mFullBitmap; + } + Bitmap blurredBitmap; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && mRadius <= 25) { try { - bitmap = SupportRSBlur.blur(getContext(), mBitmap, mRadius); + blurredBitmap = SupportRSBlur.blur(getContext(), blurringBitmap, mRadius); } catch (NoClassDefFoundError e) { try { - bitmap = RSBlur.blur(getContext(), mBitmap, mRadius); + blurredBitmap = RSBlur.blur(getContext(), blurringBitmap, mRadius); } catch (Exception ee) { - bitmap = FastBlur.blur(mBitmap, mRadius, true); + blurredBitmap = FastBlur.blur(blurringBitmap, mRadius, true); } } catch (Exception e) { - bitmap = FastBlur.blur(mBitmap, mRadius, true); + blurredBitmap = FastBlur.blur(blurringBitmap, mRadius, true); } } else { - bitmap = FastBlur.blur(mBitmap, mRadius, true); + blurredBitmap = FastBlur.blur(blurringBitmap, mRadius, true); } - canvas.drawBitmap(bitmap, 0, 0, null); + Bitmap retBitmap; + if (mEffectiveRect != null) { + mFullCanvas.drawBitmap(blurredBitmap, mDstRect, mEffectiveRect, null); + retBitmap = mFullBitmap; + } else { + retBitmap = blurredBitmap; + } + + canvas.drawBitmap(retBitmap, 0, 0, null); } } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/BlurEffectViewNode.java b/doric-android/doric/src/main/java/pub/doric/shader/BlurEffectViewNode.java index 096bf51c..fa6ff96f 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/BlurEffectViewNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/BlurEffectViewNode.java @@ -15,12 +15,15 @@ */ package pub.doric.shader; +import android.graphics.Rect; import android.widget.FrameLayout; +import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSValue; import pub.doric.DoricContext; import pub.doric.extension.bridge.DoricPlugin; +import pub.doric.utils.DoricUtils; /** * @Description: Describe the effect view @@ -44,8 +47,22 @@ public class BlurEffectViewNode extends StackNode { if (prop.isNumber()) { ((BlurEffectView) view).setRadius(prop.asNumber().toInt()); } + } else if ("effectiveRect".equals(name)) { + if (prop.isObject()) { + int x = DoricUtils.dp2px(prop.asObject().getProperty("x").asNumber().toFloat()); + int y = DoricUtils.dp2px(prop.asObject().getProperty("x").asNumber().toFloat()); + int width = DoricUtils.dp2px(prop.asObject().getProperty("width").asNumber().toFloat()); + int height = DoricUtils.dp2px(prop.asObject().getProperty("height").asNumber().toFloat()); + ((BlurEffectView) view).setEffectiveRect(new Rect(x, y, x + width, y + height)); + } } else { super.blend(view, name, prop); } } + + @Override + protected void blendSubNode(JSObject subProp) { + super.blendSubNode(subProp); + mView.invalidate(); + } } diff --git a/doric-android/doric/src/main/java/pub/doric/shader/RootNode.java b/doric-android/doric/src/main/java/pub/doric/shader/RootNode.java index ec8d1539..7bd0caa7 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/RootNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/RootNode.java @@ -22,8 +22,6 @@ import android.widget.FrameLayout; import pub.doric.DoricContext; import pub.doric.extension.bridge.DoricPlugin; -import com.github.pengfeizhou.jscore.JSObject; - /** * @Description: com.github.penfeizhou.doric.widget * @Author: pengfei.zhou