React-Native项目中使用Gif图做loading遮罩

作者: 尹_路人 | 来源:发表于2016-10-26 18:26 被阅读3162次

    先来张效果图

    废话不多说
    Show you the Code!

    源码

    功能还是在原来的项目中实现的,代码可以单独剥离,自己动手丰衣足食
    源码点击这里

    文件路径: ListViewLoadMore/app/common/LoadingView.js

    import React, { Component } from 'react';
    import {
        View,
        Image,
        StyleSheet,
        Dimensions,
        TouchableOpacity,
        Modal
    } from 'react-native';
    const { width, height } = Dimensions.get('window')
    import loadingImage from '../../assets/0.gif'
    class LoadingView extends Component{
        constructor(props) {
            super(props);
        }
        _close(){
            console.log("onRequestClose ---- ")
        }
        render() {
            const { showLoading, opacity, backgroundColor } = this.props
            return (
                <Modal onRequestClose={() => this._close()} visible={showLoading} transparent>
                    <View style={ [styles.loadingView, {opacity: opacity||0.3, backgroundColor: backgroundColor||'gray'}]}></View>
                    <View style={ styles.loadingImageView }>
                        <View style={ styles.loadingImage }>
                            {
                                this.props.loadingViewClick?
                                <TouchableOpacity onPress={ this.props.loadingViewClick }>
                                    <Image style={ styles.loadingImage } source={ loadingImage }/>
                                </TouchableOpacity>
                                :
                                <Image style={ styles.loadingImage } source={ loadingImage }/>
                            }
                        </View>
                    </View>
                </Modal>
            )
        }
    }
    const styles = StyleSheet.create({
        loadingView: {
            flex: 1,
            height,
            width,
            position: 'absolute'
        },
        loadingImage: {
            width: 150,
            height: 100,
        },
        loadingImageView: {
            position: 'absolute',
            width,
            height,
            justifyContent: 'center',
            alignItems: 'center'
        }
    })
    LoadingView.propTypes = {
        loadingViewClick: React.PropTypes.func, //.isRequired,
        showLoading: React.PropTypes.bool.isRequired,
        opacity: React.PropTypes.number,
        backgroundColor: React.PropTypes.string
    }
    
    
    export default LoadingView
    

    备注说明:

    • 代码就这么多,如果有其他方面的需求,诸如添加文案,可以自己在此基础上自定义,应该没什么难度
    • 图片资源可以自己设置和修改

    使用

    在需要使用的地方 先引入组件

    import LoadingView from '../common/LoadingView.js'
    

    然后在render()中添加组件

    render() {
        return (
            <View style={ styles.mainView }>
                
                ...
                
                <LoadingView showLoading={ this.state.showLoading } />
            </View>
        )
    }
    
    • 组件的showLoading属性是必需的,显示与隐藏是通过showLoading控制的
    • 组件支持自定义背景不透明度 opacity,默认0.6
    • 组件支持自定义背景颜色 backgroundcolor,默认gray
    • 组件支持gif图点击事件loadingViewClick(可选)

    Android需要特殊处理Gif加载

    android/app/build.gradle中需要添加以下内容

    dependencies {
      // If your app supports Android versions before Ice Cream Sandwich (API level 14)
      compile 'com.facebook.fresco:animated-base-support:0.11.0'
    
      // For animated GIF support
      compile 'com.facebook.fresco:animated-gif:0.11.0'
    
      // For WebP support, including animated WebP
      compile 'com.facebook.fresco:animated-webp:0.11.0'
      compile 'com.facebook.fresco:webpsupport:0.11.0'
    
      // For WebP support, without animations
      compile 'com.facebook.fresco:webpsupport:0.11.0'
    }
    

    Over

    先这样,有问题留言

    相关文章

      网友评论

      • DeepFull:为什么我的gif只播放一次额
      • 0fb0420863cc:import loadingImage from '../../assets/0.gif'
        <Image style={ styles.loadingImage } source={ loadingImage }/>

        楼主 我这样加载GIF图片 在虚拟机正常 在真机上就看不到GIF图片了 请教下应该怎么处理?
      • 嘿_小小小:你好,我的gif为什么总是显示一半,就重新开始了
        嘿_小小小:嗨呀,和你上面的代码一模一样的。 我发现是这样的,当gif持续时长很长的时候, 是显示不完全的, 不过一般加载的gif都很短,是一个重复的动作,没多大的影响。
        尹_路人:@Zain_yan 你说的问题我的确遇见过一次,但是没有刻意调整哪里后来就好了,也就没再细究 :joy: :joy: :joy: ,你贴一下你的代码,或者发我邮箱
      • Hank_谢旱:安卓的那个提示太关键,非常感谢
      • 孙小超:android新版的RN也默认支持gif了,其实就是集成了fresco
        卿可津:hi,我的RN 是 0.43,请问为什么还是不支持 GIF 呢?在dependency 找不到相关依赖。
        尹_路人:@孙小超 多谢! :clap:
      • 490422f5d58d:溜溜溜,感谢分享

      本文标题:React-Native项目中使用Gif图做loading遮罩

      本文链接:https://www.haomeiwen.com/subject/wjfjuttx.html