@react-native-ohos/blur
v4.6.0
Published
React Native Blur component
Readme
模板版本:v0.4.2
本项目基于 @react-native-community/[email protected] 开发。
该第三方库的仓库已迁移至 Gitcode,且支持直接从 npm 下载,新的包名为:@react-native-ohos/blur 版本所属关系如下:
| 三方库名称 | 三方库版本(npm地址) | 发布信息 | 支持RN版本 | Autolink | 编译API版本 | 社区基线版本 | 源码地址 | | ------------ | ------------ | ------------------------------ | ------------- | ------------- |------------------------ | ------------- | ------------- | | @react-native-ohos/blur | ~4.6.0 | Gitcode Releases | 0.82.*/ 0.84.| 是 | API12+ | 4.4.1 | master | | @react-native-ohos/blur | ~4.5.0 | Gitcode Releases | 0.77. | 否 | API12+ | 4.4.1 | br_rnoh0.77 | | @react-native-ohos/blur | ~ 4.4.2 | Gitcode Releases | 0.72.* | 是 | API12+ | 4.4.0 | br_rnoh0.72 | | @react-native-oh-tpl/blur | <= 4.4.1@deprecated | Github Releases | 0.72.* | 否 | API12+ | 4.4.0 | sig |
简介
react-native-community/blur 是一个为 React Native 应用提供模糊和活力效果的组件库。
下载安装
进入到工程目录并输入以下命令:
npm
npm install @react-native-ohos/bluryarn
yarn add @react-native-ohos/blurLink
| | 是否支持autolink | RN框架版本 | |--------------------------------------|-----------------|------------| | ~4.6.0 | 是 | 0.82/0.84 |
使用AutoLink的工程需要根据该文档配置,Autolink框架指导文档:https://gitcode.com/CPF-RN/ohos_react_native/blob/master/docs/zh-cn/Autolinking.md
如您使用的版本支持 Autolink,并且工程已接入 Autolink,可跳过ManualLink配置。
首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 harmony
1.在工程根目录的 oh-package.json5 添加 overrides 字段
{
...
"overrides": {
"@rnoh/react-native-openharmony" : "./react_native_openharmony"
}
}2.引入原生端代码
目前有两种方法:
- 通过 har 包引入;
- 直接链接源码。
方法一:通过 har 包引入
[!TIP] har 包位于三方库安装路径的
harmony文件夹下。
打开 entry/oh-package.json5,添加以下依赖
"dependencies": {
"@rnoh/react-native-openharmony": "file:../react_native_openharmony",
"@react-native-ohos/blur": "file:../../node_modules/@react-native-ohos/blur/harmony/blur.har"
}点击右上角的 sync 按钮
或者在终端执行:
cd entry
ohpm install方法二:直接链接源码
[!TIP] 如需使用直接链接源码,请参考直接链接源码说明
3.配置 CMakeLists 和引入 BlurPackage
打开 entry/src/main/cpp/CMakeLists.txt,添加:
project(rnapp)
cmake_minimum_required(VERSION 3.4.1)
set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
+ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
add_subdirectory("${RNOH_CPP_DIR}" ./rn)
# RNOH_BEGIN: add_package_subdirectories
add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package)
+ add_subdirectory("${OH_MODULES}/@react-native-ohos/blur/src/main/cpp" ./blur)
# RNOH_END: add_package_subdirectories
add_library(rnoh_app SHARED
"./PackageProvider.cpp"
"${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
)
target_link_libraries(rnoh_app PUBLIC rnoh)
# RNOH_BEGIN: link_packages
target_link_libraries(rnoh_app PUBLIC rnoh_sample_package)
+ target_link_libraries(rnoh_app PUBLIC rnoh_blur)
# RNOH_END: link_packages打开 entry/src/main/cpp/PackageProvider.cpp,添加:
#include "RNOH/PackageProvider.h"
#include "SamplePackage.h"
+ #include "BlurPackage.h"
using namespace rnoh;
std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
return {
std::make_shared<SamplePackage>(ctx),
+ std::make_shared<BlurPackage>(ctx)
};
}4. 在 ArkTs 侧引入 BlurPackage
打开 entry/src/main/ets/RNPackagesFactory.ts,添加:
...
+ import { BlurPackage } from '@react-native-ohos/blur';
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
return [
+ new BlurPackage(ctx)
];
}运行
点击右上角的 sync 按钮
或者在终端执行:
cd entry
ohpm install然后编译、运行即可。
约束与限制
兼容性
要使用此库,需要使用正确的 React-Native 和 RNOH 版本。另外,还需要使用配套的 DevEco Studio 和 手机 ROM。
在以下版本验证通过:
- RNOH: 0.84.2; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.878; ROM:6.0.0.130
使用示例
下面的代码展示了这个库的基本使用场景:
[!WARNING] 使用时 import 的库名不变。
import React, { useState} from 'react';
import {
Image,
StyleSheet,
Platform,
Switch,
Text,
View,
SafeAreaView,
Dimensions,
} from 'react-native';
import {
BlurView,
BlurViewProps,
} from '@react-native-community/blur';
export const Blurs = () => {
const [blurBlurType, setBlurBlurType] = useState<BlurViewProps['blurType']>('dark');
const [blurAmount, setBlurAmount] = useState<number>(100);
const tintColor = blurBlurType === 'dark' ? 'white' : 'black';
return (
<View style={styles.container}>
<View style={styles.blurContainer}>
<BlurView
blurType={blurBlurType}
blurAmount={blurAmount}
reducedTransparencyFallbackColor='red'
style={[styles.blurView]}
/>
<Text style={[styles.text, { color: tintColor }]}>
Blur component ({Platform.OS})
</Text>
<View style={styles.row}>
<Text onPress={() => {
setBlurBlurType('light')
}}>light</Text>
<Text onPress={() => {
setBlurBlurType('dark')
}}>dark</Text>
<Text onPress={() => {
setBlurBlurType('chromeMaterialLight')
}}>chromeMaterialLight</Text>
</View>
<View style={styles.row}>
<Text onPress={() => {
setBlurAmount(20)
}}>20</Text>
<Text onPress={() => {
setBlurAmount(40)
}}>40</Text>
<Text onPress={() => {
setBlurAmount(60)
}}>60</Text>
<Text onPress={() => {
setBlurAmount(80)
}}>80</Text>
<Text onPress={() => {
setBlurAmount(100)
}}>100</Text>
</View>
</View>
</View>
);
};
export const BlurDemo = () => {
const [showBlurs, setShowBlurs] = React.useState(false);
//'../assets/bgimage.jpeg' 此路径的图片为本地图片,在使用demo时将此图片的路径换为自己本地图片路径
return (
<View style={styles.container}>
<Image
source={require('../assets/bgimage.jpeg')}
resizeMode="cover"
style={styles.img}
/>
{showBlurs ? <Blurs /> : null}
<SafeAreaView style={styles.blurToggle}>
<Switch
onValueChange={(value) => setShowBlurs(value)}
value={showBlurs}
/>
</SafeAreaView>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'transparent',
},
blurContainer: {
flex: 1,
backgroundColor: 'transparent',
justifyContent: 'center',
alignItems: 'stretch',
},
row: {
marginTop: 50,
flex: 1,
width: '100%',
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'stretch',
},
blurView: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
width: Dimensions.get('window').width,
},
blurView2: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
},
img: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
height: Dimensions.get('window').height,
width: Dimensions.get('window').width,
},
text: {
fontSize: 20,
fontWeight: 'bold',
textAlign: 'center',
margin: 10,
color: 'white',
},
blurToggle: {
position: 'absolute',
top: 30,
right: 10,
alignItems: 'flex-end',
},
});
export default BlurDemo;
接口说明
[!TIP] "Platform" 列表示该属性在原三方库上支持的平台。
[!TIP] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
属性
| Name | Description | Type | Required | Platform | HarmonyOS Support |
| :--- | :--- | :--- | :--- | :--- | :--- |
| blurType | 模糊类型 | enum | yes | iOS,Android | yes |
| blurAmount? | 0 - 100(Android 上的最大 blurAmount 为 32,因此更高的值将被限制为 32) | number | no | iOS,Android | yes |
| reducedTransparencyFallbackColor? | 降低透明度时的回退颜色 | Any color | no | iOS | no |
| blurRadius? | 匹配 iOS 的 blurAmount | number | no | Android | no |
| downsampleFactor? | 匹配 iOS 的 blurAmount | number | no | Android | no |
| overlayColor? | 基于 iOS blurType 的默认颜色 | Any color | no | Android | no |
blurType
[!TIP] 如果要使用自适应模糊效果需要配置深色模式配置文档如果不配置深色模式则自适应模糊效果将没有深色模式,只有浅色模式。
| Name | Description | Platform | HarmonyOS Support |
| :--- | :--- | :--- | :--- |
| xlight | 超亮模糊类型 | iOS,Android | yes |
| light | 亮色模糊类型 | iOS,Android | yes |
| dark | 暗色模糊类型 | iOS,Android | yes |
| extraDark | 超暗模糊类型(仅限 tvOS) | iOS | yes |
| regular | 常规模糊类型(仅限 iOS 10+ 和 tvOS) | iOS | yes |
| prominent | 显著模糊类型(仅限 iOS 10+ 和 tvOS) | iOS | yes |
| chromeMaterial | 一种自适应模糊效果,营造出正常厚度材质的外观 | iOS 13 only | yes |
| material | 一种自适应模糊效果,营造出正常厚度材质的外观 | iOS 13 only | yes |
| thickMaterial | 一种自适应模糊效果,营造出比正常材质更厚的材质外观 | iOS 13 only | yes |
| thinMaterial | 一种自适应模糊效果,营造出超薄材质的外观 | iOS 13 only | yes |
| ultraThinMaterial | 一种自适应模糊效果,营造出超薄材质的外观 | iOS 13 only | yes |
| chromeMaterialDark | 一种模糊效果,营造出超薄材质的外观,且始终为暗色 | iOS 13 only | yes |
| materialDark | 一种模糊效果,营造出薄材质的外观,且始终为暗色 | iOS 13 only | yes |
| thickMaterialDark | 一种模糊效果,营造出正常厚度材质的外观,且始终为暗色 | iOS 13 only | yes |
| thinMaterialDark | 一种模糊效果,营造出比正常材质更厚的材质外观,且始终为暗色 | iOS 13 only | yes |
| ultraThinMaterialDark | 一种模糊效果,营造出系统镶边(System Chrome)的外观,且始终为暗色 | iOS 13 only | yes |
| chromeMaterialLight | 一种自适应模糊效果,营造出系统镶边(System Chrome)的外观 | iOS 13 only | yes |
| materialLight | 一种自适应模糊效果,营造出正常厚度材质的外观 | iOS 13 only | yes |
| thickMaterialLight | 一种自适应模糊效果,营造出薄材质的外观 | iOS 13 only | yes |
| thinMaterialLight | 一种自适应模糊效果,营造出薄材质的外观 | iOS 13 only | yes |
| ultraThinMaterialLight | 一种自适应模糊效果,营造出超薄材质的外观 | iOS 13 only | yes |
API
[!TIP] "Platform"列表示该组件在原三方库上支持的平台。
[!TIP] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该组件;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
| Name | Description | Type | Required | Platform | HarmonyOS Support |
| :--- | :--- | :--- | :--- | :--- | :--- |
| BlurView | 提供模糊效果的视图组件 | component | no | iOS,Android | yes |
| VibrancyView | Vibrancy 效果让模糊视图下方的内容更加生动地透显出来 | component | no | iOS | no |
遗留问题
- [ ] @react-native-community/blur的 VibrancyView组件未实现HarmonyOS化 issue#7
- [ ] @react-native-community/blur的reducedTransparencyFallbackColor属性未实现 HarmonyOS化issue#8
其他
无
目录结构
rntpc_react-native-blur/
├── harmony/ # 鸿蒙原生模块(HAR 包)
│ └── blur/
│ ├── blur.har # 编译产物
│ ├── BuildProfile.ets
│ ├── build-profile.json5
│ ├── index.ets # 模块入口
│ ├── oh-package.json5 # 鸿蒙包配置
│ ├── hvigorfile.ts
│ ├── consumer-rules.txt
│ ├── obfuscation-rules.txt
│ └── src/main/
│ ├── module.json5
│ ├── cpp/ # C++ TurboModule 实现
│ │ ├── CMakeLists.txt
│ │ ├── BlurPackage.h # 包注册
│ │ ├── BlurViewComponentInstance.{cpp,h} # 组件实例
│ │ ├── BlurViewJSIBinder.h # JSI 绑定
│ │ ├── BlurViewNode.{cpp,h} # 渲染节点
│ │ ├── ComponentDescriptors.{cpp,h}
│ │ ├── EventEmitters.{cpp,h}
│ │ ├── Props.{cpp,h}
│ │ ├── ShadowNodes.{cpp,h}
│ │ └── States.{cpp,h}
│ └── resources/ # 多语言资源
│ ├── base/element/string.json
│ ├── en_US/element/string.json
│ └── zh_CN/element/string.json
│
├── src/ # JS/TS 源码(库入口)
│ ├── index.tsx # 库导出入口
│ ├── components/
│ │ ├── BlurView.harmony.tsx # BlurView 鸿蒙实现
│ │ └── VibrancyView.harmony.tsx # VibrancyView 鸿蒙实现
│ └── fabric/
│ └── BlurViewNativeComponentHarmony.ts # Fabric Codegen 类型
│
├── example/ # 示例工程
│ ├── index.js # RN 注册入口
│ ├── package.json
│ ├── contexts.ts
│ ├── app.json / babel.config.js / metro.config.js / react-native.config.js
│ ├── tsconfig.json / jest.config.js
│ ├── assets/
│ │ └── bgimage.jpeg
│ ├── src/
│ │ └── index.tsx # 示例页面代码
│ ├── scripts/
│ │ └── create-build-profile.js
│ └── harmony/ # 示例鸿蒙工程
│ ├── oh-package.json5 / hvigorfile.ts
│ ├── build-profile.template.json5
│ └── entry/src/main/
│ ├── ets/
│ │ ├── entryability/EntryAbility.ets
│ │ ├── pages/ # 鸿蒙页面(Index / 测试页)
│ │ └── assets/fonts/ # 字体资源
│ ├── cpp/ # C++ 入口 + PackageProvider
│ └── resources/ # 图标、颜色、rawfile 资源
│
├── scripts/
│ └── bootstrap.js # 环境初始化脚本
├── img/ # README 用图
│ ├── blur-effect.gif
│ └── blur-page.png
│
├── package.json # npm 包配置(库名 @react-native-ohos/blur)
├── tsconfig.json / tsconfig.build.json # TypeScript 配置
├── babel.config.js # Babel 配置
├── README.md / README_en.md # 中/英文说明
├── README.OpenSource # 开源信息
├── CHANGELOG.md # 版本变更记录
├── COMMITTERS.md # 贡献者
├── LICENSE # MIT
├── OAT.xml # OpenHarmony 合规扫描配置
├── lefthook.yml # Git 钩子配置
├── .gitignore / .npmignore开源协议
本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。
