基于HarmonyOS ArkUI 3.0 框架,成功开发了相册
土司dgg 2021-12-02 10:54:15 发布3447 浏览 20 点赞 25 收藏
HarmonyOS ArkUI 3.0 正式到来,今天就带大家感受一下HarmonyOS ArkUI 3.0 框架开发的相册功能,功能很简单,主要是图片放大和图片左右滑动事件。
实现效果

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

主要是选择开发语言为ets

代码实现
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
方舟开发框架声明式编程很符合接下来前端的趋势,希望框架能越来越完善。
©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
其它
标签
HarmonyOS
开发
其他
相关推荐
鸿蒙PC突破金融核心场景:四大金融IT服务商完成自研PC框架适配
智慧的键盘侠
11
0用JSLeakWatcher解决HarmonyOS内存泄漏难题
鸿蒙小助手
1357
0HarmonyOS应用性能监测服务APMS-崩溃问题定位分析指导
鸿蒙小助手
705
0HarmonyOS应用性能监测服务APMS-故障分析:快速定位线上崩溃根因
鸿蒙小助手
702
0
土司dgg
我还没有写个人简介......
83
帖子
0
提问
201
粉丝
最新发布
高通收购RISC-V芯片设计公司Ventana以加强CPU研发
2025-12-11 08:34:40 发布特朗普称英伟达可向中国出售 H200 芯片,华盛顿要分得 25% 收益
2025-12-09 10:37:25 发布热门推荐
京公网安备:11010502051901号