react-native图片组件Image

学习交流:https://gitee.com/potato512/Learn_ReactNative

react-native学习交流QQ群:806870562


效果


代码示例

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

const imgurl = {
    uri:"https://f10.baidu.com/it/u=1934388360,3660384600&fm=72"
};

type Props = {};
export default class ImagePage extends Component<Props> {
    render() {
        return(
          <View style={styles.containerStyle}>
              <View style={{flexDirection:'row',flexWrap:"wrap",justifyContent:"space-around"}}>
                  <Image style={styles.imageUrlStyle} source={imgurl}></Image>

                  <View style={{justifyContent:"center",alignItems:"center"}}>
                      <Image style={[styles.imageUrlStyle,{resizeMode:'contain'}]}
                             source={{uri:'https://f10.baidu.com/it/u=1934388360,3660384600&fm=72'}} />
                      <Text style={{position:"absolute",top:20,left:20,color:"#FF4500",}}>
                          contain全显缩略模式
                      </Text>
                  </View>

                  <Image style={[styles.imageUrlStyle,{resizeMode:'center'}]} source={imgurl}></Image>
                  <Image style={[styles.imageUrlStyle,{resizeMode:'stretch'}]} source={imgurl}></Image>
              </View>

            <Image style={styles.imageLocalStyle} source={require("../../Resources/01.png")}></Image>
          </View>
        );
    }
};

const styles = StyleSheet.create({
  containerStyle: {
      marginTop:20,justifyContent:'center',alignItems:'center',},imageUrlStyle: {
      margin:10,width: 150,height: 150,backgroundColor: '#C0C0C0',// 显示模式:缩略全显contain,拉升全显(会变形)stretch,裁剪后显示认)cover
      resizeMode:'cover',imageLocalStyle: {
      margin:10,width: 120,height: 120,// 显示模式:缩略全显contain,拉升全显(会变形)stretch,裁剪后显示认)cover
      resizeMode:'contain',}
});

使用注意

1、图片来源

(1)本地图片使用source={require("../../Resources/01.png")

(2)网络图片使用source={{uri:'https://f10.baidu.com/it/u=1934388360,3660384600&fm=72'}

2、图片显示模式

(1)contain:容器完全容纳图片图片等比例进拉伸;

(2)stretch:图片被拉伸适应容器大小,有可能会发生变形。

(3)cover:图片居中显示,没有被拉伸,超出部分被截断;

(4)center:类似cover

3、图片组件不能包含其他组件

4、图片大小

(1)本地图片:如果没有设置图片的宽高,则显示本地图片的实际大小

(2)网络图片:如果没有设置图片的宽高,则无法显示图片,因为网络图片没有认大小

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...