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

@ -25,7 +25,7 @@ android {
}
afterEvaluate {
buildJSBundle.exec()
//buildJSBundle.exec()
}
task buildJSBundle(type: Exec) {

View File

@ -12,6 +12,8 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
renderscriptTargetApi 18
renderscriptSupportModeEnabled true
}
buildTypes {
@ -26,6 +28,11 @@ android {
assets.srcDirs = ["../../doric-js/bundle"]
}
}
packagingOptions {
exclude 'lib/*/libRSSupport.so'
exclude 'lib/*/librsjni.so'
exclude 'lib/*/librsjni_androidx.so'
}
}
dependencies {

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);
}
}
}

View File

@ -0,0 +1,39 @@
import {
VLayout,
Group,
Panel,
jsx,
layoutConfig,
BlurEffect,
Image,
Text,
createRef,
animate,
loge,
Stack,
RotationXAnimation,
RotationAnimation,
} from "doric";
@Entry
export class BlurEffectsDemo extends Panel {
build(root: Group) {
let ref = createRef<Image>();
<VLayout layoutConfig={layoutConfig().most()} parent={root}>
<BlurEffect layoutConfig={layoutConfig().most()} radius={15}>
<Image
ref={ref}
imageUrl="https://pic3.zhimg.com/v2-5847d0813bd0deba333fcbe52435e83e_b.jpg"
onClick={async () => {
const rotation = new RotationAnimation();
rotation.fromRotation = 0;
rotation.toRotation = 2;
rotation.duration = 1000;
rotation.repeatCount = -1;
ref.current.doAnimation(context, rotation);
}}
/>
</BlurEffect>
</VLayout>;
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

24
doric-js/index.d.ts vendored
View File

@ -96,6 +96,7 @@ declare module 'doric/lib/src/widget/index.widget' {
export * from 'doric/lib/src/widget/draggable';
export * from 'doric/lib/src/widget/switch';
export * from 'doric/lib/src/widget/gesture';
export * from 'doric/lib/src/widget/effect';
}
declare module 'doric/lib/src/native/index.native' {
@ -1088,6 +1089,29 @@ declare module 'doric/lib/src/widget/gesture' {
export function gestureContainer(views: View | View[], config?: Partial<GestureContainer>): GestureContainer;
}
declare module 'doric/lib/src/widget/effect' {
import { Stack } from "doric/lib/src/widget/layouts";
import { View } from "doric/lib/src/ui/view";
export class BlurEffect extends Stack {
/**
* Specify the effective rectangle.
* If not set, the default is the entire area.
*/
effectiveRect?: {
x: number;
y: number;
width: number;
height: number;
};
/**
* Specify the radius of blur effect.
* If not set, the default is 15.
*/
radius?: number;
}
export function blurEffect(views: View | View[], config?: Partial<BlurEffect>): BlurEffect;
}
declare module 'doric/lib/src/native/modal' {
import { BridgeContext } from "doric/lib/src/runtime/global";
import { Gravity } from "doric/lib/src/util/gravity";

20
doric-js/lib/src/widget/effect.d.ts vendored Normal file
View File

@ -0,0 +1,20 @@
import { Stack } from "../widget/layouts";
import { View } from "../ui/view";
export declare class BlurEffect extends Stack {
/**
* Specify the effective rectangle.
* If not set, the default is the entire area.
*/
effectiveRect?: {
x: number;
y: number;
width: number;
height: number;
};
/**
* Specify the radius of blur effect.
* If not set, the default is 15.
*/
radius?: number;
}
export declare function blurEffect(views: View | View[], config?: Partial<BlurEffect>): BlurEffect;

View File

@ -0,0 +1,53 @@
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
/*
* 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.
*/
import { Stack } from "../widget/layouts";
import { Property, View } from "../ui/view";
import { layoutConfig } from "../util/layoutconfig";
export class BlurEffect extends Stack {
}
__decorate([
Property,
__metadata("design:type", Object)
], BlurEffect.prototype, "effectiveRect", void 0);
__decorate([
Property,
__metadata("design:type", Number)
], BlurEffect.prototype, "radius", void 0);
export function blurEffect(views, config) {
const ret = new BlurEffect;
ret.layoutConfig = layoutConfig().fit();
if (views instanceof View) {
ret.addChild(views);
}
else {
views.forEach(e => {
ret.addChild(e);
});
}
if (config) {
ret.apply(config);
}
return ret;
}

View File

@ -11,3 +11,4 @@ export * from './nestedSlider';
export * from './draggable';
export * from './switch';
export * from './gesture';
export * from './effect';

View File

@ -26,3 +26,4 @@ export * from './nestedSlider';
export * from './draggable';
export * from './switch';
export * from './gesture';
export * from './effect';

View File

@ -0,0 +1,55 @@
/*
* 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.
*/
import { Stack } from "../widget/layouts"
import { Property, View } from "../ui/view"
import { layoutConfig } from "../util/layoutconfig"
export class BlurEffect extends Stack {
/**
* Specify the effective rectangle.
* If not set, the default is the entire area.
*/
@Property
effectiveRect?: {
x: number,
y: number,
width: number,
height: number,
}
/**
* Specify the radius of blur effect.
* If not set, the default value is 15.
* Suggested value is from 1 to 25.
*/
@Property
radius?: number
}
export function blurEffect(views: View | View[], config?: Partial<BlurEffect>) {
const ret = new BlurEffect
ret.layoutConfig = layoutConfig().fit()
if (views instanceof View) {
ret.addChild(views)
} else {
views.forEach(e => {
ret.addChild(e)
})
}
if (config) {
ret.apply(config)
}
return ret
}

View File

@ -25,4 +25,5 @@ export * from './input'
export * from './nestedSlider'
export * from './draggable'
export * from './switch'
export * from './gesture'
export * from './gesture'
export * from './effect'

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long