學習本系列內容須要具有必定 HTML 開發基礎,沒有基礎的朋友能夠先轉至 HTML快速入門(一) 學習html
本人接觸 React Native 時間並非特別長,因此對其中的內容和性質瞭解可能會有所誤差,在學習中若是有錯會及時修改內容,也歡迎萬能的朋友們批評指出,謝謝react
文章初版出自簡書,若是出現圖片或頁面顯示問題,煩請轉至 簡書 查看 也但願喜歡的朋友能夠點贊,謝謝android
{nativeEvent: {layout: {x, y, width, height}}}
url地址
或者 一個本地路徑
在介紹幾種加載圖片資源的方式以前,先肯定一下圖片的樣式,以下react-native
imgStyle: { width:100, height:100, }
從當前RN項目中加載圖片資源,和 html 同樣,咱們須要將圖片放到 RN 項目中,這邊我建立了個 img 文件夾,裏面包含了須要使用到的圖片資源網絡
{/* 不設置尺寸的狀況下 */} <Image source={require('./img/icon.jpg')}></Image>
效果:
app
{/* 設置尺寸的狀況下 */} <Image source={require('./img/icon.jpg')} style={styles.imgStyle}></Image>
效果:
佈局
以iOS爲例,咱們根據原生的開發習慣把圖片放到 images.xcassets 中,這樣在編譯連接完成後,會將裏面的圖片統一打包以供使用學習
若是是Android的話,須要打開目錄 android/app/src/res/ ,將圖片按照安卓原生開發習慣將不一樣大小的圖片方便放進四個文件夾內flex
完成了圖片的導入,最好從新運行模擬器,以確保圖片已經正確打包,而後就是使用圖片的時候了
動畫
{/* 和上面同樣,若是不預先設置尺寸的話,依舊是根據圖片資源本來的大小進行展現 */} {/* 路徑書寫格式中 image! 是固定寫法,後面直接加上圖片的名字便可,不可加 .後綴名 */} <Image source={require('image!lufei')}></Image>
效果:
注意:大括號內的是 uri 不是 url
){/* uri是固定寫法,後面跟上圖片網絡URL地址的字符串便可,還有,網絡圖片必須設置圖片的大小,不然沒法顯示,通常還須要配合填充方式以達到想要的效果 */} <Image source={{uri:'https://img.alicdn.com/tps/TB1OvT9NVXXXXXdaFXXXXXXXXXX-520-280.jpg'}} style={styles.imgStyle}></Image>
效果:
resizeMode屬性
,它包含了3個可選參數,下面就以加載網絡圖片爲例先加載網絡圖片,並設置樣式
// 導入Dimensions庫 var Dimensions = require('Dimensions'); // 入口 export default class TestRN extends Component { render() { return ( <View style={styles.container}> <Image source={{uri:'https://img.alicdn.com/tps/TB1OvT9NVXXXXXdaFXXXXXXXXXX-520-280.jpg'}} style={styles.imgStyle}></Image> </View> ); } } // 樣式 const styles = StyleSheet.create({ container: { backgroundColor:'blue', flex:1, }, imgStyle: { // 設置背景顏色 backgroundColor:'green', // 設置寬度 width:Dimensions.get('window').width, // 設置高度 height:150 } });
效果:
默認
):圖片居中顯示且不拉伸圖片,超出的部分剪裁掉imgStyle: { // 設置背景顏色 backgroundColor:'green', // 設置寬度 width:Dimensions.get('window').width, // 設置高度 height:150, // 設置圖片填充模式 resizeMode:'cover' }
效果:
imgStyle: { // 設置背景顏色 backgroundColor:'green', // 設置寬度 width:Dimensions.get('window').width, // 設置高度 height:150, // 設置圖片填充模式 resizeMode:'contain' }
效果:
imgStyle: { // 設置背景顏色 backgroundColor:'green', // 設置寬度 width:Dimensions.get('window').width, // 設置高度 height:150, // 設置圖片填充模式 resizeMode:'stretch' }
效果:
// 樣式 const styles = StyleSheet.create({ container: { backgroundColor:'blue', flex:1, // 設置主軸對齊方式 justifyContent:'center', // 設置側軸對齊方式 alignItems:'center' }, imgStyle: { backgroundColor:'green', width:Dimensions.get('window').width, height:200, // 設置圖片填充模式 resizeMode:'stretch' } });
效果:
<Image source={{uri:'https://img.alicdn.com/tps/TB1OvT9NVXXXXXdaFXXXXXXXXXX-520-280.jpg'}} style={styles.imgStyle}> <Text style={{marginTop:100}}>這是一張來自淘寶的圖片</Text> </Image>
效果:
如今咱們就結合前面的一下教程,用最通俗的方式作一個簡單的QQ消息模塊界面
import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, Image } from 'react-native'; // 導入Dimensions庫 var Dimensions = require('Dimensions'); // 入口 export default class TestRN extends Component { render() { return ( <View style={styles.container}> {/* 由於還沒講到listView,這邊就用View表明Cell*/} <View style={styles.cellStyle}> {/* 頭像 */} <Image source={require('./img/lufei.jpg')} style={styles.imgStyle}></Image> {/* 暱稱 */} <Text style={styles.nameStyle}>暱稱</Text> </View> {/* 分隔線 */} <View style={styles.lineStyle}></View> <View style={styles.cellStyle}> {/* 頭像 */} <Image source={require('./img/lufei.jpg')} style={styles.imgStyle}></Image> {/* 暱稱 */} <Text style={styles.nameStyle}>暱稱</Text> </View> {/* 分隔線 */} <View style={styles.lineStyle}></View> <View style={styles.cellStyle}> {/* 頭像 */} <Image source={require('./img/lufei.jpg')} style={styles.imgStyle}></Image> {/* 暱稱 */} <Text style={styles.nameStyle}>暱稱</Text> </View> {/* 分隔線 */} <View style={styles.lineStyle}></View> </View> ); } } // 樣式 const styles = StyleSheet.create({ container: { backgroundColor:'white', flex:1, // 設置換行方式 flexWrap:'wrap' }, cellStyle: { // 尺寸 height:44, width:Dimensions.get('window').width, // 設置背景顏色 backgroundColor:'white', // 設置主軸方向 flexDirection:'row', // 設置側軸對齊方式 alignItems:'center' }, imgStyle: { width:30, height:30, // 設置圖片填充模式 resizeMode:'cover', // 設置圓角 borderRadius:15, // 設置圖片位置 marginLeft:10 }, nameStyle: { // 設置暱稱位置 marginLeft:10 }, lineStyle: { // 背景色 backgroundColor:'black', // 設置尺寸 width:Dimensions.get('window').width, height:1 } }); AppRegistry.registerComponent('TestRN', () => TestRN);
{nativeEvent: {layout: {x, y, width, height}}}
url地址
或者 一個本地路徑
在介紹幾種加載圖片資源的方式以前,先肯定一下圖片的樣式,以下
imgStyle: { width:100, height:100, }
從當前RN項目中加載圖片資源,和 html 同樣,咱們須要將圖片放到 RN 項目中,這邊我建立了個 img 文件夾,裏面包含了須要使用到的圖片資源
{/* 不設置尺寸的狀況下 */} <Image source={require('./img/icon.jpg')}></Image>
效果:
{/* 設置尺寸的狀況下 */} <Image source={require('./img/icon.jpg')} style={styles.imgStyle}></Image>
效果:
以iOS爲例,咱們根據原生的開發習慣把圖片放到 images.xcassets 中,這樣在編譯連接完成後,會將裏面的圖片統一打包以供使用
若是是Android的話,須要打開目錄 android/app/src/res/ ,將圖片按照安卓原生開發習慣將不一樣大小的圖片方便放進四個文件夾內
完成了圖片的導入,最好從新運行模擬器,以確保圖片已經正確打包,而後就是使用圖片的時候了
{/* 和上面同樣,若是不預先設置尺寸的話,依舊是根據圖片資源本來的大小進行展現 */} {/* 路徑書寫格式中 image! 是固定寫法,後面直接加上圖片的名字便可,不可加 .後綴名 */} <Image source={require('image!lufei')}></Image>
效果:
注意:大括號內的是 uri 不是 url
){/* uri是固定寫法,後面跟上圖片網絡URL地址的字符串便可,還有,網絡圖片必須設置圖片的大小,不然沒法顯示,通常還須要配合填充方式以達到想要的效果 */} <Image source={{uri:'https://img.alicdn.com/tps/TB1OvT9NVXXXXXdaFXXXXXXXXXX-520-280.jpg'}} style={styles.imgStyle}></Image>
效果:
resizeMode屬性
,它包含了3個可選參數,下面就以加載網絡圖片爲例先加載網絡圖片,並設置樣式
// 導入Dimensions庫 var Dimensions = require('Dimensions'); // 入口 export default class TestRN extends Component { render() { return ( <View style={styles.container}> <Image source={{uri:'https://img.alicdn.com/tps/TB1OvT9NVXXXXXdaFXXXXXXXXXX-520-280.jpg'}} style={styles.imgStyle}></Image> </View> ); } } // 樣式 const styles = StyleSheet.create({ container: { backgroundColor:'blue', flex:1, }, imgStyle: { // 設置背景顏色 backgroundColor:'green', // 設置寬度 width:Dimensions.get('window').width, // 設置高度 height:150 } });
效果:
默認
):圖片居中顯示且不拉伸圖片,超出的部分剪裁掉imgStyle: { // 設置背景顏色 backgroundColor:'green', // 設置寬度 width:Dimensions.get('window').width, // 設置高度 height:150, // 設置圖片填充模式 resizeMode:'cover' }
效果:
imgStyle: { // 設置背景顏色 backgroundColor:'green', // 設置寬度 width:Dimensions.get('window').width, // 設置高度 height:150, // 設置圖片填充模式 resizeMode:'contain' }
效果:
imgStyle: { // 設置背景顏色 backgroundColor:'green', // 設置寬度 width:Dimensions.get('window').width, // 設置高度 height:150, // 設置圖片填充模式 resizeMode:'stretch' }
效果:
// 樣式 const styles = StyleSheet.create({ container: { backgroundColor:'blue', flex:1, // 設置主軸對齊方式 justifyContent:'center', // 設置側軸對齊方式 alignItems:'center' }, imgStyle: { backgroundColor:'green', width:Dimensions.get('window').width, height:200, // 設置圖片填充模式 resizeMode:'stretch' } });
效果:
<Image source={{uri:'https://img.alicdn.com/tps/TB1OvT9NVXXXXXdaFXXXXXXXXXX-520-280.jpg'}} style={styles.imgStyle}> <Text style={{marginTop:100}}>這是一張來自淘寶的圖片</Text> </Image>
效果:
如今咱們就結合前面的一下教程,用最通俗的方式作一個簡單的QQ消息模塊界面單元格
import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, Image } from 'react-native'; // 導入Dimensions庫 var Dimensions = require('Dimensions'); // 入口 export default class TestRN extends Component { render() { return ( <View style={styles.container}> {/* 由於還沒講到listView,這邊就用View表明Cell*/} <View style={styles.cellStyle}> {/* 頭像 */} <Image source={require('./img/lufei.jpg')} style={styles.imgStyle}></Image> {/* 暱稱 */} <Text style={styles.nameStyle}>暱稱</Text> </View> {/* 分隔線 */} <View style={styles.lineStyle}></View> <View style={styles.cellStyle}> {/* 頭像 */} <Image source={require('./img/lufei.jpg')} style={styles.imgStyle}></Image> {/* 暱稱 */} <Text style={styles.nameStyle}>暱稱</Text> </View> {/* 分隔線 */} <View style={styles.lineStyle}></View> <View style={styles.cellStyle}> {/* 頭像 */} <Image source={require('./img/lufei.jpg')} style={styles.imgStyle}></Image> {/* 暱稱 */} <Text style={styles.nameStyle}>暱稱</Text> </View> {/* 分隔線 */} <View style={styles.lineStyle}></View> </View> ); } } // 樣式 const styles = StyleSheet.create({ container: { backgroundColor:'white', flex:1, // 設置換行方式 flexWrap:'wrap' }, cellStyle: { // 尺寸 height:44, width:Dimensions.get('window').width, // 設置背景顏色 backgroundColor:'white', // 設置主軸方向 flexDirection:'row', // 設置側軸對齊方式 alignItems:'center' }, imgStyle: { width:30, height:30, // 設置圖片填充模式 resizeMode:'cover', // 設置圓角 borderRadius:15, // 設置圖片位置 marginLeft:10 }, nameStyle: { // 設置暱稱位置 marginLeft:10 }, lineStyle: { // 背景色 backgroundColor:'black', // 設置尺寸 width:Dimensions.get('window').width, height:1 } }); AppRegistry.registerComponent('TestRN', () => TestRN);
效果: