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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
鴻蒙HarmonyOS-獲取系統(tǒng)照片并解碼渲染顯示2(附更完整的Demo)

[[374067]]

想了解更多內(nèi)容,請訪問:

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

https://harmonyos./#zz

聲明一下哦,本篇是接著我的上一篇文章——#2020征文-手機#獲取系統(tǒng)照片并解碼渲染顯示(附完整demo) 原創(chuàng) 來寫的。需要的可以先讀讀上一篇文件滴,本篇則是在上一篇代碼基礎(chǔ)上進一步修改而來。

說一下功能的升級(較上一版本):(ps:我也想搞分布式,可目前的現(xiàn)實不允許,還是等遠程模擬器的多設(shè)備分布式聯(lián)調(diào)能力開放吧)

1.沒有圖片會出現(xiàn)提示

2.相冊中的所有照片都可顯示,并且顯示計數(shù)

3.應用隨打開隨刷新

不多說,先上demo跑起來的效果,如下兩張圖:第一張圖是在手機遠程模擬器中一張圖片都沒有時候的顯示界面,第二張是自己打開遠程模擬器的照相功能一頓亂點,照了N張之后的顯示界面

完整的demo在附件中進行下載

老規(guī)矩先說升級的大概思路

1.采用TableLayout布局實現(xiàn)了所有照片的顯示

2.添加兩個Text用來顯示無照片的提示信息和照片的計數(shù)信息

3.在onActive生命周期函數(shù)中添加方法實現(xiàn)實時刷新

1.采用TableLayout布局實現(xiàn)了所有照片的顯示

1.1 在布局文件中添加TableLayout布局代碼,需要注意的是:這里我外邊套了一層ScrollView,這是為了在圖片多的時候,TableLayout可以滑動

 
 
 
 
  1.                 ohos:height="600vp" 
  2.                 ohos:left_padding="25vp" 
  3.                 > 
  4.     
  5.         ohos:id="$+id:layout_id" 
  6.         ohos:height="match_content" 
  7.         ohos:width="match_parent" 
  8.         > 
  9.      
  10.      

 1.2 在java代碼中獲取到這個布局

 
 
 
 
  1.  TableLayout img_layout;         
  2. img_layout = (TableLayout)findComponentById(ResourceTable.Id_layout_id); 
  3. img_layout.setColumnCount(3); 

 1.3 將新生成的圖片放入布局中 

 
 
 
 
  1. Image img = new Image(MainAbilitySlice.this); 
  2. img.setId(mediaId); 
  3. img.setHeight(300); 
  4. img.setWidth(300); 
  5. img.setMarginTop(20); 
  6. img.setMarginLeft(20); 
  7. img.setPixelMap(pixelMap); 
  8. img.setScaleMode(Image.ScaleMode.ZOOM_CENTER); 
  9. img_layout.addComponent(img); 

 2.添加兩個Text用來顯示無照片的提示信息和照片的計數(shù)信息

2.1 首先在布局文件中加入兩個text

 
 
 
 
  1.       ohos:id="$+id:text_pre_id" 
  2.       ohos:width="match_parent" 
  3.       ohos:height="match_parent" 
  4.       ohos:text_alignment="center" 
  5.       ohos:text_size="45fp" 
  6.       ohos:text="Opening..."> 
  7.   
  8.       ohos:id="$+id:text_id" 
  9.       ohos:width="match_content" 
  10.       ohos:height="match_content" 
  11.       ohos:text_alignment="center" 
  12.       ohos:text_size="20fp"> 

 2.2 在java中獲得這兩個text組件

 
 
 
 
  1. Text pre_text,text; 
  2. pre_text = (Text)findComponentById(ResourceTable.Id_text_pre_id); 
  3. text = (Text)findComponentById(ResourceTable.Id_text_id); 

 2.3 利用能不能獲取到圖片來判斷這兩個text組件的顯示邏輯

 
 
 
 
  1. if(img_ids.size() > 0){ 
  2.          pre_text.setVisibility(Component.HIDE); 
  3.          text.setVisibility(Component.VISIBLE); 
  4.          text.setText("照片數(shù)量:"+img_ids.size()); 
  5.      }else{ 
  6.          pre_text.setVisibility(Component.VISIBLE); 
  7.          pre_text.setText("No picture."); 
  8.          text.setVisibility(Component.HIDE); 
  9.      } 

 3.在onActive生命周期函數(shù)中添加方法實現(xiàn)實時刷新

