React Native Image 实现placeholder占位图

react-native Image没有placeholder这样的props,但是业务有需要这种场景, 解决方法为:

  • 使用ImageBackground包裹Image,如果图片有透明度,背景图和网络加载的图片就叠加在一起了

组件类ImageView.js

import React from 'react';
import {Image, StyleSheet, View, ViewPropTypes} from 'react-native';
import PropTypes from 'prop-types'; class ImageView extends React.PureComponent {
static propTypes = {
source: PropTypes.object,
style: ViewPropTypes.style,
placeholderSource: PropTypes.number.isRequired,
};
state: {
loading: boolean;
}; constructor(props) {
super(props);
this.state = {loading: true,};
} render() {
return (<View style={this.props.style}>
<Image style={[this.props.style, styles.imageStyle]} source={this.props.source} onLoad={() => this.setState({loading: false})}/>
{this.state.loading ?
<Image style={[this.props.style, styles.imageStyle]} source={this.props.placeholderSource}/> : null}
</View>
);
} } const styles = StyleSheet.create({
imageStyle: {
position: 'absolute',
top: 0,
right: 0,
left: 0,
bottom: 0
}
}); export default ImageView;

二、使用

<ImageView style={styles.icon} source={{uri: imageUrl}} placeholderSource={require("./img/icon.png")}/>

本博客地址: wukong1688

本文原文地址:https://www.cnblogs.com/wukong1688/p/10983879.html

转载请著名出处!谢谢~~

[RN] React Native Image 实现placeholder占位图的更多相关文章

  1. [RN] React Native 幻灯片效果 Banner

    [RN] React Native 幻灯片效果 Banner 1.定义Banner import React, {Component} from 'react'; import {Image, Scr ...

  2. [RN] React Native 实现 类似QQ 登陆页面

    [RN] React Native 实现 类似QQ 登陆页面 一.主页index.js 项目目录下index.js /** * @format */ import {AppRegistry} from ...

  3. [RN] React Native 实现图片预览

    [RN] React Native 实现图片预览 效果预览: 代码如下: 'use strict'; import React, {Component} from 'react'; import {I ...

  4. [RN] React Native 常见基本问题归纳总结

    [RN] React Native  常见基本问题归纳总结 本问题总结涉及到版本为: "react": "16.8.3","react-native& ...

  5. [RN] React Native 关闭所有黄色警告

    [RN] React Native 关闭所有黄色警告 console.ignoredYellowBox = ['Warning: BackAndroid is deprecated. Please u ...

  6. [RN] React Native 下实现底部标签(支持滑动切换)

    上一篇文章 [RN] React Native 下实现底部标签(不支持滑动切换) 总结了不支持滑动切换的方法,此篇文章总结出 支持滑动 的方法 准备工作之类的,跟上文类似,大家可点击上文查看相关内容. ...

  7. [RN] React Native 常用命令行

    [RN] React Native 常用命令行 1.查看当前版本 react-native --version 或 react-native -v 2.创建指定版本的React Native项目 1) ...

  8. [RN] React Native 使用 阿里 ant-design

    React Native 使用 阿里 ant-design 实例效果如图: 一.安装 npm install antd-mobile-rn --save npm install babel-plugi ...

  9. [RN] React Native 获取地理位置

    React Native 获取地理位置 实现原理: 1.用  navigator.geolocation.getCurrentPosition 获取到坐标信息 2.调用 高德地图 接口,解析位置数据 ...

随机推荐

  1. mysql 基本操作 三

    1.alter 创建测试表 MariaDB [jason]> create table testalter_tbl(i )); Query OK, rows affected (0.08 sec ...

  2. WCF服务支持HTTP(get,post)

    WCF服务支持HTTP(get,post)方式请求例子   方式一: /// <summary> /// Http Get请求 /// </summary> /// <p ...

  3. 【javascript】判断是否为正整数

    function isNormalInteger(str) { var n = Math.floor(Number(str)); return n !== Infinity && St ...

  4. pandas的使用(7)--分组

    pandas的使用(7)--分组

  5. 我的周记10——“知行合一"

    印象中有个名人说过一句名言:与其游手好闲地学习,不如学习游手好闲 来自 玉伯 .  字是真的好看,有风格 现在已经是第十篇周记了,写着写着慢慢偏离了初衷,但庆幸的是坚持下来写.我相信在用心写好每篇周记 ...

  6. 脱离 WebView 的通信 JavaScriptCore

    JavascriptCore JavascriptCore 一直作为 WebKit 中内置的 JS 引擎使用,在 iOS7 之后,Apple 对原有的 C/C++ 代码进行了 OC 封装,成为系统级的 ...

  7. svg图片拖动与缩放

    引入jquery.js文件,svg-pan-zoom.min.js文件 和 hammer.min.js 文件 这三个文件可以在网上搜一下下载 //svg拖动和缩放 initPanZoom() { th ...

  8. jquery添加插件

    转自:https://www.cnblogs.com/joey0210/p/3408349.html 前言 如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquer ...

  9. Java--8--新特性--串并行流与ForkJoin框架

    并行流就是把一个内容分成多个数据块,并用不同的线程分别处理每个数据块的流.穿行流则相反,并行流的底层其实就是ForkJoin框架的一个实现. 那么先了解一下ForkJoin框架吧. Fork/Join ...

  10. Apache源码编译安装脚本

      Apache是开源的的.最流行的Web服务器软件之一,它快速.可靠并且可通过简单的API扩充,将Perl/Python/PHP等解释器编译到服务器中.Apache的模块超多,以及具有运行稳定,强大 ...