成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

開發 OpenHarmony
文章由鴻蒙社區產出,想要了解更多內容請前往:51CTO和華為官方戰略合作共建的鴻蒙技術社區https://harmonyos.51cto.com/#zz

[[374067]]

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

51CTO和華為官方合作共建的鴻蒙技術社區

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

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

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

1.沒有圖片會出現提示

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

3.應用隨打開隨刷新

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

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


老規矩先說升級的大概思路

1.采用TableLayout布局實現了所有照片的顯示

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

3.在onActive生命周期函數中添加方法實現實時刷新

1.采用TableLayout布局實現了所有照片的顯示

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

  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代碼中獲取到這個布局

  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用來顯示無照片的提示信息和照片的計數信息

2.1 首先在布局文件中加入兩個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中獲得這兩個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("照片數量:"+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生命周期函數中添加方法實現實時刷新

3.1 onActive生命周期函數介紹

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

3.2 在onActive函數中添加需要的調用

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

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

  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("照片數量:"+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目前來說,還算基本能看。。。有時間的我會繼續嘗試修改完善。

有興趣的朋友可以關注一下

完整demo的源碼見附件

©著作權歸作者和HarmonyOS技術社區共同所有,如需轉載,請注明出處,否則將追究法律責任

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

51CTO和華為官方合作共建的鴻蒙技術社區

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

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

2021-01-04 10:03:28

鴻蒙手機app開發顯示系統圖片

2022-03-07 16:46:03

HarmonyOS鴻蒙操作系統

2022-06-29 13:59:40

家居應用鴻蒙

2022-02-28 15:52:07

canvasHarmonyOS鴻蒙

2021-05-28 09:52:00

鴻蒙HarmonyOS應用

2022-09-05 15:22:27

ArkUIets

2022-03-18 15:41:29

原子化服務HarmonyOS鴻蒙

2020-11-11 11:56:05

HarmonyOS

2021-05-19 08:41:11

鴻蒙HarmonyOS應用

2025-04-11 08:45:00

2021-10-08 10:02:50

鴻蒙HarmonyOS應用

2025-03-31 08:52:00

AI模型研究

2021-05-17 14:37:02

鴻蒙HarmonyOS應用

2017-08-22 15:27:50

冷卻系統數據中心

2018-02-05 08:25:14

LinuxDebian離線更新

2021-01-11 10:05:03

鴻蒙HarmonyOS鴻蒙3861

2020-11-05 10:05:25

App

2021-06-06 17:50:41

微軟Edge瀏覽器
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩黄色av| 成人午夜精品一区二区三区 | 最新免费黄色网址 | 亚洲国产成人av好男人在线观看 | 国产精品永久 | 国产一区二区三区免费 | 福利视频一区二区三区 | 99福利视频| 男人的天堂avav | 国产视频一区二区 | 国产一区二区三区视频 | 欧美日韩在线免费观看 | 精品国产乱码久久久久久影片 | 欧美在线高清 | 国产一区亚洲 | 二区不卡 | 亚洲一区二区免费视频 | 国产激情第一页 | 久久一区二区三区免费 | 欧美精品成人影院 | 成人欧美一区二区三区黑人孕妇 | 精品国产亚洲一区二区三区大结局 | 最近中文字幕在线视频1 | 中文字幕免费 | 伊伊综合网 | 久久精品视频91 | 久久久国产精品网站 | 日韩欧美不卡 | 午夜免费观看 | 一区二区三区国产 | 成人自拍视频网站 | 欧美激情精品久久久久久 | 很黄很污的网站 | 久久99精品久久久久久国产越南 | av国产精品 | 免费精品 | 亚洲成人99 | 99久久久久 | 国产日韩欧美激情 | 久久久久久久久久久久久九 | 91久久国产综合久久 |