HarmonyOS ArkUI之仿微信朋友圈图片预览

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

和华为官方合作共建的朋友片预鸿蒙技术社区

https://harmonyos.51cto.com

前言

新世界的大门已打开,关也关不住!

《华为开发者大会2021》推出了方舟开发框l架(ArkUI),圈图官方解释:方舟开发框架是微信一种跨设备的高性能UI开发框架,支持声明式编程和跨设备多态UI。朋友片预

本项目就是圈图基于ArkUI中的声明式编程开发,语言ETS(Extended Type Script),微信代码都在ets文件中编写,亿华云计算朋友片预这个文件用于描述 UI 布局、圈图样式、微信事件交互和页面逻辑。朋友片预

官方文档地址:基于TS扩展的圈图声明式开发范式1 基于TS扩展的声明式开发范式2

本文介绍仿微信朋友圈实现列表展示,九宫格小图图片展示,微信点击图片进行图片预览,朋友片预图片左右滑动切换。圈图

效果演示

项目类说明

主要知识点

九宫格列表和选择图片列表:网格容器组件(Grid)

浏览大图切换页面:滑动容器组件(Swiper)

循环渲染迭代数组:渲染组件(ForEach) (目前第二个参数中 itemGenerator: (item: any, index?: number) => void index不能使用)

基础的组件:图片显示(Image) 文本显示(Text) 按钮(Button)

布局容器组件:沿垂直方向布局的容器(Column),沿水平方向布局容器(Row),云服务器堆叠容器(Stack)

代码解析

1、朋友圈列表展示

列表使用List组件实现多数据列表展示(核心代码实例)。

List({  initialIndex: 0 }) {     ForEach(this.listItems, item => {       ListItem() {         Row() {           Column() {             Image(item.bigImg)              .width(50)              .height(50)              .borderRadius(30)              .margin(5)              .onClick(() => {               })            Blank()          }.height("100%")          Column() {             Text(item.name)              .fontSize(16)              .margin({  left: 0})              .width("100%")            Row() {               Text(item.content)                .fontSize(14)                .margin({  top: 10 })                .fontColor(Color.Grey)                .width("80%")                .textAlign(TextAlign.Start)              Blank()            }            Column() {               Grid() {                 ForEach(this.imageDataArray, item => {                   GridItem() {                     Image(item.smallImg).width(50).height(50)                  }.sharedTransition("0", {  duration: 100, curve: Curve.Linear })                  .onClick(()=>{                     console.log("item.id==="+item.id)                    router.push({                       uri: pages/imageflige,                      params: {                         imageIndex: item.id, // 当前图片位置                      }                    })                  })                }, item => item.name)              }              .width(155)              .columnsTemplate(1fr 1fr 1fr)              .rowsGap(2)              .columnsGap(2)            }            .width(100%)            .height(200)            .alignItems(HorizontalAlign.Start)            .margin({  top: 10 })          }.height("100%")        }        .height("100%")      }      .height(250)      .margin({  top: 10 })    }, item => item.name)  } 

2、九宫格展示

主要是网格容器Grid组件和渲染组件ForEach(核心代码示例)。

Column() {      Grid() {        ForEach(this.imageDataArray, item => {          GridItem() {            Image(item.smallImg).width(50).height(50)         }.sharedTransition("0", {  duration: 100, curve: Curve.Linear })         .onClick(()=>{            console.log("item.id==="+item.id)           router.push({              uri: pages/imageflige,             params: {                imageIndex: item.id, // 当前图片位置             }           })         })       }, item => item.name)     }     .width(155)     .columnsTemplate(1fr 1fr 1fr)     .rowsGap(2)     .columnsGap(2)   }   .width(100%)   .height(200)   .alignItems(HorizontalAlign.Start)   .margin({  top: 10 }) 

3、大图预览

使用滑动容器组件Swiper,通过传递点击的当前下标定位到指定位置(核心代码示例)。

import router from @system.router; @Entry @Component struct Imageflige {   @State private listPicture: Array<Resource> = [    $r("app.media.food1"), $r("app.media.food2"), $r("app.media.food3"),    $r("app.media.food4"), $r("app.media.food5"), $r("app.media.food1"),    $r("app.media.food2"), $r("app.media.food3"), $r("app.media.food4")  ]  @State imageIndex: number = 0  build() {     Column() {       Stack({  alignContent: Alignment.Top }) {         // 切换页面        Swiper() {           ForEach(this.listPicture, item => {             // 图片            Image(item)              .width(100%)              .height(100%)              .objectFit(ImageFit.Contain) //缩放类型          }, item => item.toString())        }        .width(100%)        .height(100%)        .index(this.imageIndex) // 设置当前索引        .indicator(true) // 不显示指示器        .loop(true) // 关闭循环        .sharedTransition("0", {  duration: 100, curve: Curve.Linear })        .onChange((index: number) => {  // 索引变化监听          // 更新索引值          this.imageIndex = index        })        Image($r("app.media.back"))          .width(35)          .height(35)          .margin(10)          .backgroundColor(Color.White)          .onClick(() => {             router.back()          })      }      .height("100%")      .width("100%")      .alignContent(Alignment.TopStart)    }  }  private aboutToAppear() {     this.imageIndex = router.getParams().imageIndex  } }    

评论功能有两部分:评论列表和评论发送输入框。

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

和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

香港云服务器
系统运维
上一篇:当投资者经过第二阶段的认真学习之后又充满了信心,认为自己可以在市场上叱咤风云地大干一场了。但没想到“看花容易绣花难”,由于对理论知识不会灵活运用.从而失去灵活应变的本能,就经常会出现小赢大亏的局面,结果往往仍以失败告终。这使投资者很是困惑和痛苦,不知该如何办,甚至开始怀疑这个市场是不是不适合自己。在这种情况下,有的人选择了放弃,但有的意志坚定者则决定做最后的尝试。
下一篇:域后缀首选.com,.net,然后是.cn。后缀选择不当,导致流量损失。域名是企业与互联网网址之间的链接,关键是企业在网络上存在的标志。因此,选择好域名是开展网上工作的首要重要条件。