Image组件类似于iOS中UIImage控件,该组件可以通过多种方式加载图片资源。
使用方式,加载方式有如下几种:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | /** * Sample React Native App * https://github.com/facebook/react-native * 周少停 * image 加载的三种方式+设置图片的内容模式 */ import React, { Component } from 'react' ; import { AppRegistry, StyleSheet, Text, Image, View, } from 'react-native' class Project extends Component { render() { return ( <View style={styles.container}> <Text>加载工程中图片</Text> <Image source={require( './imgs/1111.png' )} style={{width:120,height:120}}/> <Text>加载Xcode中asset的图片,也可以使用uri的方式加载</Text> <Image source={require( 'image!520' )} style={{width:120,height:120}} /> <Text>加载网络中的图片</Text> <Image source={{uri: 'https://unsplash.it/600/400/?random' }} style={{width:120,height:200}}/> <Text>设置加载图片的模式</Text> <Image source={{uri: 'https://unsplash.it/600/400/?random' }} style={{width:120,height:200,resizeMode:Image.resizeMode.cover}}/> <Image source={{uri: 'https://unsplash.it/600/400/?random' }} style={{width:120,height:200,resizeMode:Image.resizeMode.contain}}/> <Image source={{uri: 'https://unsplash.it/600/400/?random' }} style={{width:120,height:200,resizeMode:Image.resizeMode.stretch}}/> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, marginTop:20, //上边距 flexDirection: 'column' , //主轴方向 垂直 justifyContent: 'flex-start' , //控件在主轴上的对齐方向 alignItems: 'flex-start' , //控件在侧轴上的对齐方向 backgroundColor: '#F5FCFF' , } }); AppRegistry.registerComponent( 'Project' , () => Project);<br> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | /** * Sample React Native App * https://github.com/facebook/react-native * 周少停 2016-09-13 * Imaage的常见属性 */ import React, { Component } from 'react' ; import { AppRegistry, StyleSheet, Text, View, Image } from 'react-native' ; //导入json数据 var BadgeData = require( './BadgeData.json' ); //定义一些全局变量 var Dimensions = require( 'Dimensions' ); var {width} = Dimensions.get( 'window' ); //屏宽 var cols = 3 //总列数 var boxW = 100; //单个盒子的宽度&高度 var vMargin = (width - cols*boxW)/(cols + 1); //盒子中间的水平间距 var hMargin = 25; class Project extends Component { render() { return ( <View style={styles.container}> { /*返回包的数据*/ } { this .renderAllBadge()} </View> ); } //返回所有的包 renderAllBadge(){ //定义一个数组,装每个包的信息 var allBadge = []; //遍历json数据 for ( var i=0;i<BadgeData.data.length;i++){ //去除单独的数据对象 var badge = BadgeData.data[i]; //直接装入数组 allBadge.push( <View key={i} style={styles.outViewStyle}> <Image source={{uri:badge.icon}} style={styles.imageStyle} /> <Text style={styles.mainTitleStyle}> {badge.title} </Text> </View> ); } //返回数组 return allBadge; } } const styles = StyleSheet.create({ container: { flex: 1, marginTop:20, //上边距 flexDirection: 'row' , //主轴方向,水平 alignItems: 'flex-start' , //定义控件在侧轴上的对齐方式 flexWrap: 'wrap' , //是否换行 backgroundColor: '#F5FCFF' }, outViewStyle: { backgroundColor: 'gray' , alignItems: 'center' , width:boxW, height:boxW, marginLeft:vMargin, marginBottom:hMargin }, imageStyle:{ width:80, height:80 }, mainTitleStyle:{ color: 'white' } }); AppRegistry.registerComponent( 'Project' , () => Project); |
分类:
React Native
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】