3.1 onActive生命周期函數(shù)介紹

  • Page會在進入INACTIVE狀態(tài)后來到前臺,然后系統(tǒng)調(diào)用此回調(diào)。Page在此之后進入ACTIVE狀態(tài),該狀態(tài)是應用與用戶交互的狀態(tài)。所以當你把應用放到后臺,打開照相機照相的時候,然后在打開此應用的時候就會調(diào)用該生命周期函數(shù)

3.2 在onActive函數(shù)中添加需要的調(diào)用

 
 
 
 
  1. @Override 
  2.  public void onActive() { 
  3.      super.onActive(); 
  4.      displayPic(); 
  5.  } 

 3.3 displayPic函數(shù)封裝了整個展示圖片的代碼

 
 
 
 
  1. public void displayPic(){ 
  2.         img_layout.removeAllComponents(); 
  3.         ArrayList img_ids = new ArrayList(); 
  4.         DataAbilityHelper helper = DataAbilityHelper.creator(getContext()); 
  5.         try { 
  6.             ResultSet result = helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, null, null); 
  7.             if(result == null){ 
  8.                 pre_text.setVisibility(Component.VISIBLE); 
  9.             }else{ 
  10.                 pre_text.setVisibility(Component.HIDE); 
  11.             } 
  12.             while(result != null && result.goToNextRow()){ 
  13.                 int mediaId = result.getInt(result.getColumnIndexForName(AVStorage.Images.Media.ID)); 
  14.                 Uri uri = Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,""+mediaId); 
  15.                 FileDescriptor filedesc = helper.openFile(uri,"r"); 
  16.                 ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions(); 
  17.                 decodingOpts.desiredSize = new Size(300,300); 
  18.                 ImageSource imageSource = ImageSource.create(filedesc,null); 
  19.                 PixelMap pixelMap = imageSource.createThumbnailPixelmap(decodingOpts,true); 
  20.                 Image img = new Image(MainAbilitySlice.this); 
  21.                 img.setId(mediaId); 
  22.                 img.setHeight(300); 
  23.                 img.setWidth(300); 
  24.                 img.setMarginTop(20); 
  25.                 img.setMarginLeft(20); 
  26.                 img.setPixelMap(pixelMap); 
  27.                 img.setScaleMode(Image.ScaleMode.ZOOM_CENTER); 
  28.                 img_layout.addComponent(img); 
  29.                 System.out.println("xxx"+uri); 
  30.                 img_ids.add(mediaId); 
  31.             } 
  32.         }catch (DataAbilityRemoteException | FileNotFoundException e){ 
  33.             e.printStackTrace(); 
  34.         } 
  35.         if(img_ids.size() > 0){ 
  36.             pre_text.setVisibility(Component.HIDE); 
  37.             text.setVisibility(Component.VISIBLE); 
  38.             text.setText("照片數(shù)量:"+img_ids.size()); 
  39.         }else{ 
  40.             pre_text.setVisibility(Component.VISIBLE); 
  41.             pre_text.setText("No picture."); 
  42.             text.setVisibility(Component.HIDE); 
  43.         } 
  44.     } 

 這個demo目前來說,還算基本能看。。。有時間的我會繼續(xù)嘗試修改完善。

有興趣的朋友可以關(guān)注一下

完整demo的源碼見附件

?著作權(quán)歸作者和HarmonyOS技術(shù)社區(qū)共同所有,如需轉(zhuǎn)載,請注明出處,否則將追究法律責任

想了解更多內(nèi)容,請訪問:

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

https://harmonyos./#zz

 


標題名稱:鴻蒙HarmonyOS-獲取系統(tǒng)照片并解碼渲染顯示2(附更完整的Demo)
URL分享:http://www.dlmjj.cn/article/djpedic.html