鸿蒙5.0开发实战-图片预览器实现案例

往期鸿蒙全套实战精彩文章必看内容:


今天分享一个简单的图片预览器,支持加载本地和网络图片,支持滑动翻页和默认页数。使用系统弹窗组件CustomDialogController实现。

首先搭建弹窗的内容部分,使用Swiper可以完美解决多张图片的滑动、翻页、默认页数等功能。

@CustomDialogexport default  struct PhotoBrowser {  controller:CustomDialogController  //图片列表  images:Resource[]|string[]  //默认第几张  index:number = 0
  build() {    Swiper(){      ForEach(this.images,(str,index)=>{        Image(str)          .width('100%')          .height('100%')          .objectFit(ImageFit.Auto)      })    }    .index(this.index)    .indicatorStyle({      color:Color.White    })    .loop(false)    .backgroundColor(Color.Black)    .width('100%')    .height('100%')    .onClick(()=>{      this.controller.close()    })  }}

内容部分写完后,我们要把它放进弹窗控制器里,要注意CustomDialogController默认样式不是全屏的,通过设置customStyle和offset属性可以让弹窗全屏:

dialogController: CustomDialogController = new CustomDialogController({    builder: PhotoBrowser({images:this.imgList,index:1}),    customStyle: true,    offset: { dx: 0, dy: 0 },    alignment: DialogAlignment.Top,  })

  使用时和普通弹窗一样:

 this.dialogController.open() 
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值