This article mainly introduces the detailed explanation of the use of ReactNative Image component. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.
It’s been quite interesting to learn ReactNative recently. During the learning process, I found that the content of articles written by some people on the Internet is outdated. This is mainly because the version of ReactNative is upgraded too quickly. If you now read an article written in 16 or even 15 years, and compare the knowledge points with official documents, you will be surprised. Therefore, I advise students who want to learn ReactNative to choose learning materials based on official documents and official demos, and other materials as supplements.
Image component
In ReactNative, Image is a component used to display images, which has the same effect as the ImageView control when developing Android. It can be used to display network images, static resources, temporary local images, and images on the local disk (such as photo albums), etc. Proper use of Image components can convey information to users more vividly and intuitively.
Image component loads static resources in the project
The static resources here refer to the images in the js part that are loaded, and are not resources under native applications of android and ios file, to load this kind of image resource, we introduce the image file through require('the path of the image file relative to this file directory') and set it to the source attribute of the Image component. As follows
<Image style={styles.image} // ./表示当前文件目录 ../ 父目录 source={require('./reactlogo.png')} />
One thing to note is that the path cannot be spliced with strings in the above require, otherwise a loading error will be reported.
Loading native image resources
The native resources mentioned here refer to the drawable in the res directory when we develop Android, or the mipmap directory. And the corresponding resource directory under ios. Loading this kind of image resources is a little different from loading resources in the project. Here we take android as an example. Load the file under drawable as follows
##
<Image source={{uri: 'launcher_icon'}} style={{width: 38, height: 38}} />);In addition to loading through the above method, You can specify the image width and height in the nativeImageSource in the following way
<Image source={nativeImageSource({ android: 'launcher_icon', width: 96, height: 96 })} />. If you set the width and height in the style attribute of the image component at the same time, the final width and height will be based on the style medium width. Whichever is higher. The image resources under drawable are loaded by default above. If you want to load the resources in mipmap, you can do as follows
<Image source={nativeImageSource({ android: 'mipmap/launcher_icon', width: 96, height: 96 })} />Through the above method, we can load the image. If The pictures newly added to the drawable need to be recompiled and run, otherwise they will not take effect.
Loading network images
<Image source={{uri: 'https://facebook.github.io/react/img/logo_og.png'}} style={{width: 38, height: 38}} />);One thing to note about loading network images is that you need to specify the width and height of the style, otherwise The image will not be displayed (the default width and height are not set to 0 anymore).
Commonly used attributes of the Image component
- width: Set the width of the image
- height: Set the height of the picture
- borderWidth: Set the border width
- borderColor: Set the border Color
- backgroundColor: Set the background color (this property is generally used when some pictures have transparent backgrounds)
- opacity: Opacity, The value is between 0 and 1, where 1 means opaque and 0 means transparent.
- tintColor: Colorize the picture. This attribute is more useful. For example, a black and white picture will often turn into another color picture when clicked. This attribute can be used at this time
<Image style={{flex: 1}} source={[ {uri: 'https://facebook.github.io/react/img/logo_small.png', width: 38, height: 38}, {uri: 'https://facebook.github.io/react/img/logo_small_2x.png', width: 76, height: 76}, uri: 'https://facebook.github.io/react/img/logo_og.png', width: 400, height: 400} ]} />resizeModeThis attribute is used to set the zoom mode of the image, the corresponding value is as follows
- cover: Maintain the aspect ratio of the image until the width and height are greater than or equal to the size of the container view (refer to the effect below)
- contain: Maintain the aspect ratio of the image Under the premise, scale the image until the width and height are less than or equal to the size of the container view
- stretch: Stretch the image without maintaining the aspect ratio until the width and height just fill the container
- center Center without stretching
- repeat: Repeat tile the image until it fills the container. The image will maintain its original size. (iOS)
Default Android does not support GIF and WebP formats. You need to add the corresponding dependencies as needed in the build.gradle file.
dependencies { // If your app supports Android versions before Ice Cream Sandwich (API level 14) compile 'com.facebook.fresco:animated-base-support:1.0.1' // For animated GIF support compile 'com.facebook.fresco:animated-gif:1.0.1' // For WebP support, including animated WebP compile 'com.facebook.fresco:animated-webp:1.0.1' compile 'com.facebook.fresco:webpsupport:1.0.1' // For WebP support, without animations compile 'com.facebook.fresco:webpsupport:1.0.1' }
If you use ProGuard while using GIF, you need to add the following rules to proguard-rules.pro
-keep class com.facebook.imagepipeline.animated.factory.AnimatedFactoryImpl { public AnimatedFactoryImpl(com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory, com.facebook.imagepipeline.core.ExecutorSupplier); }
ImageBackground
该组件是Image组件的扩展,它支持嵌套组件。如在图片上显示一个文本,则可以通过如下实现 实现效果图如下,一般的我们可以嵌套ActivityIndicator来提示用户图片正在加载,当加载完成隐藏此控件。 网络图片加载监听 对于网络图片的加载,ReactNative提供了一些属性用于图片不同加载时期的监听。 onLoadStart:图片开始加载时调用 onLoad:图片加载完成时调用,此时图片加载成功 onLoadEnd:加载结束后调用,与onLoad不同的是不论成功还是失败,此回调函数都会被执行。 使用方法如下 对于iOS,还提供了加载进度的回调函数onProgress 可以通过参数event.nativeEvent.loaded获取已经加载的大小,通过event.nativeEvent.total获取图片的总大小。 不仅如此,ReactNative还提供了预加载图片函数prefetch(url: string),它可以将图片下载到磁盘缓存 好了,今天就介绍到这里,文中若有错误的地方欢迎指正,再次感谢。文中一些示例源码,可前往GitHub在线预览,也可以下载项目学习其他组件。 <ImageBackground
style={{width: 100, height: 100, backgroundColor: 'transparent'}}
source={{uri: 'https://facebook.github.io/react/img/logo_og.png'}}
>
<Text style={styles.nestedText}>
React
</Text>
</ImageBackground>
<Image
source={{uri:'https://facebook.github.io/react/img/logo_og.png'}}
style={[styles.base, {overflow: 'visible'}]}
onLoadStart={() => console.log('onLoadStart')}
onLoad={(event) => console.log('onLoad') }
onLoadEnd={() => console.log('onLoadEnd')}
/>
<Image
style={styles.image}
onProgress={(event) => {
console.log('onProgress')
this.setState({
progress: Math.round(100 * event.nativeEvent.loaded / event.nativeEvent.total)
})}}/>
var prefetchTask = Image.prefetch('https://facebook.github.io/react/img/logo_og.png');
prefetchTask.then(() => {
//此处可设置状态,显示Image组件。此时组件会使用预加载的图片信息。而不用再次加载
console.log('加载图片成功')
}, error => {
console.log('加载图片失败')
})
The above is the detailed content of Detailed introduction to ReactNative Image component. For more information, please follow other related articles on the PHP Chinese website!

本文将介绍七种利用免费的BingImageCreator获得高质量输出的方法。BingImageCreator(现称为MicrosoftDesigner的ImageCreator)是一个出色的在线人工智能艺术生成器之一。它能根据用户的提示生成高度逼真的视觉效果。提示越具体、清晰和创意,生成的效果也会更出色。BingImageCreator在创建高质量图像方面取得了重大进展。它现在使用Dall-E3培训模式,显示出更高水平的细节和现实主义。然而,它能否始终如一地生成高清结果取决于几个因素,包括快速

小米手机image怎么删除?在小米手机中是可以删除image,但是多数的用户不知道image如何的删除,接下来就是小编为用户带来的小米手机image删除方法教程,感兴趣的用户快来一起看看吧!小米手机image怎么删除1、首先打开小米手机中的【相册】功能;2、然后勾选不需要的图片,点击右下角的【删除】按钮;3、之后点击最顶部的【相册】进入到专区,选择【回收站】;4、接着直接点击下图所示的【清空回收站】;5、最后直接点击【永久删除】即可完成。

LINUX是一种开源的操作系统,它的灵活性和可定制性使得它成为了许多开发者和系统管理员的首选,在LINUX系统中,图像处理是一个非常重要的任务,而Imagemagick和Image是两个非常流行的图像处理工具,本文将为您介绍如何在Centos系统中安装Imagemagick和Image,并提供详细的安装教程。Imagemagic安装Centos教程Imagemagick是一个功能强大的图像处理工具集,它可以在命令行下执行各种图像操作,以下是在Centos系统上安装Imagemagick的步骤:1

cookies是一种常见的网络技术,用于存储用户在网站上的个人偏好和行为信息。在今天的数字化时代,几乎所有的网站都使用cookies来提供个性化和更好的用户体验。本文将详细介绍cookies的使用说明,帮助用户更好地理解和掌握这一技术。首先,我们来了解一下cookies的基本概念。Cookies是网站在用户浏览器上存储的小型文本文件,包含有关用户访问网站的一

任务管理器是大家平时应用电脑上时常见的一个程序流程,能够见到电脑上全部常见运作的程序流程.有朋友想打开win7任务管理器,不清楚win7任务管理器快捷键是什么.下边就教下大伙儿迅速打开win7任务管理器的方式.1.按住键盘上的Ctrl+Alt+Delete键以后,再挑选运行任务管理器。2.另一个快捷键也可以打开任务管理器,按住Esc+shift+Ctrl,就可以打开任务管理器了。3.按Win+R键打开运行窗口在页面中键入C:\Windows\system32\taskmgr.exe,按回车,也可

怎么自己重装系统win7?系统使用时间长了也容易出现故障,对此许多win7用户都喜欢自己来重装系统,在选择用u盘安装的同时,我们也可以选择在电脑正常运行的情况下载重装系统,可是自己如何重装win7系统呢?接下来小编就给大家讲讲win7系统重装的详细步骤吧。感兴趣的朋友们快来看看吧!1、第一步我们打开浏览器搜索下载装机吧一键重装系统软件,选择win7系统。2、等待软件下载完成后,点击重启电脑即可。3、进入启动页面,选择第二选项zhuangjibape进入。4、进入pe系统后,等待装机吧在线重装系统

win10系统是目前操作系统的主流,所以在购买电脑的时候一般都是预装win10系统,但是有的朋友比较喜欢使用win7系统,但是又不知道win7系统怎么安装,那么简单详细的win7安装方法是什么呢?所本期就win7系统怎么安装的问题,给大家带来win7系统安装图解。简单详细的win7安装方法如下:1、搜索咔咔装机官网,进入后下载咔咔装机工具,备份系统C盘重要文件,打开咔咔装机点击在线重装系统。2、接下来就可以选择微软原版win7系统,点击下方的安装此系统。3、选择需要安装的第三方软件,点击下一步。

imagefilledrectangle()函数绘制一个填充矩形。语法imagefilledrectangle($img,$x1,$y1,$x2,$y2,$color)参数image 使用imagecreatetruecolor()创建一个空白图像。x1点1的x坐标。y1 点1的y坐标。x2 点2的x坐标。y2 点2的y坐标。color 填充颜色。返回值imagefilledrectangle()函数成功返


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
