android: add BlurEffectView

This commit is contained in:
pengfei.zhou
2021-11-24 14:37:48 +08:00
committed by osborn
parent 040823a8af
commit d24e0ecff6
18 changed files with 2280 additions and 1768 deletions

View File

@@ -47,6 +47,7 @@ import pub.doric.resource.DoricBase64Loader;
import pub.doric.resource.DoricLocalLoader;
import pub.doric.resource.DoricRemoteLoader;
import pub.doric.resource.DoricResourceManager;
import pub.doric.shader.BlurEffectViewNode;
import pub.doric.shader.DraggableNode;
import pub.doric.shader.GestureContainerNode;
import pub.doric.shader.HLayoutNode;
@@ -130,6 +131,7 @@ public class DoricRegistry {
this.registerViewNode(SwitchNode.class);
this.registerViewNode(FlexNode.class);
this.registerViewNode(GestureContainerNode.class);
this.registerViewNode(BlurEffectViewNode.class);
this.getResourceManager().registerLoader(new DoricAndroidLoader("drawable"));
this.getResourceManager().registerLoader(new DoricAndroidLoader("raw"));
this.getResourceManager().registerLoader(new DoricAssetsLoader());

View File

@@ -0,0 +1,90 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.shader;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.os.Build;
import androidx.annotation.NonNull;
import jp.wasabeef.glide.transformations.internal.FastBlur;
import jp.wasabeef.glide.transformations.internal.RSBlur;
import jp.wasabeef.glide.transformations.internal.SupportRSBlur;
/**
* @Description: This could blur what's contained.
* @Author: pengfei.zhou
* @CreateDate: 2021/11/24
*/
public class BlurEffectView extends DoricLayer {
private Rect effectiveRect = null;
private int mRadius = 15;
private Bitmap mBitmap = null;
private Canvas mBlurCanvas = null;
public BlurEffectView(@NonNull Context context) {
super(context);
}
public void setEffectiveRect(Rect rect) {
this.effectiveRect = rect;
invalidate();
}
public void setRadius(int radius) {
if (radius <= 0) {
return;
}
this.mRadius = radius;
invalidate();
}
@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);
}
mBlurCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
super.dispatchDraw(mBlurCanvas);
Bitmap bitmap;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
&& mRadius <= 25) {
try {
bitmap = SupportRSBlur.blur(getContext(), mBitmap, mRadius);
} catch (NoClassDefFoundError e) {
try {
bitmap = RSBlur.blur(getContext(), mBitmap, mRadius);
} catch (Exception ee) {
bitmap = FastBlur.blur(mBitmap, mRadius, true);
}
} catch (Exception e) {
bitmap = FastBlur.blur(mBitmap, mRadius, true);
}
} else {
bitmap = FastBlur.blur(mBitmap, mRadius, true);
}
canvas.drawBitmap(bitmap, 0, 0, null);
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.shader;
import android.widget.FrameLayout;
import com.github.pengfeizhou.jscore.JSValue;
import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin;
/**
* @Description: Describe the effect view
* @Author: pengfei.zhou
* @CreateDate: 2021/11/24
*/
@DoricPlugin(name = "BlurEffect")
public class BlurEffectViewNode extends StackNode {
public BlurEffectViewNode(DoricContext doricContext) {
super(doricContext);
}
@Override
protected FrameLayout build() {
return new BlurEffectView(getContext());
}
@Override
protected void blend(FrameLayout view, String name, JSValue prop) {
if ("radius".equals(name)) {
if (prop.isNumber()) {
((BlurEffectView) view).setRadius(prop.asNumber().toInt());
}
} else {
super.blend(view, name, prop);
}
}
}