android: add effectiveRect for BlurEffect

This commit is contained in:
pengfei.zhou 2021-11-24 15:51:39 +08:00 committed by osborn
parent 7792bcafc0
commit 9704f95592
3 changed files with 61 additions and 20 deletions

View File

@ -34,19 +34,23 @@ import jp.wasabeef.glide.transformations.internal.SupportRSBlur;
* @CreateDate: 2021/11/24 * @CreateDate: 2021/11/24
*/ */
public class BlurEffectView extends DoricLayer { public class BlurEffectView extends DoricLayer {
private Rect effectiveRect = null; private Rect mEffectiveRect = null;
private int mRadius = 15; private int mRadius = 15;
private Bitmap mBitmap = null; private Bitmap mFullBitmap = null;
private Canvas mBlurCanvas = null; private Canvas mFullCanvas = null;
private Bitmap mRectBitmap = null;
private Canvas mRectCanvas = null;
private Rect mDstRect = null;
public BlurEffectView(@NonNull Context context) { public BlurEffectView(@NonNull Context context) {
super(context); super(context);
} }
public void setEffectiveRect(Rect rect) { public void setEffectiveRect(Rect rect) {
this.effectiveRect = rect; this.mEffectiveRect = rect;
this.mDstRect = new Rect(0, 0, rect.width(), rect.height());
invalidate(); invalidate();
} }
@ -60,31 +64,53 @@ public class BlurEffectView extends DoricLayer {
@Override @Override
protected void dispatchDraw(Canvas canvas) { protected void dispatchDraw(Canvas canvas) {
if (mBitmap == null if (mFullBitmap == null
|| mBitmap.getWidth() != canvas.getWidth() || mFullBitmap.getWidth() != canvas.getWidth()
|| mBitmap.getHeight() != canvas.getHeight()) { || mFullBitmap.getHeight() != canvas.getHeight()) {
mBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); mFullBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
mBlurCanvas = new Canvas(mBitmap); mFullCanvas = new Canvas(mFullBitmap);
} }
mBlurCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); mFullCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
super.dispatchDraw(mBlurCanvas); super.dispatchDraw(mFullCanvas);
Bitmap bitmap; 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 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
&& mRadius <= 25) { && mRadius <= 25) {
try { try {
bitmap = SupportRSBlur.blur(getContext(), mBitmap, mRadius); blurredBitmap = SupportRSBlur.blur(getContext(), blurringBitmap, mRadius);
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {
try { try {
bitmap = RSBlur.blur(getContext(), mBitmap, mRadius); blurredBitmap = RSBlur.blur(getContext(), blurringBitmap, mRadius);
} catch (Exception ee) { } catch (Exception ee) {
bitmap = FastBlur.blur(mBitmap, mRadius, true); blurredBitmap = FastBlur.blur(blurringBitmap, mRadius, true);
} }
} catch (Exception e) { } catch (Exception e) {
bitmap = FastBlur.blur(mBitmap, mRadius, true); blurredBitmap = FastBlur.blur(blurringBitmap, mRadius, true);
} }
} else { } 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);
} }
} }

View File

@ -15,12 +15,15 @@
*/ */
package pub.doric.shader; package pub.doric.shader;
import android.graphics.Rect;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.utils.DoricUtils;
/** /**
* @Description: Describe the effect view * @Description: Describe the effect view
@ -44,8 +47,22 @@ public class BlurEffectViewNode extends StackNode {
if (prop.isNumber()) { if (prop.isNumber()) {
((BlurEffectView) view).setRadius(prop.asNumber().toInt()); ((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 { } else {
super.blend(view, name, prop); super.blend(view, name, prop);
} }
} }
@Override
protected void blendSubNode(JSObject subProp) {
super.blendSubNode(subProp);
mView.invalidate();
}
} }

View File

@ -22,8 +22,6 @@ import android.widget.FrameLayout;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import com.github.pengfeizhou.jscore.JSObject;
/** /**
* @Description: com.github.penfeizhou.doric.widget * @Description: com.github.penfeizhou.doric.widget
* @Author: pengfei.zhou * @Author: pengfei.zhou