基础篇章:React Native之 Image 的讲解

今天一起来学习一些Image这个组件,它其实就是相当于我们android控件中的ImageView。

我们先看例子,看看加载本地图片和远程服务器图片的方式,其实差不多。

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

class DisplayAnImage extends Component {
  render() {
    return (
      <View> <Image  source={require('./img/favicon.png')} /> <Image  style={{width: 50,height: 50}} source={{uri: 'https://facebook.github.io/react/img/logo_og.png'}} /> </View> ); } }

第一个就是直接在source里写相对路径,第二个就是直接写图片的服务器地址即可。

当然它也支持在android中显 示GIF 和 WebP 图片,方式如下:
1. 在android/app/build.gradle中依赖下列开源库

dependencies {
// If your app supports Android versions before Ice Cream Sandwich (API level 14)
compile 'com.facebook.fresco:animated-base-support:0.11.0'
// For animated GIF support
compile 'com.facebook.fresco:animated-gif:0.11.0'
// For WebP support,including animated WebP
compile 'com.facebook.fresco:animated-webp:0.11.0'
compile 'com.facebook.fresco:webpsupport:0.11.0'
// For WebP support,without animations
compile 'com.facebook.fresco:webpsupport:0.11.0'
}

2. 在proguard-rules.pro中配置防混淆

-keep class com.facebook.imagepipeline.animated.factory.AnimatedFactoryImpl {
public AnimatedFactoryImpl(com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory,com.facebook.imagepipeline.core.ExecutorSupplier);
}

属性

  • onLayout function 布局发生变化时调用,参数为:{nativeEvent: {layout: {x,y,width,height}}}.
  • onLoad function 图片加载成功时回调该方法。
  • onLoadEnd function 加载结束后,不管成功与否,都回调该方法。
  • onLoadStart function 顾名思义,加载开始时调用。
  • resizeMode enum(‘cover’,‘contain’,‘stretch’) 决定当组件尺寸和图片尺寸不成比例的时候如何调整图片的大小。
  • source {uri: string} uri是一个表示图片的资源标识的字符串,它可以是一个http地址或是一个本地文件路径(使用require(相对路径)来引用)。

Image的style

  • backfaceVisibility [‘visible’,‘hidden’] 隐藏或者显示
  • backgroundColor color 背景色
  • borderBottomLeftRadius 左下角圆角大小
  • borderBottomRightRadius
  • borderColor color 边框颜色
  • borderRadius 边框圆角
  • borderTopLeftRadius
  • borderTopRightRadius
  • borderWidth number 边框宽度
  • opacity 设置透明度
  • overflow [‘visible’,‘hidden’] 设置图片尺寸超过容器可以设置显示或者隐藏
  • resizeMode 图片调整模式
  • tintColor color 颜色设置
  • overlayColor 当图片圆角显示时,剩余空间设置的颜色,android独有

例子实践

看看我模仿的QQ上的一个页面,漂不漂亮?说实话,敲代码这个东西,还得实践,看看这个效果,通过这几篇简单的学习,你能实现吗?

效果图

例子代码

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

