自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

鴻蒙HarmonyOS三方件開發(fā)指南-GifImage

開發(fā) OpenHarmony
文章由鴻蒙社區(qū)產(chǎn)出,想要了解更多內(nèi)容請(qǐng)前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術(shù)社區(qū)https://harmonyos.51cto.com

[[383409]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

1. GifImage組件功能介紹

1.1. 功能介紹:

GifImage組件是一個(gè)可以顯示加載動(dòng)態(tài)圖片(gif格式)的三方組件。

1.2. 模擬器上運(yùn)行效果:

2. GifImage使用方法

2.1. 新建工程,增加組件Har包依賴

在應(yīng)用模塊中添加HAR,只需要將GifImage.har復(fù)制到entry\libs目錄下即可(由于build.gradle中已經(jīng)依賴的libs目錄下的*.har,因此不需要在做修改)。

2.2. 設(shè)置gif的布局文件

修改主頁面的布局文件ability_main.xml,將Image更新為Gif并將圖片源設(shè)為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加載代碼

設(shè)置Gif gif= findComponentById(ResourceTable.Id_**)即可。

3. GifImage開發(fā)實(shí)現(xiàn)

3.1. 新建一個(gè)Module

新建一個(gè)Module,類型選擇HarmonyOS Library,模塊名為Gif,如圖:

3.2. 新建Gif類

新建一個(gè)Gif類,繼承自Image類,設(shè)置 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); 

為了實(shí)現(xiàn)動(dòng)畫,需要定義一個(gè)AnimatorValue,并設(shè)置動(dòng)畫偵聽回調(diào)函數(shù),代碼如下:

  1. // 動(dòng)畫 
  2.  
  3. private AnimatorValue animatorValue; 

創(chuàng)建ImageSource和 RawFileEntry讀取文件并通過while循環(huán)獲得圖片的每一幀:

  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啟動(dòng)動(dòng)畫:

  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(); 

為實(shí)現(xiàn)圖片切換效果,在動(dòng)畫監(jiān)聽回調(diào)函數(shù)內(nèi)設(shè)置setPixelMap,進(jìn)度為v*pixelMapList.size()。

(轉(zhuǎn)換為Int類型)

  1. // 動(dòng)畫偵聽函數(shù) 
  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庫模塊構(gòu)建為HAR包,構(gòu)建HAR包的方法如下:

在Gradle構(gòu)建任務(wù)中,雙擊PackageDebugHar或PackageReleaseHar任務(wù),構(gòu)建Debug類型或Release類型的HAR。

待構(gòu)建任務(wù)完成后,可以在GifImage> bulid > outputs > har目錄中,獲取生成的HAR包。

項(xiàng)目源代碼地址:https://github.com/isoftstone-dev/gif_HarmonyOS

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

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)