基于HarmonyOS ArkUI 3.0 框架,成功开发了相册
头像 土司dgg 2021-12-02 10:54:15    发布
3441 浏览 20 点赞 25 收藏

HarmonyOS ArkUI 3.0 正式到来,今天就带大家感受一下HarmonyOS ArkUI 3.0 框架开发的相册功能,功能很简单,主要是图片放大和图片左右滑动事件。

实现效果

基于HarmonyOS ArkUI 3.0 框架,成功开发了相册-鸿蒙开发者社区

项目开发
创建新的应用
选择一个空的模板  

基于HarmonyOS ArkUI 3.0 框架,成功开发了相册-鸿蒙开发者社区

主要是选择开发语言为ets  

基于HarmonyOS ArkUI 3.0 框架,成功开发了相册-鸿蒙开发者社区

代码实现
hml结构:渲染函数


build() {
      // 超出屏幕,可滚动
    Scroll() {
      //      图片列表
      Flex({ wrap: FlexWrap.Wrap, justifyContent: FlexAlign.Center, alignContent: FlexAlign.Center }) {
        ForEach(this.imageList, (item, index) => {
          // 循环图片
          Image(item.url)
            .width(item.width)
            .height(item.height)
            .opacity(item.opacity)
            .onClick(() => {
              // 保存点击的id
              if (this.currentId === 0) {
                this.currentId = item.id
                this.currentIndex = this.currentId - 1
                this.isBig = true
              } else {
                this.currentId = 0
                this.currentIndex = -2
                this.isBig = false
              }
              this.changeList()
            })
            // 滑动事件
            .gesture(PanGesture({ direction: PanDirection.Horizontal })
              .onActionStart((e: GestureEvent) => { // 左右滑动开始
                // 保存开始的坐标
                if (!this.isBig) return false
                this.startX = e.offsetX
              })
              .onActionEnd((e: GestureEvent) => { // 左右滑动结束
                // 计算左滑还是右滑,初始化开始坐标
                if (!this.isBig) return false
                this.isSlideL = e.offsetX - this.startX > 0 ? false : true
                this.startX = 0
                this.showAnimate()
              })
            )
            .border({ width: 2, radius: 2, color: '#fff' })
        })
      }.width('100%')
    }

  }
  • 数据处理
  changeList() {
    this.imageList = this.imageList.map((item) => {
      const newI = item
      if (item.id === this.currentId && this.isBig) {
        newI.width = '100%'
        newI.height = '100%'
        newI.opacity = 1
      } else if (item.id !== this.currentId && this.isBig) {
        newI.width = '0%'
        newI.height = 0
        newI.opacity = 0
      } else {
        newI.width = '50%'
        newI.height = 200
        newI.opacity = 1
      }
      return newI
    })
  }
// 左右滑动,改变图片的透明度
  showAnimate() {
    //    计算下一个图片的index序列
    let nextIndex = this.currentIndex
    if (!this.isSlideL) {
      nextIndex -= 1
    } else {
      nextIndex += 1
    }
    nextIndex = nextIndex === this.imageList.length ? 0 : (nextIndex === -1 ? (this.imageList.length - 1) : nextIndex)
    //    将下一个图片序列赋值给当前的序列
    this.currentIndex = nextIndex
    this.imageList = this.imageList.map((item, index) => {
      const newI = item
      if (nextIndex === index) {
        newI.opacity = 1
        newI.width = '100%'
        newI.height = '100%'
      } else {
        newI.opacity = 0
        newI.width = '0%'
        newI.height = '0%'
      }
      return newI
    })
  }ndefined

方舟开发框架声明式编程很符合接下来前端的趋势,希望框架能越来越完善。


©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
其它
地址:北京市朝阳区北三环东路三元桥曙光西里甲1号第三置业A座1508室 商务内容合作QQ:2291221 电话:13391790444或(010)62178877
版权所有:电脑商情信息服务集团 北京赢邦策略咨询有限责任公司
声明:本媒体部分图片、文章来源于网络,版权归原作者所有,我司致力于保护作者版权,如有侵权,请与我司联系删除
京ICP备:2022009079号-2
京公网安备:11010502051901号
ICP证:京B2-20230255