当前位置:   article > 正文

手把手教你用鸿蒙HarmonyOS实现微信聊天界面(三)_hormonyos 聊天功能

hormonyos 聊天功能

简介

       本系列文章记录作者大三开学第一个月中学习HarmonyOS移动应用开发学习经历,此篇为《微信聊天界面》项目,实现功能有

1、聊天信息功能,包括图片、文字

2、发送定位功能

3、选择发送本机图片功能

4、拍照并发送图片功能

        如果在真机调试请将config文件中包名换成自己的应用包名即可,申请权限有文件读写、位置获取、相机调用、麦克风调用。
 

之前文章

手把手教你用鸿蒙HarmonyOS实现微信聊天界面(一)_MarsHys的博客-CSDN博客

手把手教你用鸿蒙HarmonyOS实现微信聊天界面(二)_MarsHys的博客-CSDN博客

聊天界面效果如图

 

 图片选择界面

         在该聊天界面选择图片里主要组件是ListContainer其余就是顶栏的Text与底下的Button。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:alignment="center"
  7. ohos:orientation="vertical">
  8. <ListContainer
  9. ohos:weight="11"
  10. ohos:background_element="#FFE9E9E9"
  11. ohos:id="$+id:list_container"
  12. ohos:height="match_parent"
  13. ohos:width="match_parent"
  14. ohos:layout_alignment="horizontal_center"/>
  15. <Button
  16. ohos:id="$+id:close_image_button"
  17. ohos:weight="1"
  18. ohos:width="match_parent"
  19. ohos:height="0"
  20. ohos:text_size="27fp"
  21. ohos:text="关闭"
  22. ohos:background_element="#FFFDFDFF"
  23. />
  24. </DirectionalLayout>

         是通过对话框弹出的界面CommonDialog组件,由图片Button的点击事件触发

  1. imageButton.setClickedListener(component1 -> {
  2. dialog = new CommonDialog(getContext());
  3. initImageData();
  4. DirectionalLayout directionalLayout = new DirectionalLayout(getContext());
  5. Component component2;
  6. component2 = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_image_main, null, false);
  7. imagelistContainer= (ListContainer) component2.findComponentById(ResourceTable.Id_list_container);
  8. closeimagebutton = (Button) component2.findComponentById(ResourceTable.Id_close_image_button);
  9. closeimagebutton.setClickedListener(component3 -> {
  10. dialog.destroy();
  11. });
  12. directionalLayout.addComponent(component2);
  13. initImageListContainer();
  14. dialog.setContentCustomComponent(directionalLayout);
  15. dialog.setTitleText("图片");
  16. dialog.setSize(MATCH_PARENT,MATCH_PARENT);
  17. //dialog.setContentText("This is CommonDialog Content area.");
  18. dialog.setButton(IDialog.BUTTON3, "CONFIRM", (iDialog, i) -> iDialog.destroy());
  19. dialog.show();
  20. //addAndUpdateMessage(messageData.size(),"message","image");
  21. });

         在第二篇中我们讲到图片是通过Uri访问的图片资源,Uri的资源获取类PictureManager 在上篇已经讲过。在展示里在ListContainer的实体类中是按行存储Uri,在Provider里转换为图片。

        图片资源实体类

  1. public class ImageLineItem {
  2. private int index;
  3. private Uri[] uris;
  4. public ImageLineItem(int index) {
  5. this.index = index;
  6. }
  7. public int getIndex() {
  8. return index;
  9. }
  10. public void setIndex(int index) {
  11. this.index = index;
  12. }
  13. public Uri[] getUris() {
  14. return uris;
  15. }
  16. public void setUris(Uri[] uris) {
  17. this.uris = uris;
  18. }
  19. }

         Provider里加载图片布局,对每一个Image组件都添加了点击方法调用发送并更新消息方法addAndUpdateMessag实现点击发送图片。

  1. public class ImageLineProvider extends BaseItemProvider {
  2. private static final String TAG = ImageLineProvider.class.getSimpleName();
  3. private List<ImageLineItem> list;
  4. private AbilitySlice slice;
  5. private MainAbilitySlice mainAbilitySlice;
  6. public void setMainAbilitySlice(MainAbilitySlice mainAbilitySlice){
  7. this.mainAbilitySlice = mainAbilitySlice;
  8. }
  9. public ImageLineProvider(List<ImageLineItem> list, AbilitySlice slice) {
  10. LogUtil.info(TAG,"list.size() : "+list.size());
  11. this.list = list;
  12. this.slice = slice;
  13. }
  14. @Override
  15. public int getCount() {
  16. return list == null ? 0 : list.size();
  17. }
  18. @Override
  19. public Object getItem(int position) {
  20. if (list != null && position >= 0 && position < list.size()){
  21. return list.get(position);
  22. }
  23. return null;
  24. }
  25. @Override
  26. public long getItemId(int position) {
  27. return position;
  28. }
  29. private Component getItemComponent(int position) {
  30. return getComponent(position);
  31. }
  32. private Component getComponent(int position) {
  33. LogUtil.info(TAG,"list.size()"+list.size());
  34. final Component cpt;
  35. cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_images_line, null, false);
  36. ImageLineItem imageLineItem = list.get(position);
  37. Image image1,image2,image3;
  38. image1 = (Image) cpt.findComponentById(ResourceTable.Id_image1);
  39. image2 = (Image) cpt.findComponentById(ResourceTable.Id_image2);
  40. image3 = (Image) cpt.findComponentById(ResourceTable.Id_image3);
  41. DataAbilityHelper helper=DataAbilityHelper.creator(slice.getContext());
  42. //定义图片来源对象
  43. ImageSource imageSource;
  44. Uri[] uris = imageLineItem.getUris();
  45. FileDescriptor fd = null;
  46. image1.setClickedListener(component1 -> {
  47. mainAbilitySlice.addAndUpdateMessage(mainAbilitySlice.getMessageDataSize(), String.valueOf(uris[0]),"image");
  48. mainAbilitySlice.getDialog().destroy();
  49. });
  50. image2.setClickedListener(component1 -> {
  51. mainAbilitySlice.addAndUpdateMessage(mainAbilitySlice.getMessageDataSize(), String.valueOf(uris[1]),"image");
  52. mainAbilitySlice.getDialog().destroy();
  53. });
  54. image3.setClickedListener(component1 -> {
  55. mainAbilitySlice.addAndUpdateMessage(mainAbilitySlice.getMessageDataSize(), String.valueOf(uris[2]),"image");
  56. mainAbilitySlice.getDialog().destroy();
  57. });
  58. try {
  59. fd = helper.openFile(uris[0], "r");
  60. } catch (DataAbilityRemoteException | FileNotFoundException e) {
  61. e.printStackTrace();
  62. }
  63. imageSource = ImageSource.create(fd, null);
  64. //创建位图
  65. PixelMap pixelMap = imageSource.createPixelmap(null);
  66. image1.setPixelMap(pixelMap);
  67. imageSource.release();
  68. helper.release();
  69. try {
  70. fd = helper.openFile(uris[1], "r");
  71. } catch (DataAbilityRemoteException | FileNotFoundException e) {
  72. e.printStackTrace();
  73. }
  74. imageSource = ImageSource.create(fd, null);
  75. //创建位图
  76. pixelMap = imageSource.createPixelmap(null);
  77. image2.setPixelMap(pixelMap);
  78. imageSource.release();
  79. helper.release();
  80. try {
  81. fd = helper.openFile(uris[2], "r");
  82. } catch (DataAbilityRemoteException | FileNotFoundException e) {
  83. e.printStackTrace();
  84. }
  85. imageSource = ImageSource.create(fd, null);
  86. //创建位图
  87. pixelMap = imageSource.createPixelmap(null);
  88. image3.setPixelMap(pixelMap);
  89. imageSource.release();
  90. helper.release();
  91. return cpt;
  92. }
  93. @Override
  94. public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
  95. return getItemComponent(position);
  96. }
  97. }

        图片获取与布局就讲完了,最后看一下发送效果吧

(Preview:下篇讲如何实现发送定位功能)

 Gitee链接

WeChatPage: 鸿蒙版微信界面

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/306773
推荐阅读
相关标签
  

闽ICP备14008679号