IDC

鸿蒙HarmonyOS-获取系统照片并解码渲染显示2(附更完整的Demo)

作者:admin 2021-07-28 我要评论

想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区 https://harmonyos.51cto.com/#zz 声明一下哦,本篇是接着我的上一篇文章#2020征文-手机#...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

想了解更多内容,请访问:

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


本文转载自网络,原文链接:https://harmonyos.51cto.com/#zz

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 鸿蒙HarmonyOS-获取系统照片并解码渲染

    鸿蒙HarmonyOS-获取系统照片并解码渲染

  • 设计模式,一看就懂的桥模式,解耦可变

    设计模式,一看就懂的桥模式,解耦可变

  • 一篇文章带你弄懂Final关键字的相关知

    一篇文章带你弄懂Final关键字的相关知

  • 封杀两年后,Github恢复伊朗开发者使用

    封杀两年后,Github恢复伊朗开发者使用

腾讯云代理商
海外云服务器