当前位置:主页 > 查看内容

鸿蒙HarmonyOS三方件开发指南-GifImage

发布时间:2021-05-18 00:00| 位朋友查看

简介:想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区 https://harmonyos.51cto.com 1. GifImage组件功能介绍 1.1. 功能介绍: GifImage组件是一个可以显示加载动态图片(gif格式)的三方组件。 1.2. 模拟器上运行效果: 2. GifImage使用方法……

想了解更多内容,请访问:

51CTO和华为官方战略合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

1. GifImage组件功能介绍

1.1. 功能介绍:

GifImage组件是一个可以显示加载动态图片(gif格式)的三方组件。

1.2. 模拟器上运行效果:

2. GifImage使用方法

2.1. 新建工程,增加组件Har包依赖

在应用模块中添加HAR,只需要将GifImage.har复制到entry\libs目录下即可(由于build.gradle中已经依赖的libs目录下的*.har,因此不需要在做修改)。

2.2. 设置gif的布局文件

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

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:orientation="vertical"
  7.  
  8.     <com.isoftstone.modulea.Gif 
  9.         ohos:id="$+id:testimg" 
  10.         ohos:height="match_content" 
  11.         ohos:width="match_content" 
  12.         ohos:image_src="$media:gif6" 
  13.         ohos:layout_alignment="horizontal_center" 
  14.     /> 
  15.  
  16.     <com.isoftstone.modulea.Gif 
  17.         ohos:id="$+id:testimg1" 
  18.         ohos:image_src="$media:coffe" 
  19.         ohos:height="match_content" 
  20.         ohos:width="match_content" 
  21.         ohos:layout_alignment="horizontal_center" 
  22.         /> 
  23.  
  24.     <com.isoftstone.modulea.Gif 
  25.         ohos:layout_alignment="horizontal_center" 
  26.         ohos:height="match_content" 
  27.         ohos:image_src="$media:deleting" 
  28.         ohos:width="match_content" 
  29.         ohos:id="$+id:text" 
  30.         /> 
  31.  
  32. </DirectionalLayout> 

2.3. MainAbilitySlice的UI加载代码

设置Gif gif= findComponentById(ResourceTable.Id_**)即可。

3. GifImage开发实现

3.1. 新建一个Module

新建一个Module,类型选择HarmonyOS Library,模块名为Gif,如图:

3.2. 新建Gif类

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

  1. public class Gif extends Image{ 
  2. public Gif(Context context) throws IOException, NotExistException, WrongTypeException { 
  3.     super(context); 
  4.     this.context=context; 
  5.     ResourceManager resourceManager =context.getResourceManager(); 
  6.     init(resourceManager); 
  7. public Gif(Context context, AttrSet attrSet) throws IOException, NotExistException, WrongTypeException { 
  8.     super(context, attrSet); 
  9.     this.context=context; 
  10.     String id  = attrSet.getAttr("image_src").get().getStringValue(); 
  11.     // $media:16777218 
  12.     Pattern pattern = Pattern.compile("[^0-9]"); 
  13.     Matcher matcher = pattern.matcher(id); 
  14.     String all = matcher.replaceAll(""); 
  15.     ids = Integer.valueOf(all); 
  16.     ResourceManager resourceManager = context.getResourceManager(); 
  17.     init(resourceManager); 

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

  1. // 动画 
  2.  
  3. private AnimatorValue animatorValue; 

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

  1. private void init()  { 
  2.   ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions(); 
  3.   ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions(); 
  4.   decodingOptions.allowPartialImage=true
  5.   sourceOptions.formatHint="image/gif"
  6.   RawFileEntry rawFileEntry = resourceManager.getRawFileEntry(resourceManager.getMediaPath(ids)); 
  7.   imageSource = ImageSource.create(rawFileEntry.openRawFile(),sourceOptions); 
  8.   if (imageSource != null) { 
  9.       i=0; 
  10.       while(imageSource.createPixelmap(i,decodingOptions)!=null) { 
  11.           pixelMapList.add(imageSource.createPixelmap(i, decodingOptions)); 
  12.           i++; 
  13.       } 

通过AnimatorValue启动动画:

  1. animatorValue = new AnimatorValue(); 
  2.    animatorValue.setCurveType(Animator.CurveType.LINEAR); 
  3.    animatorValue.setDelay(100); 
  4.    animatorValue.setLoopedCount(Animator.INFINITE); 
  5.    animatorValue.setDuration(2000); 
  6.    animatorValue.setValueUpdateListener(mAnimatorUpdateListener); 
  7.    animatorValue.start(); 

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

(转换为Int类型)

  1. // 动画侦听函数 
  2.  
  3. private final AnimatorValue.ValueUpdateListener mAnimatorUpdateListener 
  4.         = new AnimatorValue.ValueUpdateListener() { 
  5.     @Override 
  6.     public void onUpdate(AnimatorValue animatorValue, float v) { 
  7.       index++; 
  8. setPixelMap(pixelMapList.get((int)(v*pixelMapList.size()))); 
  9.         invalidate(); 
  10.     } 
  11. }; 

3.3. 编译HAR包

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

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

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

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

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

想了解更多内容,请访问:

51CTO和华为官方战略合作共建的鸿蒙技术社区

https://harmonyos.51cto.com


本文转载自网络,原文链接:https://harmonyos.51cto.com/#zz
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