日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
鴻蒙HarmonyOS三方件開發(fā)指南-GifImage

想了解更多內容,請訪問:

創(chuàng)新互聯(lián)服務項目包括福海網站建設、福海網站制作、福海網頁制作以及福海網絡營銷策劃等。多年來,我們專注于互聯(lián)網行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯(lián)網行業(yè)的解決方案,福海網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到福海省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!

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

https://harmonyos.

1. GifImage組件功能介紹

1.1. 功能介紹:

GifImage組件是一個可以顯示加載動態(tài)圖片(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.  
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:height="match_parent" 
  4.     ohos:width="match_parent" 
  5.     ohos:orientation="vertical"> 
  6.  
  7.     
  8.         ohos:id="$+id:testimg" 
  9.         ohos:height="match_content" 
  10.         ohos:width="match_content" 
  11.         ohos:image_src="$media:gif6" 
  12.         ohos:layout_alignment="horizontal_center" 
  13.     /> 
  14.  
  15.     
  16.         ohos:id="$+id:testimg1" 
  17.         ohos:image_src="$media:coffe" 
  18.         ohos:height="match_content" 
  19.         ohos:width="match_content" 
  20.         ohos:layout_alignment="horizontal_center" 
  21.         /> 
  22.  
  23.     
  24.         ohos:layout_alignment="horizontal_center" 
  25.         ohos:height="match_content" 
  26.         ohos:image_src="$media:deleting" 
  27.         ohos:width="match_content" 
  28.         ohos:id="$+id:text" 
  29.         /> 
  30.  
  31.  

2.3. MainAbilitySlice的UI加載代碼

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

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

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

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

 
 
 
 
  1. // 動畫 
  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啟動動畫:

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

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

(轉換為Int類型)

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

在Gradle構建任務中,雙擊PackageDebugHar或PackageReleaseHar任務,構建Debug類型或Release類型的HAR。

待構建任務完成后,可以在GifImage> bulid > outputs > har目錄中,獲取生成的HAR包。

項目源代碼地址:https://github.com/isoftstone-dev/gif_HarmonyOS

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

想了解更多內容,請訪問:

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

https://harmonyos.


分享題目:鴻蒙HarmonyOS三方件開發(fā)指南-GifImage
本文URL:http://www.dlmjj.cn/article/dpdjjos.html