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

鴻蒙HarmonyOS-獲取系統(tǒng)照片并解碼渲染顯示2(附更完整的Demo)

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

[[374067]]

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

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

https://harmonyos.51cto.com/#zz

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

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

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

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

3.應(yīng)用隨打開隨刷新

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

完整的demo在附件中進(jìn)行下載


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

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

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

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

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

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

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

 1.2 在java代碼中獲取到這個(gè)布局

  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.添加兩個(gè)Text用來顯示無照片的提示信息和照片的計(jì)數(shù)信息

2.1 首先在布局文件中加入兩個(gè)text

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

 2.2 在java中獲得這兩個(gè)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 利用能不能獲取到圖片來判斷這兩個(gè)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ù)中添加方法實(shí)現(xiàn)實(shí)時(shí)刷新

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

  • Page會(huì)在進(jìn)入INACTIVE狀態(tài)后來到前臺(tái),然后系統(tǒng)調(diào)用此回調(diào)。Page在此之后進(jìn)入ACTIVE狀態(tài),該狀態(tài)是應(yīng)用與用戶交互的狀態(tài)。所以當(dāng)你把應(yīng)用放到后臺(tái),打開照相機(jī)照相的時(shí)候,然后在打開此應(yīng)用的時(shí)候就會(huì)調(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ù)封裝了整個(gè)展示圖片的代碼

  1. public void displayPic(){ 
  2.         img_layout.removeAllComponents(); 
  3.         ArrayList<Integer> img_ids = new ArrayList<Integer>(); 
  4.         DataAbilityHelper helper = DataAbilityHelper.creator(getContext()); 
  5.         try { 
  6.             ResultSet result = helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, nullnull); 
  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.     } 

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

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

完整demo的源碼見附件

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

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

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

https://harmonyos.51cto.com/#zz

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-01-04 10:03:28

鴻蒙手機(jī)app開發(fā)顯示系統(tǒng)圖片

2022-03-07 16:46:03

HarmonyOS鴻蒙操作系統(tǒng)

2022-06-29 13:59:40

家居應(yīng)用鴻蒙

2022-02-28 15:52:07

canvasHarmonyOS鴻蒙

2022-09-05 15:22:27

ArkUIets

2025-04-11 08:45:00

2025-03-31 08:52:00

AI模型研究

2022-03-18 15:41:29

原子化服務(wù)HarmonyOS鴻蒙

2018-02-05 08:25:14

LinuxDebian離線更新

2017-08-22 15:27:50

冷卻系統(tǒng)數(shù)據(jù)中心

2011-06-14 18:37:50

Flash

2014-07-10 10:09:11

JSON數(shù)據(jù)行轉(zhuǎn)列

2021-10-08 10:02:50

鴻蒙HarmonyOS應(yīng)用

2021-06-06 17:50:41

微軟Edge瀏覽器

2021-05-28 09:52:00

鴻蒙HarmonyOS應(yīng)用

2012-05-22 14:26:10

Windows 8顯示器

2021-01-11 10:05:03

鴻蒙HarmonyOS鴻蒙3861

2010-07-28 13:20:25

IT運(yùn)維系統(tǒng)
點(diǎn)贊
收藏

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