HarmonyOS三方件开发指南(10)——GifImage

Stella981
• 阅读 469

目录:

1. GifImage组件功能介绍

2. GifImage使用方法

3. GifImage开发实现

4.《HarmonyOS三方件开发指南》系列文章合集

1. GifImage组件功能介绍
1.1. 功能介绍:
GifImage组件是一个可以显示加载动态图片(gif格式)的三方组件。

1.2. 模拟器上运行效果:
 HarmonyOS三方件开发指南(10)——GifImage

2. GifImage使用方法
2.1. 新建工程,增加组件Har包依赖
在应用模块中添加HAR,只需要将GifImage.har复制到entry\libs目录下即可(由于build.gradle中已经依赖的libs目录下的*.har,因此不需要在做修改)。

2.2. 设置gif的布局文件
修改主页面的布局文件ability_main.xml,将Image更新为Gif并将图片源设为gif格式

<?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">

    <com.isoftstone.modulea.Gif
        ohos:id="$+id:testimg"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:image_src="$media:gif6"
        ohos:layout_alignment="horizontal_center"
    />

    <com.isoftstone.modulea.Gif
        ohos:id="$+id:testimg1"
        ohos:image_src="$media:coffe"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:layout_alignment="horizontal_center"
        />

    <com.isoftstone.modulea.Gif
        ohos:layout_alignment="horizontal_center"
        ohos:height="match_content"
        ohos:image_src="$media:deleting"
        ohos:width="match_content"
        ohos:id="$+id:text"
        />

</DirectionalLayout>

2.3.  MainAbilitySlice的UI加载代码
设置Gif gif= findComponentById(ResourceTable.Id_**)即可。
3. GifImage开发实现
3.1. 新建一个Module
新建一个Module,类型选择HarmonyOS Library,模块名为Gif,如图

HarmonyOS三方件开发指南(10)——GifImage

3.2. 新建Gif类
新建一个Gif类,继承自Image类,设置 ResourceManager并通过attrSet.getAttr("image_src").get().getStringValue() 获取图片路径,代码如下:

public class Gif extends Image{
public Gif(Context context) throws IOException, NotExistException, WrongTypeException {
    super(context);
    this.context=context;
    ResourceManager resourceManager =context.getResourceManager();
    init(resourceManager);
}
public Gif(Context context, AttrSet attrSet) throws IOException, NotExistException, WrongTypeException {
    super(context, attrSet);
    this.context=context;
    String id  = attrSet.getAttr("image_src").get().getStringValue();
    // $media:16777218
    Pattern pattern = Pattern.compile("[^0-9]");
    Matcher matcher = pattern.matcher(id);
    String all = matcher.replaceAll("");
    ids = Integer.valueOf(all);
    ResourceManager resourceManager = context.getResourceManager();
    init(resourceManager);
}
}

为了实现动画,需要定义一个AnimatorValue,并设置动画侦听回调函数,代码如下:

// 动画

private AnimatorValue animatorValue;

创建ImageSource和 RawFileEntry读取文件并通过while循环获得图片的每一帧:

private void init()  {
  ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
  ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
  decodingOptions.allowPartialImage=true;
  sourceOptions.formatHint="image/gif";
  RawFileEntry rawFileEntry = resourceManager.getRawFileEntry(resourceManager.getMediaPath(ids));
  imageSource = ImageSource.create(rawFileEntry.openRawFile(),sourceOptions);
  if (imageSource != null) {
      i=0;
      while(imageSource.createPixelmap(i,decodingOptions)!=null) {
          pixelMapList.add(imageSource.createPixelmap(i, decodingOptions));
          i++;
      }
}

 通过AnimatorValue启动动画

 animatorValue = new AnimatorValue();
    animatorValue.setCurveType(Animator.CurveType.LINEAR);
    animatorValue.setDelay(100);
    animatorValue.setLoopedCount(Animator.INFINITE);
    animatorValue.setDuration(2000);
    animatorValue.setValueUpdateListener(mAnimatorUpdateListener);
    animatorValue.start();

为实现图片切换效果,在动画监听回调函数内设置setPixelMap,进度为v*pixelMapList.size()

(转换为Int类型)

// 动画侦听函数

private final AnimatorValue.ValueUpdateListener mAnimatorUpdateListener
        = new AnimatorValue.ValueUpdateListener() {
    @Override
    public void onUpdate(AnimatorValue animatorValue, float v) {
      index++;
setPixelMap(pixelMapList.get((int)(v*pixelMapList.size())));
        invalidate();
    }
};

3.3. 编译HAR包
利用Gradle可以将HarmonyOS Library库模块构建为HAR包,构建HAR包的方法如下:

在Gradle构建任务中,双击PackageDebugHar或PackageReleaseHar任务,构建Debug类型或Release类型的HAR。

待构建任务完成后,可以在GifImage> bulid > outputs > har目录中,获取生成的HAR包。

HarmonyOS三方件开发指南(10)——GifImage

项目源代码地址:https://github.com/isoftstone-dev/gif\_HarmonyOS

欢迎交流:HWIS-HOS@isoftstone.com

作者:软通田可辉

想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
11个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
4年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593
Wesley13 Wesley13
3年前
MYSQL主从同步故障解决(主键重复)
MYSQL主从同步故障解决(主键重复)转载2010年04月05日18:52:00标签:mysql(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fso.csdn.net%2Fso%2Fsearch%2Fs.do%3Fq%
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
美凌格栋栋酱 美凌格栋栋酱
4个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(