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閱讀 173,242評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,229評論 4 61
  • “游園春色關不住,映日桃花別樣紅。” 今晨醒來,打開銅鏡,忽覺兩鬢處不知何時竟已生出些許銀絲。看著窗外隨風飄落的遍...
    織虹為錦閱讀 395評論 15 8
  • 最近聽了《最重要的事,只有一件》,反思自己,感受頗深,我就是抓不住重點,很多精力被浪費了,最重要的事情只有一件,...
    以纏證道閱讀 509評論 1 2
  • 的確 我們還年輕 年輕到根本不敢因為一個人或一句話就賭上一生 的確 世界沒有變 是我們看錯了世界 人心朝著物欲橫流...
    蔓草島嶼閱讀 231評論 0 0