【发布时间】:2016-09-05 00:05:26
【问题描述】:
【问题讨论】:
标签: image image-processing react-native blur
【问题讨论】:
标签: image image-processing react-native blur
现在您可以在不使用任何库的情况下使用以下属性执行此操作:blurRadius。
例如
<Image
style={styles.img}
resizeMode='cover'
source={imgSrc}
blurRadius={1}
/>
说明:数字(1)表示您要对图像应用的模糊量,数字越大,图像越模糊。
不幸的是,这还不能在 Android 上运行 (RN 0.40.0)。不过,对于只在寻找 iOS 解决方案的人来说,它可能会很有用。
编辑:它现在似乎可以在 Android 上运行。
【讨论】:
<ImageBackground上使用
尝试使用“react-native”中的 {ImageBackground} 并像这样设置 blurRadius={number}:
<ImageBackground
style={{flex: 1}}
source={require('../assets/example.jpg')}
blurRadius={90}>
<Text>Blur background<Text>
</ImageBackground>
https://facebook.github.io/react-native/docs/images.html#background-image-via-nesting https://facebook.github.io/react-native/docs/image.html#blurradius
【讨论】:
要在react-native 中模糊和整个视图,您可以使用@react-native-community/blur 并像这样应用它:
import React, { Component } from 'react';
import { BlurView } from '@react-native-community/blur';
import {
StyleSheet,
Text,
View,
findNodeHandle,
Platform,
Image,
} from 'react-native';
const styles = StyleSheet.create({
title: {
color: 'black',
fontSize: 15,
},
absolute: {
position: 'absolute',
top: 0,
left: 0,
bottom: 0,
right: 0,
},
blurredView: {
// For me android blur did not work until applying a background color:
backgroundColor: 'white',
},
});
export default class MyBlurredComponent extends Component {
constructor(props) {
super(props);
this.state = { viewRef: null };
}
onTextViewLoaded() {
this.setState({ viewRef: findNodeHandle(this.viewRef) });
}
render() {
return (
<View>
<View
style={[
styles.blurredView,
]}
ref={(viewRef) => { this.viewRef = viewRef; }}
onLayout={() => { this.onTextViewLoaded(); }}
>
<Image
style={{ width: 100, height: 100 }}
source={{ uri: 'https://facebook.github.io/react-native/docs/assets/GettingStartedCongratulations.png' }}
/>
<Text style={[styles.title]}>
TEXT 2222 \n
TEXT 3333
</Text>
</View>
{
(this.state.viewRef || Platform.OS === 'ios') && <BlurView
style={styles.absolute}
viewRef={this.state.viewRef}
blurType="light"
blurAmount={3}
blurRadius={5}
/>
}
</View>
);
}
}
// 部门:
"react-native": "0.53.3",
"@react-native-community/blur": "^3.2.2"
结果:
【讨论】:
npm install react-native-blur
import BlurView from 'react-native-blur';
...
<BlurView blurType="light" style={styles.blur}>
...
【讨论】:
如果您使用 CRNA 创建应用,即使用 expo。您可以使用 Expo 中的 BlurView。
import {BlurView} from 'expo';
它有两个道具来控制模糊效果。
tint 接受字符串值,即light、default 或dark。
和intensity,范围从1 to 100
【讨论】:
任何试图在 react native android 中模糊视频视图的人都无法使用在编写此答案时可用的库来做到这一点。但是我通过使用 WebView 作为视频的框架来实现这个效果。编写一个带有 video 标签的小 HTML 页面,并根据您的要求对其进行样式设置,然后将带有 blur 作为值的 CSS 过滤器属性添加到 video 标签的样式中。还创建一些 javascript 函数来播放和暂停视频(它们将只是一个衬里),您可以使用 WebView 的 injectJavaScript() 方法从 react-native 代码中调用这些函数。您可以使用 WebView 组件的源探针的 html 属性将此 html 代码直接添加到 WebView。这显然是一个 hacky 解决方案,它不会像本地解决方案那样快。如果你想应用一些动画或者对视频布局做一些事情,那么在 react-native 代码中使用 WebView 组件,这将和其他 react-native 组件一样快。
【讨论】:
使用最新的 React 本机版本 (0.57),您可以使用 blurRadius,它适用于 iOS 和 Android http://facebook.github.io/react-native/docs/image#blurradius
【讨论】:
【讨论】:
实现此功能的另一种方法是使用来自expo Blur 的模糊视图。您可以通过运行将其添加到 expo 托管项目中:
expo install expo-blur
Read more here
Android 和 iOS 都支持,也有 Web 支持。
示例用法:
import { BlurView } from 'expo-blur';
import {Text} from 'react-native'
const ExampleBlurView=()=>{
return( <BlurView intensity={80} tint={'default'} style={{flex:1}}>
<Text>Hello World</Text>
<BlurView/>
})}
【讨论】:
使用 blurRadius 道具。它预先附加到 React Native 中所有基于 Image 的组件,例如 Image、BackgroundImage。一个从 0 到 100 的数值,代表半径百分比,其中 10 = 10% 和 100 = 100%
【讨论】:
正如@Shaikh Amaan FM 所说,您可以使用 WebView。它必须位于要模糊的视图上方。
...
<WebView
style={[s.absolute, s.transparent]}
originWhitelist={['*']}
source={{ html:
'<div style="' +
'position: absolute; top: 0; right:0; bottom: 0; left: 0;' +
'background: rgba(255,255,255,0.2); backdrop-filter: blur(48px);' +
'/*width:100%; height:100%; margin:0; padding:-10px;*/' +
'/*background: #ff000033;*/ /*transparent*/ /*background: #4fc3f733;*/
/*border: none*/" ' +
'></div>'
}}
/>
...
const s = StyleSheet.create({
absolute: {
position: 'absolute',
top: 0,
left: 0,
bottom: 0,
right: 0,
},
transparent: {
backgroundColor: '#00000000'
}
});
【讨论】: