react-native图片组件Image

前端之家收集整理的这篇文章主要介绍了react-native图片组件Image前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

react-native学习交流QQ群:806870562


效果


代码示例

  1. import React,{Component} from 'react';
  2. import {
  3. StyleSheet,View,Image,Text
  4. } from 'react-native';
  5.  
  6. const imgUrl = {
  7. uri:"https://f10.baidu.com/it/u=1934388360,3660384600&fm=72"
  8. };
  9.  
  10. type Props = {};
  11. export default class ImagePage extends Component<Props> {
  12. render() {
  13. return(
  14. <View style={styles.containerStyle}>
  15. <View style={{flexDirection:'row',flexWrap:"wrap",justifyContent:"space-around"}}>
  16. <Image style={styles.imageUrlStyle} source={imgUrl}></Image>
  17.  
  18. <View style={{justifyContent:"center",alignItems:"center"}}>
  19. <Image style={[styles.imageUrlStyle,{resizeMode:'contain'}]}
  20. source={{uri:'https://f10.baidu.com/it/u=1934388360,3660384600&fm=72'}} />
  21. <Text style={{position:"absolute",top:20,left:20,color:"#FF4500",}}>
  22. contain全显缩略模式
  23. </Text>
  24. </View>
  25.  
  26. <Image style={[styles.imageUrlStyle,{resizeMode:'center'}]} source={imgUrl}></Image>
  27. <Image style={[styles.imageUrlStyle,{resizeMode:'stretch'}]} source={imgUrl}></Image>
  28. </View>
  29.  
  30. <Image style={styles.imageLocalStyle} source={require("../../Resources/01.png")}></Image>
  31. </View>
  32. );
  33. }
  34. };
  35.  
  36. const styles = StyleSheet.create({
  37. containerStyle: {
  38. marginTop:20,justifyContent:'center',alignItems:'center',},imageUrlStyle: {
  39. margin:10,width: 150,height: 150,backgroundColor: '#C0C0C0',// 显示模式:缩略全显contain,拉升全显(会变形)stretch,裁剪后显示(默认)cover
  40. resizeMode:'cover',imageLocalStyle: {
  41. margin:10,width: 120,height: 120,// 显示模式:缩略全显contain,拉升全显(会变形)stretch,裁剪后显示(默认)cover
  42. resizeMode:'contain',}
  43. });

使用注意

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相关文章