关于harmonyos:34HarmonyOS鸿蒙开发组件Image

4次阅读

共计 11067 个字符,预计需要花费 28 分钟才能阅读完成。

3.4【HarmonyOS 鸿蒙开发】组件 Image

作者:韩茹

公司:程序咖(北京)科技有限公司

鸿蒙巴士专栏作家

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”
ohos:image_src=”$color:black
ohos:image_src=”$media:warning
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

<Image
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:layout_alignment="center"
        ohos:image_src="$media:chengxuka"/>

获取输入框的内容:在代码中创立 Image

                // 创立 Image
        Image image = new Image(getContext());
        // 设置要显示的图片
        image.setPixelMap(ResourceTable.Media_chengxuka);

这里留神,应用 Image 不要导错包,应用上面的这条导入:

import ohos.agp.components.Image;

成果:

<img src=”https://img.chengxuka.com/WX20210608-135037@2x.png/mark” alt=”WX20210608-135037@2x” style=”zoom:70%;” />

三、设置 Image

    1. 设置透明度
  <Image
          ohos:height="match_content"
          ohos:width="match_content"
          ohos:layout_alignment="center"
          ohos:top_margin="20vp"
          ohos:image_src="$media:chengxuka"
          ohos:alpha="0.5"
          />

设置透明度为 0.5 的成果和没有设置透明度的比照:

    1. 设置缩放系数
  <Image
          ohos:height="match_content"
          ohos:width="match_content"
          ohos:layout_alignment="center"
          ohos:top_margin="20vp"
          ohos:image_src="$media:chengxuka"
          ohos:scale_x="0.5"
          ohos:scale_y="0.5"
          />

成果

    1. 设置缩放形式

    当图片尺寸与 Image 尺寸不同时,能够依据不同的缩放形式来对图片进行缩放,如设置 Image 的宽高为 150vp

  <?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">
  
  <!--    scale_mode,图片尺寸与 Image 尺寸不同时,能够依据不同的缩放形式来对图片进行缩放
              center,示意不缩放,按 Image 大小显示原图两头局部。zoom_center,示意原图依照比例缩放到与 Image 最窄边统一,并居中显示。stretch,示意将原图缩放到与 Image 大小统一。inside,示意将原图按比例缩放到与 Image 雷同或更小的尺寸,并居中显示。clip_center,示意将原图按比例缩放到与 Image 雷同或更大的尺寸,并居中显示。-->
  <!--    center,示意不缩放,按 Image 大小显示原图两头局部。-->
      <Image
          ohos:height="150vp"
          ohos:width="150vp"
          ohos:image_src="$media:chengxuka"
          ohos:scale_mode="center"
          />
  <!--    zoom_center,示意原图依照比例缩放到与 Image 最窄边统一,并居中显示。-->
      <Image
          ohos:height="150vp"
          ohos:width="150vp"
          ohos:image_src="$media:chengxuka"
          ohos:scale_mode="zoom_center"
          ohos:background_element="$graphic:background_image"
          />
  
  <!--    stretch,示意将原图缩放到与 Image 大小统一。图片可能会变形 -->
      <Image
          ohos:height="150vp"
          ohos:width="150vp"
          ohos:image_src="$media:chengxuka"
          ohos:scale_mode="stretch"
          ohos:background_element="$graphic:background_image"
          />
  <!--    inside,示意将原图按比例缩放到与 Image 雷同或更小的尺寸,并居中显示。-->
      <Image
          ohos:height="150vp"
          ohos:width="150vp"
          ohos:image_src="$media:chengxuka"
          ohos:scale_mode="inside"
          ohos:background_element="$graphic:background_image"
          />
  <!--    clip_center,示意将原图按比例缩放到与 Image 雷同或更大的尺寸,并居中显示。-->
      <Image
          ohos:height="150vp"
          ohos:width="150vp"
          ohos:image_src="$media:chengxuka"
          ohos:scale_mode="clip_center"
          ohos:background_element="$graphic:background_image"
          />
  
  </DirectionalLayout>

这里咱们为了更好的看到成果,在图片上加了边框,设置背景资源如下 background_image.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:shape="rectangle">

    <stroke
        ohos:color="#FF0000"
        ohos:width="6"/>
</shape>

成果:

<img src=”https://img.chengxuka.com/WX20210608-143148@2x.png/mark” alt=”WX20210608-143148@2x” style=”zoom:50%;” />

    1. 设置裁剪对齐模式

    当 Image 尺寸小于图片尺寸时,能够对图片进行裁剪,仍以 Image 的宽高为 150vp 为例,小于图片尺寸。

  <?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">
  
  <!--    clip_alignment,图像裁剪对齐形式
              left    示意按左对齐裁剪。right   示意按右对齐裁剪。top     示意按顶部对齐裁剪。bottom  示意按底部对齐裁剪。center  示意按居中对齐裁剪。-->
  <!--    left    示意按左对齐裁剪。-->
      <Image
          ohos:height="150vp"
          ohos:width="150vp"
          ohos:image_src="$media:chengxuka"
          ohos:clip_alignment="left"
          />
  <!--    right   示意按右对齐裁剪。-->
      <Image
          ohos:height="150vp"
          ohos:width="150vp"
          ohos:image_src="$media:chengxuka"
          ohos:clip_alignment="right"
          />
  
  <!--    top     示意按顶部对齐裁剪。-->
      <Image
          ohos:height="150vp"
          ohos:width="150vp"
          ohos:image_src="$media:chengxuka"
          ohos:clip_alignment="top"
          />
  <!--    bottom  示意按底部对齐裁剪。-->
      <Image
          ohos:height="150vp"
          ohos:width="150vp"
          ohos:image_src="$media:chengxuka"
          ohos:clip_alignment="bottom"
          />
  <!--    center  示意按居中对齐裁剪。-->
      <Image
          ohos:height="150vp"
          ohos:width="150vp"
          ohos:image_src="$media:chengxuka"
          ohos:clip_alignment="center"
          />
  
  </DirectionalLayout>

成果如下:

<img src=”https://img.chengxuka.com/WX20210608-143453@2x.png/mark” alt=”WX20210608-143453@2x” style=”zoom:50%;” />

四、写个例子 - 轮播图

咱们做一个轮播图:设置两个按钮,上一张和下一张,点击下一张就显示下一张图片,点击上一张,就显示上一张图片。

<img src=”https://img.chengxuka.com/imageyunxing1.gif” alt=”imageyunxing1″ style=”zoom:50%;” />

 思路剖析:咱们先将这些图片资源存储在一个数组里,而后设置一个下标变量 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());
    }
}

而后运行程序即可。

更多内容:

1、社区:鸿蒙巴士 https://www.harmonybus.net/

2、公众号:HarmonyBus

3、技术交换 QQ 群:714518656

4、视频课:https://www.chengxuka.com

正文完
 0