react-native开发Web前端之路让前端飞

React Native实战系列第七篇 — Image组件

2017-05-22  本文已影响735人  撩课_叶建华
Snip20170522_9.png

一、前言

<p></p>
<p></p>

二、Image组件的基本用法

2.1 从当前项目中加载图片
<View style={styles.container}>
        <Text style={styles.textMarginTop}>加载本地的图片</Text>
        <Image 
          source={require('./img/2.png')} 
          style={{width: 120, height: 120}}  />
</View>

<p></p>
<p></p>

2.2 加载使用APP中的图片
<View style={styles.container}>
       <Text style={styles.textMarginTop}>加载原生项目中的图片</Text>
       <Image
         source={{uri: 'icon_homepage_map'}} 
         style={{width: 50,height:50}}/>
</View>

<p></p>
<p></p>

2.3 加载来自网络的图片
<View style={styles.container}>
        <Image source={{uri:'https://www.baidu.com/img/bd_logo1.png'}} 
          style={{flex:1,width:200, height:100, 
          resizeMode: Image.resizeMode.cover}}/>
        <Image source={{uri:'https://www.baidu.com/img/bd_logo1.png'}}
          style={{flex:1,width:200, height:100, 
          resizeMode: Image.resizeMode.contain}}/>
        <Image source={{uri:'https://www.baidu.com/img/bd_logo1.png'}}
          style={{flex:1,width:200, height:100, 
          resizeMode: Image.resizeMode.stretch}}>
</View>

Image.resizeMode.cover:图片居中显示,没有被拉伸,超出部分被截断;
Image.resizeMode.contain:容器完全容纳图片,图片等比例进拉伸;
Image.resizeMode.stretch: 图片被拉伸适应容器大小,有可能会发生变形。

<p></p>
<p></p>

2.4 设置图片为背景
 <Image 
   source={{uri:'https://www.baidu.com/img/bd_logo1.png'}} 
   style={{flex:1,width:200, height:100, 
   resizeMode: Image.resizeMode.stretch}}>
           <Text style={{marginTop: 60, backgroundColor: 'red'}}>
            下面是背景图片
           </Text>
</Image>

<p></p>
<p></p>

三、Image组件的常见属性

3.1 属性方法

<p></p>
<p></p>

3.2 样式风格属性

FlexBox 支持弹性盒子风格
Transforms 支持属性动画
backgroundColor 背景颜色
borderColor 边框颜色
borderWidth 边框宽度
borderRadius 边框圆角
overflow 设置图片尺寸超过容器可以设置显示或者隐藏('visible','hidden')
tintColor 颜色设置
opacity 设置不透明度0.0(透明)-1.0(完全不透明)

<p></p>
<p></p>

四、Image组件的小练习

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

// 导入本地的数据
const dataArr = require('./LocalData/data.json');


export default class XMGMain extends Component {
    render() {
        return (
            <View style={styles.container}>
                {this._renderItem()}
            </View>
        );
    }

    /**
     * 创建一个子盒子放入数组
     * @private
     */
    _renderItem() {
        // 1. 创建组件数组
        let itemArr = [];

        // 2. 遍历
        dataArr.forEach((value, index)=>{
            // 2.1 创建组件装入数组
            itemArr.push(
                <View key={index} style={styles.innerViewStyle}>
                    <Image source={{uri: value.icon}}  style={styles.imgStyle}/>
                    <Text>{value.name}</Text>
                </View>
            );
        });

        return itemArr;
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        /*设置主轴的方向*/
        flexDirection:'row',
        /*换行显示*/
        flexWrap:'wrap',
        justifyContent:'space-around',
    },

    innerViewStyle:{
        width: 100,
        height: 100,
        backgroundColor:'#e8e8e8',
        borderColor:'#ccc',
        borderWidth:1,

        /*主轴和侧轴居中*/
        justifyContent:'center',
        alignItems:'center',

        marginTop: 20
    },

    imgStyle:{
        width: 80,
        height: 80
    }
});


module.exports = XMGMain;

<p></p>
<p></p>

案例运行结果

<p></p>
<p></p>
<p></p>
<p></p>

长按图片-->识别图中二维码

近期正在把公众账号的文章转移到简书,如果要了解第一动态,请关注我的微信公众账号,带你从零到一的进行React Native开发实战,在其他文章中会有对应的code和资料发放!

上一篇 下一篇

猜你喜欢

热点阅读