React-native Loading图的正确加载

Loading图适用于网络图片加载失败或者加载过程中的占位图,以下先展示最简单的网络请求占位图:

请求的图片地址:

const URL = 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=1333472173,1434096058&fm=80&w=179&h=119&img.JPEG';

render方法:

render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          Loading图的模拟加载
        </Text>
        {this.renderLoadImg()}
        <Text style={styles.instructions}>
          1秒以后,会请求网络数据
        </Text>
      </View>
    );
  }

renderLoadImg方法的解释:
当网络数据加载完成后,显示所加载的图片;没有加载完成的时候,显示Loading图。

renderLoadImg(){
    let img;
    if(this.state.imgUrl){
      img = (<Image source={{uri:this.state.imgUrl}} style={{width:200,height:200}} />);
    }else{
      img = (<Image source={require('./img/loading.png')} style={{width:200,height:200}} />)
    }
    return img;
}

模拟漫长的网络请求过程:用到了setTimeout方法,模拟1s后请求成功。

componentDidMount(){
    setTimeout(()=>{
      this.setState({
        imgUrl:URL
      })
    },1000)
} 
完整代码如下:
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Image
} from 'react-native';

const URL = 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=1333472173,1434096058&fm=80&w=179&h=119&img.JPEG';


export default class imgRes extends Component {

  constructor(props){
    super(props);
    this.state = {
      imgUrl:null
    }
  }

  componentDidMount(){
    setTimeout(()=>{
      this.setState({
        imgUrl:URL
      })
    },1000)
    
  } 

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          Loading图的模拟加载
        </Text>
        {this.renderLoadImg()}
        <Text style={styles.instructions}>
          1秒以后,会请求网络数据
        </Text>
      </View>
    );
  }

  renderLoadImg(){
    let img;
    if(this.state.imgUrl){
      img = (<Image source={{uri:this.state.imgUrl}} style={{width:200,height:200}} />);
    }else{
      img = (<Image source={require('./img/loading.png')} style={{width:200,height:200}} />)
    }
    return img;
  }

}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('imgRes', () => imgRes);

效果如下:

示例效果
使RN支持gif图设置方法:

在android\build.gradle中dependencies依赖中添加如下代码:

compile 'com.facebook.fresco:animated-gif:0.13.0'

这样就可以引用gif图片了。。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,698评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,225评论 4 61
  • “游园春色关不住,映日桃花别样红。” 今晨醒来,打开铜镜,忽觉两鬓处不知何时竟已生出些许银丝。看着窗外随风飘落的遍...
    织虹为锦阅读 3,025评论 15 8
  • 最近听了《最重要的事,只有一件》,反思自己,感受颇深,我就是抓不住重点,很多精力被浪费了,最重要的事情只有一件,...
    以缠证道阅读 3,414评论 1 2
  • 的确 我们还年轻 年轻到根本不敢因为一个人或一句话就赌上一生 的确 世界没有变 是我们看错了世界 人心朝着物欲横流...
    蔓草岛屿阅读 1,527评论 0 0