鸿蒙OS应用开发之——Java UI框架-常用组件Image

一 概述

  • 创建Image
  • 设置Image

二 创建Image

2.1 使用说明

Image组件需要结合布局文件使用

2.2 XML创建Image

<?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:height="200vp"
        ohos:width="200vp"
        ohos:layout_alignment="center"
        ohos:image_src="$media:dog"/>

</DirectionalLayout>

2.3 代码创建Image

 DependentLayout dependentLayout=new DependentLayout(getContext());
 Image image=new Image(getContext());
 image.setPixelMap(ResourceTable.Media_dog);
 dependentLayout.addComponent(image);
 super.setUIContent(dependentLayout);

三 设置Image

原图

3.1 设置透明度

布局文件
<Image
    ohos:id="$+id:image"
    ohos:width="match_content"
    ohos:height="match_content"
    ohos:layout_alignment="center"
    ohos:image_src="$media:plant"
    ohos:alpha="0.5"/>
效果图

3.2 设置缩放系数

布局文件
<Image
    ohos:id="$+id:image"
    ohos:width="match_content"
    ohos:height="match_content"
    ohos:layout_alignment="center"
    ohos:image_src="$media:plant"
    ohos:scale_x="0.5"
    ohos:scale_y="0.5"/>
效果图

3.3 设置缩放方式

缩放方式表
缩放方式
按比例将原图扩大(缩小)到Image的宽度,居中显示。zoom_center
按比例将原图扩大(缩小)到Image的宽度,显示在Image的上部分位置。zoom_start
按比例将原图扩大(缩小)到Image的宽度,显示在Image的下部分位置。zoom_end
不按比例将图片扩大/缩小到Image的大小显示stretch
保持原图的大小,显示在Image的中心。当原图的尺寸大于Image的尺寸时,超过部分裁剪处理center
按比例将原图缩小到Image的宽度,将图片的内容完整居中显示inside
按比例将原图扩大(缩小)到Image的宽度和高度中较大的值。如设置的高度值较大时,在垂直方向上完整显示,水平方向上超出Image宽度的部分裁剪处理clip_center
布局文件
<Image
    ohos:id="$+id:image"
    ohos:width="200vp"
    ohos:height="200vp"
    ohos:layout_alignment="center"
    ohos:image_src="$media:plant"
    ohos:scale_mode="zoom_center"/>
效果图

3.4 设置裁剪对齐模式

裁剪对齐
裁剪方式
左对齐裁剪。left
右对齐裁剪。right
顶部对齐裁剪。top
底部对齐裁剪。bottom
居中裁剪。center
布局文件
<Image
    ohos:id="$+id:image"
    ohos:width="200vp"
    ohos:height="200vp"
    ohos:layout_alignment="center"
    ohos:image_src="$media:plant"
    ohos:clip_alignment="center"/>
效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值