how to make the blur effect with react-native?

荒凉一梦 提交于 2019-11-28 22:02:29

问题


how to make the blur effect with react-native ? like 'background-image'

and i want to switch the effect 'blur' and 'none','none' means no blur effect


回答1:


Now you can do this without any library using the prop: blurRadius.

E.g

<Image
    style={styles.img}
    resizeMode='cover'
    source={imgSrc} 
    blurRadius={1}
/>

Explanation: the number(1) means the amount of blur you want to apply on the image, the higher the number, the blurrier the image.

Unfortunately, this doesn't work on Android yet (RN 0.40.0). Nevertheless, it could be useful to who's looking for only an iOS solution.

Edit: It seems to be working on Android now.




回答2:


To blur and an entire View in react-native you can use @react-native-community/blur and apply it like this:

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

// Deps:

 "react-native": "0.53.3",
 "@react-native-community/blur": "^3.2.2"

Result:




回答3:


Install react-native-blur:

npm install react-native-blur

import BlurView from 'react-native-blur';

...
<BlurView blurType="light" style={styles.blur}>
...



回答4:


Try using {ImageBackground} from 'react-native' and set blurRadius={number} like this:

<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




回答5:


If you created your app using CRNA i.e, using expo. You can use BlurView from expo.

import {BlurView} from 'expo';

It got two props to control the blur effect.

tint which takes string value namely light, default, or dark. and intensity which ranges from 1 to 100




回答6:


Try this blurry library.

dependencies :: compile 'jp.wasabeef:blurry:2.0.2'

https://github.com/wasabeef/Blurry




回答7:


you can use this library to get the effects desired. https://github.com/react-native-fellowship/react-native-blur




回答8:


I found this npm look very good react-native-blur

usage




回答9:


With recent React native version (0.57) you can use blurRadius, it works both iOS and Android http://facebook.github.io/react-native/docs/image#blurradius



来源:https://stackoverflow.com/questions/37131278/how-to-make-the-blur-effect-with-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!