class ImageDemo extends Component {
  render() {
    return (
      <View style={styles.container}> <View style={styles.title_view}> <Text style={styles.title_text}> 空间动态 </Text> </View> <View style={styles.three_image_view}> <View style={styles.vertical_view}> <Image source={require('./img/igs.png')} style={{alignSelf:'center',width:45,height:45}} /> <Text style={styles.top_text}> 好友动态 </Text> </View> <View style={styles.vertical_view}> <Image source={require('./img/eqc.png')} style={{alignSelf:'center',height:45}}/> <Text style={styles.top_text}> 附近 </Text> </View> <View style={styles.vertical_view}> <Image source={require('./img/iei.png')} style={{alignSelf:'center',height:45}}/> <Text style={styles.top_text} > 兴趣部落 </Text> </View> </View> <View style={{height:30,backgroundColor:'#f9f9fb'}}/> <View style={styles.rectangle_view}> <View style={{flexDirection:'row',alignItems: 'center'}}> <Image source={require('./img/nsa.png')} style={{alignSelf:'center',width:30,height:30}}/> <Text style={styles.rectangle_text} > 羽毛球 </Text> </View> <Image source={require('./img/ppe.png')} style={{alignSelf:'center',width:20,height:20}}/> </View> <View style={styles.rectangle_view}> <View style={{flexDirection:'row',alignItems: 'center'}}> <Image source={require('./img/nsb.png')} style={{alignSelf:'center',height:30}}/> <Text style={styles.rectangle_text} > 火车票 </Text> </View> <Image source={require('./img/ppe.png')} style={{alignSelf:'center',alignItems: 'center'}}> <Image source={require('./img/nrz.png')} style={{alignSelf:'center',height:30}}/> <Text style={styles.rectangle_text} > 视频 </Text> </View> <Image source={require('./img/ppe.png')} style={{alignSelf:'center',height:20}}/> </View> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1,backgroundColor: 'white',},title_view:{ flexDirection:'row',height:50,justifyContent: 'center',alignItems: 'center',backgroundColor:'#27b5ee',title_text:{ color:'white',fontSize:20,textAlign:'center' },three_image_view:{ paddingTop: 15,flexDirection:'row',justifyContent: 'space-around',backgroundColor:'white',vertical_view:{ justifyContent: 'center',paddingBottom:15,top_text:{ marginTop:5,color:'black',fontSize:16,rectangle_view:{ paddingTop:8,paddingBottom:8,paddingLeft:15,paddingRight:15,justifyContent: 'space-between',borderBottomColor:'#dedfe0',borderBottomWidth:1,rectangle_text:{ color:'black',textAlign:'center',paddingLeft:8,}); AppRegistry.registerComponent('ImageDemo',() => ImageDemo);

最后我想说,不懂的可以留言,或者去我的微信公众号下面留言,评论,一起交流学习。我的微信公众号:非著名程序员。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如果组件之中有复用的代码,需要重新创建一个父类,父类中存储公共代码,返回子类,同时把公用属性...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例如我们的 setState 函数式同步执行的,我们的事件处理直接绑定在了 dom 元素上,这些都跟 re...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom 转为真实 dom 进行挂载。其实函数是组件和类组件也是在这个基础上包裹了一层,一个是调...
react 本身提供了克隆组件的方法,但是平时开发中可能很少使用,可能是不了解。我公司的项目就没有使用,但是在很多三方库中都有使用。本小节我们来学习下如果使用该...
mobx 是一个简单可扩展的状态管理库,中文官网链接。小编在接触 react 就一直使用 mobx 库,上手简单不复杂。
我们在平常的开发中不可避免的会有很多列表渲染逻辑,在 pc 端可以使用分页进行渲染数限制,在移动端可以使用下拉加载更多。但是对于大量的列表渲染,特别像有实时数据...
本小节开始前,我们先答复下一个同学的问题。上一小节发布后,有小伙伴后台来信问到:‘小编你只讲了类组件中怎么使用 ref,那在函数式组件中怎么使用呢?’。确实我们...
上一小节我们了解了固定高度的滚动列表实现,因为是固定高度所以容器总高度和每个元素的 size、offset 很容易得到,这种场景也适合我们常见的大部分场景,例如...
上一小节我们处理了 setState 的批量更新机制,但是我们有两个遗漏点,一个是源码中的 setState 可以传入函数,同时 setState 可以传入第二...
我们知道 react 进行页面渲染或者刷新的时候,会从根节点到子节点全部执行一遍,即使子组件中没有状态的改变,也会执行。这就造成了性能不必要的浪费。之前我们了解...
在平时工作中的某些场景下,你可能想在整个组件树中传递数据,但却不想手动地通过 props 属性在每一层传递属性,contextAPI 应用而生。
楼主最近入职新单位了,恰好新单位使用的技术栈是 react,因为之前一直进行的是 vue2/vue3 和小程序开发,对于这些技术栈实现机制也有一些了解,最少面试...
我们上一节了了解了函数式组件和类组件的处理方式,本质就是处理基于 babel 处理后的 type 类型,最后还是要处理虚拟 dom。本小节我们学习下组件的更新机...
前面几节我们学习了解了 react 的渲染机制和生命周期,本节我们正式进入基本面试必考的核心地带 -- diff 算法,了解如何优化和复用 dom 操作的,还有...
我们在之前已经学习过 react 生命周期,但是在 16 版本中 will 类的生命周期进行了废除,虽然依然可以用,但是需要加上 UNSAFE 开头,表示是不安...
上一小节我们学习了 react 中类组件的优化方式,对于 hooks 为主流的函数式编程,react 也提供了优化方式 memo 方法,本小节我们来了解下它的用...
开源不易,感谢你的支持,❤ star me if you like concent ^_^
hel-micro,模块联邦sdk化,免构建、热更新、工具链无关的微模块方案 ,欢迎关注与了解
本文主题围绕concent的setup和react的五把钩子来展开,既然提到了setup就离不开composition api这个关键词,准确的说setup是由...
ReactsetState的执行是异步还是同步官方文档是这么说的setState()doesnotalwaysimmediatelyupdatethecomponent.Itmaybatchordefertheupdateuntillater.Thismakesreadingthis.staterightaftercallingsetState()apotentialpitfall.Instead,usecom