3.4「HarmonyOS鸿蒙开发」组件Image
yuyutoo 2024-10-21 12:07 2 浏览 0 评论
作者:韩茹
公司:程序咖(北京)科技有限公司
鸿蒙巴士专栏作家
Image是用来显示图片的组件。
一、支持的XML属性
Image的共有XML属性继承自:Component
Image的自有XML属性见下表:
属性名称 | 中文描述 | 取值 | 取值说明 | 使用案例 |
clip_alignment | 图像裁剪对齐方式 | left | 表示按左对齐裁剪。 | ohos:clip_alignment="left" |
right | 表示按右对齐裁剪。 | ohos:clip_alignment="right" | ||
top | 表示按顶部对齐裁剪。 | ohos:clip_alignment="top" | ||
bottom | 表示按底部对齐裁剪。 | ohos:clip_alignment="bottom" | ||
center | 表示按居中对齐裁剪。 | ohos:clip_alignment="center" | ||
image_src | 图像 | Element类型 | 可直接配置色值,也可引用color资源或引用media/graphic下的图片资源。 | ohos:image_src="#FFFFFFFF"<br />ohos:image_src="$color:black"<br />ohos:image_src="$media:warning"<br />ohos:image_src="$graphic:graphic_src" |
scale_mode | 图像缩放类型 | zoom_center | 表示原图按照比例缩放到与Image最窄边一致,并居中显示。 | ohos:scale_mode="center" |
zoom_start | 表示原图按照比例缩放到与Image最窄边一致,并靠起始端显示。 | |||
zoom_end | 表示原图按照比例缩放到与Image最窄边一致,并靠结束端显示。 | |||
stretch | 表示将原图缩放到与Image大小一致。 | |||
center | 表示不缩放,按Image大小显示原图中间部分。 | |||
inside | 表示将原图按比例缩放到与Image相同或更小的尺寸,并居中显示。 | |||
clip_center | 表示将原图按比例缩放到与Image相同或更大的尺寸,并居中显示。 |
二、创建Image
在“Project”窗口,打开“entry > src > main > resources > base > media”,添加一个图片至media文件夹下,以“chengxuka.jpg”为例。
既可以在XML中创建Image,也可以在代码中创建Image,两种方式如下:
在XML中创建Image
<Imagebr ohos:height="match_content"br ohos:width="match_content"br ohos:layout_alignment="center"br ohos:image_src="$media:chengxuka"/>
获取输入框的内容:在代码中创建Image
//创建Imagebr Image image = new Image(getContext());br //设置要显示的图片br image.setPixelMap(ResourceTable.Media_chengxuka);
这里注意,使用Image不要导错包,使用下面的这条导入:
import ohos.agp.components.Image;
效果:
三、设置Image
- 设置透明度
<Imagebr ohos:height="match_content"br ohos:width="match_content"br ohos:layout_alignment="center"br ohos:top_margin="20vp"br ohos:image_src="$media:chengxuka"br ohos:alpha="0.5"br />
设置透明度为0.5的效果和没有设置透明度的对比:
- 设置缩放系数
<Imagebr ohos:height="match_content"br ohos:width="match_content"br ohos:layout_alignment="center"br ohos:top_margin="20vp"br ohos:image_src="$media:chengxuka"br ohos:scale_x="0.5"br ohos:scale_y="0.5"br />
效果
- 设置缩放方式当图片尺寸与Image尺寸不同时,可以根据不同的缩放方式来对图片进行缩放,如设置Image的宽高为150vp
<?xml version="1.0" encoding="utf-8"?>br <DirectionalLayoutbr xmlns:ohos="http://schemas.huawei.com/res/ohos"br ohos:height="match_parent"br ohos:width="match_parent"br ohos:orientation="vertical">br br <!-- scale_mode,图片尺寸与Image尺寸不同时,可以根据不同的缩放方式来对图片进行缩放br center,表示不缩放,按Image大小显示原图中间部分。br zoom_center,表示原图按照比例缩放到与Image最窄边一致,并居中显示。br stretch,表示将原图缩放到与Image大小一致。br inside,表示将原图按比例缩放到与Image相同或更小的尺寸,并居中显示。br clip_center,表示将原图按比例缩放到与Image相同或更大的尺寸,并居中显示。br -->br <!-- center,表示不缩放,按Image大小显示原图中间部分。-->br <Imagebr ohos:height="150vp"br ohos:width="150vp"br ohos:image_src="$media:chengxuka"br ohos:scale_mode="center"br />br <!-- zoom_center,表示原图按照比例缩放到与Image最窄边一致,并居中显示。-->br <Imagebr ohos:height="150vp"br ohos:width="150vp"br ohos:image_src="$media:chengxuka"br ohos:scale_mode="zoom_center"br ohos:background_element="$graphic:background_image"br />br br <!-- stretch,表示将原图缩放到与Image大小一致。图片可能会变形-->br <Imagebr ohos:height="150vp"br ohos:width="150vp"br ohos:image_src="$media:chengxuka"br ohos:scale_mode="stretch"br ohos:background_element="$graphic:background_image"br />br <!-- inside,表示将原图按比例缩放到与Image相同或更小的尺寸,并居中显示。-->br <Imagebr ohos:height="150vp"br ohos:width="150vp"br ohos:image_src="$media:chengxuka"br ohos:scale_mode="inside"br ohos:background_element="$graphic:background_image"br />br <!-- clip_center,表示将原图按比例缩放到与Image相同或更大的尺寸,并居中显示。-->br <Imagebr ohos:height="150vp"br ohos:width="150vp"br ohos:image_src="$media:chengxuka"br ohos:scale_mode="clip_center"br ohos:background_element="$graphic:background_image"br />br br </DirectionalLayout>
这里我们为了更好的看到效果,在图片上加了边框,设置背景资源如下background_image.xml:
<?xml version="1.0" encoding="UTF-8" ?>br<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"br ohos:shape="rectangle">brbr <strokebr ohos:color="#FF0000"br ohos:width="6"/>br</shape>
效果:
- 设置裁剪对齐模式当Image尺寸小于图片尺寸时,可以对图片进行裁剪,仍以Image的宽高为150vp为例,小于图片尺寸。
<?xml version="1.0" encoding="utf-8"?>br <DirectionalLayoutbr xmlns:ohos="http://schemas.huawei.com/res/ohos"br ohos:height="match_parent"br ohos:width="match_parent"br ohos:orientation="vertical">br br <!-- clip_alignment,图像裁剪对齐方式br left 表示按左对齐裁剪。br right 表示按右对齐裁剪。br top 表示按顶部对齐裁剪。br bottom 表示按底部对齐裁剪。br center 表示按居中对齐裁剪。br -->br <!-- left 表示按左对齐裁剪。-->br <Imagebr ohos:height="150vp"br ohos:width="150vp"br ohos:image_src="$media:chengxuka"br ohos:clip_alignment="left"br />br <!-- right 表示按右对齐裁剪。-->br <Imagebr ohos:height="150vp"br ohos:width="150vp"br ohos:image_src="$media:chengxuka"br ohos:clip_alignment="right"br />br br <!-- top 表示按顶部对齐裁剪。-->br <Imagebr ohos:height="150vp"br ohos:width="150vp"br ohos:image_src="$media:chengxuka"br ohos:clip_alignment="top"br />br <!-- bottom 表示按底部对齐裁剪。-->br <Imagebr ohos:height="150vp"br ohos:width="150vp"br ohos:image_src="$media:chengxuka"br ohos:clip_alignment="bottom"br />br <!-- center 表示按居中对齐裁剪。-->br <Imagebr ohos:height="150vp"br ohos:width="150vp"br ohos:image_src="$media:chengxuka"br ohos:clip_alignment="center"br />br br </DirectionalLayout>
效果如下:
四、写个例子-轮播图
我们做一个轮播图:设置两个按钮,上一张和下一张,点击下一张就显示下一张图片,点击上一张,就显示上一张图片。
思路分析:
我们先将这些图片资源存储在一个数组里,然后设置一个下标变量index,用于表示当前Image组件要显示的图片,当点击上一张按钮的时候,我们将index减1。当点击下一张按钮的时候,我们将idnex加1。这里要注意index的边界值问题。
1、首先准备好一些图片,将它们粘贴到media目录下。
2、首先在layout目录下,新建一个布局文件:image_carousel.xml
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Image
ohos:id="$+id:image1"
ohos:height="0"
ohos:weight="1"
ohos:width="300vp"
ohos:layout_alignment="center"
ohos:top_margin="30vp"
ohos:bottom_margin="30vp"
ohos:scale_mode="stretch"
ohos:image_src="$media:img001"
ohos:background_element="#33FF0000"/>
<DependentLayout
ohos:height="match_content"
ohos:layout_alignment="center"
ohos:bottom_margin="30vp"
ohos:width="300vp">
<Button
ohos:id="$+id:btn1"
ohos:height="match_content"
ohos:width="match_content"
ohos:align_parent_left="true"
ohos:text_size="18fp"
ohos:background_element="$graphic:background_button"
ohos:text="上一张"
ohos:left_padding="14vp"
ohos:right_padding="14vp"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
/>
<Button
ohos:id="$+id:btn2"
ohos:height="match_content"
ohos:width="match_content"
ohos:text_size="18fp"
ohos:align_parent_right="true"
ohos:text="下一张"
ohos:background_element="$graphic:background_button"
ohos:left_padding="14vp"
ohos:right_padding="14vp"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
/>
</DependentLayout>
</DirectionalLayout>
为了让两个按钮更加美观,我们设置一下按钮的背景样式,
然后我们在graphic目录下创建所需要的xml文件,
background_button.xml代码示例:
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:shape="rectangle">
<solid
ohos:color="#3387CEFA"/>
<corners
ohos:radius="18vp"/>
<stroke
ohos:color="#9987CEFA"
ohos:width="6"/>
</shape>
3、Java中的代码
首先我们修改MainAbilitySlice中onStart()方法里,要加载显示的布局文件:
//要加载显示的布局文件
super.setUIContent(ResourceTable.Layout_image_carousel);
然后先设置图片的资源数组和index下标,这里要将它们声明为成员变量:
//1.将图片的id,存入数组中,int
int[] images = {ResourceTable.Media_img001,ResourceTable.Media_img002,ResourceTable.Media_img003,ResourceTable.Media_img004,ResourceTable.Media_img005,ResourceTable.Media_img006,ResourceTable.Media_img007,ResourceTable.Media_img008,ResourceTable.Media_img009};
//2.定义下标
int index = 0;
最后获取图片控件,按钮控件,为按钮添加点击事件:
//实现图片轮播
/*
题目思路:点击上一张,切换到上一个图片,点击下一张,切换到下一个图片。
将图片存放在数组中,统一操作index数组的下标完成。
*/
//3.获取控件对象
Image image = (Image) findComponentById(ResourceTable.Id_image1);
Button btn1 = (Button) findComponentById(ResourceTable.Id_btn1);
Button btn2 = (Button) findComponentById(ResourceTable.Id_btn2);
//4.为按钮添加点击事件
btn1.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
//上一张:点击按钮,index-1
index--;
if(index<0){
index = images.length-1;
}
if(index>images.length-1){
index= 0;
}
image.setPixelMap(images[index]);
}
});
btn2.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
//下一张:点击按钮,index+1
index++;
if(index<0){
index = images.length-1;
}
if(index>images.length-1){
index= 0;
}
image.setPixelMap(images[index]);
}
});
五、再写个例子
我们再来一个例子,和刚才差不多,点击按钮,来更改图片的透明度。
1、首先准备好一张素材图片,复制到media目录下。
2、在layout目录下,新建一个布局文件:image_alpha.xml,示例代码如下:
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<Text
ohos:height="match_content"
ohos:width="match_content"
ohos:text="增减透明度"
ohos:text_size="18fp"
ohos:text_alignment="center"
/>
<Image
ohos:id="$+id:image1"
ohos:height="400vp"
ohos:width="300vp"
ohos:layout_alignment="center"
ohos:top_margin="30vp"
ohos:bottom_margin="30vp"
ohos:scale_mode="stretch"
ohos:image_src="$media:aa"
ohos:background_element="#33FF0000"/>
<DependentLayout
ohos:height="match_content"
ohos:layout_alignment="center"
ohos:bottom_margin="30vp"
ohos:width="300vp">
<Button
ohos:id="$+id:btn1"
ohos:height="match_content"
ohos:width="match_content"
ohos:align_parent_left="true"
ohos:text_size="18fp"
ohos:background_element="$graphic:background_button"
ohos:text="+"
ohos:left_padding="14vp"
ohos:right_padding="14vp"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
/>
<Button
ohos:id="$+id:btn2"
ohos:height="match_content"
ohos:width="match_content"
ohos:text_size="18fp"
ohos:align_parent_right="true"
ohos:text="-"
ohos:background_element="$graphic:background_button"
ohos:left_padding="14vp"
ohos:right_padding="14vp"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
/>
</DependentLayout>
</DirectionalLayout>
这里我们为了页面好看,将按钮设置为圆形,在graphic目录下,添加按钮的背景文件,background_circle_button.xml,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:shape="oval">
<solid
ohos:color="#3387CEFA"/>
<corners
ohos:radius="18vp"/>
<stroke
ohos:color="#9987CEFA"
ohos:width="6"/>
</shape>
3、如果你在刚刚轮播图例子的这个项目里来实现,那么我们在slice目录下,新建一个java文件,SecondAbilitySlice.java,示例代码如下:
package com.example.hanruimage.slice;
import com.example.hanruimage.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
public class SecondAbilitySlice extends AbilitySlice{
float alpha = 1.0f;//取值范围0-1之间。
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_image_alpha);
//获取控件
Image image = (Image) findComponentById(ResourceTable.Id_image1);
Button btn1 = (Button) findComponentById(ResourceTable.Id_btn1);
Button btn2 = (Button) findComponentById(ResourceTable.Id_btn2);
//为按钮添加点击事件
btn1.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
alpha += 0.25;
if(alpha>=1){
alpha = 1;
}
image.setAlpha(alpha);
}
});
btn2.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
alpha -= 0.25;
if(alpha<=0){
alpha = 0;
}
image.setAlpha(alpha);
}
});
}
}
那么我们就要修改程序的入口,程序的默认入口是显示MainAbilitySlice,修改MainAbility文件:
package com.example.hanruimage;
import com.example.hanruimage.slice.MainAbilitySlice;
import com.example.hanruimage.slice.SecondAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
public class MainAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
// super.setMainRoute(MainAbilitySlice.class.getName());
super.setMainRoute(SecondAbilitySlice.class.getName());
}
}
然后运行程序即可。
相关推荐
- 如何在HTML中使用JavaScript:从基础到高级的全面指南!
-
“这里是云端源想IT,帮你...
- 推荐9个Github上热门的CSS开源框架
-
大家好,我是Echa。...
- 硬核!知网首篇被引过万的论文讲了啥?作者什么来头?
-
整理|袁小华近日,知网首篇被引量破万的中文论文及其作者备受关注。知网中心网站数据显示,截至2021年7月23日,由华南师范大学教授温忠麟等人发表在《心理学报》2004年05期上的学术论文“中介效应检验...
- 为什么我推荐使用JSX开发Vue3_为什么用vue不用jquery
-
在很长的一段时间中,Vue官方都以简单上手作为其推广的重点。这确实给Vue带来了非常大的用户量,尤其是最追求需求开发效率,往往不那么在意工程代码质量的国内中小企业中,Vue占据的份额极速增长...
-
- 【干货】一文详解html和css,前端开发需要哪些技术?
-
网站开发简介...
-
2025-02-20 18:34 yuyutoo
- 分享几个css实用技巧_cssli
-
本篇将介绍几个css小技巧,目录如下:自定义引用标签的符号重置所有标签样式...
- 如何在浏览器中运行 .NET_怎么用浏览器运行代码
-
概述:...
- 前端-干货分享:更牛逼的CSS管理方法-层(CSS Layers)
-
使用CSS最困难的部分之一是处理CSS的权重值,它可以决定到底哪条规则会最终被应用,尤其是如果你想在Bootstrap这样的框架中覆盖其已有样式,更加显得麻烦。不过随着CSS层的引入,这一...
-
- HTML 基础标签库_html标签基本结构
-
HTML标题HTML标题(Heading)是通过-...
-
2025-02-20 18:34 yuyutoo
- 前端css面试20道常见考题_高级前端css面试题
-
1.请解释一下CSS3的flexbox(弹性盒布局模型),以及适用场景?display:flex;在父元素设置,子元素受弹性盒影响,默认排成一行,如果超出一行,按比例压缩flex:1;子元素设置...
- vue引入外部js文件并使用_vue3 引入外部js
-
要在Vue中引入外部的JavaScript文件,可以使用以下几种方法:1.使用``标签引入外部的JavaScript文件。在Vue的HTML模板中,可以直接使用``标签来引入外部的JavaScrip...
- 网页设计得懂css的规范_html+css网页设计
-
在初级的前端工作人员,刚入职的时候,可能在学习前端技术,写代码不是否那么的规范,而在工作中,命名的规范的尤为重要,它直接与你的代码质量挂钩。网上也受很多,但比较杂乱,在加上每年的命名都会发生一变化。...
- Google在Chrome中引入HTML 5.1标记
-
虽然负责制定Web标准的WorldWideWebConsortium(W3C)尚未宣布HTML5正式推荐规格,而Google已经迁移到了HTML5.1。即将发布的Chrome38将引入H...
- HTML DOM 引用( ) 对象_html中如何引用js
-
引用对象引用对象定义了一个同内联元素的HTML引用。标签定义短的引用。元素经常在引用的内容周围添加引号。HTML文档中的每一个标签,都会创建一个引用对象。...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- mybatis plus (70)
- scheduledtask (71)
- css滚动条 (60)
- java学生成绩管理系统 (59)
- 结构体数组 (69)
- databasemetadata (64)
- javastatic (68)
- jsp实用教程 (53)
- fontawesome (57)
- widget开发 (57)
- vb net教程 (62)
- hibernate 教程 (63)
- case语句 (57)
- svn连接 (74)
- directoryindex (69)
- session timeout (58)
- textbox换行 (67)
- extension_dir (64)
- linearlayout (58)
- vba高级教程 (75)
- iframe用法 (58)
- sqlparameter (59)
- trim函数 (59)
- flex布局 (63)
- contextloaderlistener (56)