如何使用react-native制作模糊效果?

alu*_*d.g 42 image image-processing blur react-native

在此输入图像描述

如何使用react-native制作模糊效果?喜欢'background-image'

我想切换效果'模糊'和'无','无'意味着没有模糊效果

Gui*_*zog 115

现在你可以使用prop:blurRadius在没有任何库的情况下完成此操作.

例如

<Image
    style={styles.img}
    resizeMode='cover'
    source={imgSrc} 
    blurRadius={1}
/>
Run Code Online (Sandbox Code Playgroud)

说明:数字(1)表示要在图像上应用的模糊量,数字越大,图像越模糊.

不幸的是,这还不适用于Android(RN 0.40.0).然而,对于那些只寻找iOS解决方案的人来说,这可能是有用的.

编辑:它现在似乎在Android上运行.

  • 令人惊奇的是,这在没有任何库的情况下也能工作,简直是个好消息,可以在 `&lt;ImageBackground` 上使用 (3认同)

小智 12

尝试使用“react-native”中的 {ImageBackground} 并像这样设置 blurRadius={number} :

<ImageBackground
      style={{flex: 1}}
      source={require('../assets/example.jpg')}
      blurRadius={90}>
    <Text>Blur background<Text>
</ImageBackground>
Run Code Online (Sandbox Code Playgroud)

https://facebook.github.io/react-native/docs/images.html#background-image-via-nesting https://facebook.github.io/react-native/docs/image.html#blurradius


Flo*_*bre 7

要模糊化整个视图,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>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

// Deps:

 "react-native": "0.53.3",
 "@react-native-community/blur": "^3.2.2"
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明


Tha*_*eez 5

如果您使用 CRNA 创建应用程序,即使用 expo。您可以使用 expo 中的 BlurView。

import {BlurView} from 'expo-blur';

它有两个道具来控制模糊效果。

tint它采用字符串值,即light, default, 或dark。范围intensity1 to 100

Expo Docs 中的模糊视图仅适用于 iOS :

一个 React 组件,它会模糊视图下面的所有内容。在 iOS 上,它呈现原生模糊视图。在 Android 上,它会退回到半透明视图。其常见用途是导航栏、选项卡栏和模态框。

如果您的主要目标是在 iOS 和 Android 上模糊背景,则不建议使用。