代码拉取完成,页面将自动刷新
为确保动画效果,请使用真机运行
基于OpenHarmony Ace JavaScript UI框架的图片预览组件,包含水波纹动画、跳转动画以及相关手势:
由于TouchEvent内始终只有一组坐标,因此暂时未能实现缩放手势,而采用双击放大倍数+1(达到最大倍数再双击会还原成默认大小)的形式
启动动画 | 跳转动画 | 手势 | 下滑手势 |
---|---|---|---|
<element src="../../common/components/wave-animation/wave-animation.hml"></element>
<wave-animation color="#66000000"></wave-animation>
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
color | 水波纹颜色 | color string | #99ffffff |
<element src="../../common/components/image-list/image-list.hml"></element>
<image-list items="{{ images }}" @item-click="itemClick" @image-loaded="onImageLoaded"></image-list>
export default {
data: {
images: []
},
itemClick(e) {
const index = e.detail
...
},
onImageLoaded(e) {
const data = e.detail
...
}
}
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
items | 数据列表 | array | 无 |
column | 列数 | number | 3 |
Events
事件名 | 说明 | detail说明 |
---|---|---|
item-click | 当条目被点击时触发 | 条目的index |
image-loaded | 当列表内的图片加载完成时触发 | 参考下方 |
// image-loaded事件内detail对象的结构:
{
index: '条目的index',
size: {
wdith: '图片的宽',
height: '图片的高',
}
}
<element src="../../common/components/image-preview/image-preview.hml"></element>
<image-preview id="preview" background="#000000" max-scale="5" ></image-preview>
export default {
itemClick(e) {
...
this.$child('preview').show(this.images[index], this.x, this.y, size)
}
}
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
background | 预览时的背景色 | color string | #ffffffff |
maxScale | 最大缩放倍数 | number | 4 |
方法名 | 说明 | 返回值 | 参数说明 |
---|---|---|---|
show(url, x, y, size) | 由于页面的跳转动画暂时无法满足需求,因此需要调用show函数来显示图片预览并加载跳转动画 | void | url:图片的绝对路径、x:跳转动画的起始x坐标、y:跳转动画的起始y坐标、size:图片的原始宽高,数据结构{width: 1, height: 1} |
hide() | 隐藏图片预览并加载跳转动画 | void | 无 |
<element src="../../common/components/nav/nav.hml"></element>
<nav background="#e5e5e5" title-color="#444444"></nav>
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
background | 背景色,包含状态栏 | color string | #ffffff |
navHeight | 导航栏的高度,不包含状态栏 | number | 52 |
title | 标题 | string | strings.title |
titleColor | 标题文字颜色 | color string | #333333 |
用于手势识别
方法名 | 说明 | 返回值 | 参数说明 |
---|---|---|---|
setListener(listener) | 由于页面的跳转动画暂时无法满足需求,因此需要调用show函数来显示图片预览并加载跳转动画 | void | listener:手势回调 |
onDown(e) | 在ontouchstart中调用此函数 | void | e:TouchEvent |
onMove(e) | 在ontouchmove中调用此函数 | void | e:TouchEvent |
onUp(e) | 在ontouchend中调用此函数 | void | e:TouchEvent |
onScroll(e, distanceX, distanceY, diffX, diffY, up)
onTap(e)
onDoubleTap(e)
onLongPress(e)
onFling(vx, vy, reached)
下滑的手势与动画可以借助moving-down-handler.js实现
<canvas id="canvas" ontouchstart="down" ontouchmove="move" ontouchend="up"></canvas>
export default {
data: {
gesture: null,
...
},
async onInit() {
const that = this
this.gesture = new Gesture()
this.gesture.setListener({
onScroll: that.onScroll,
onDoubleTap: that.onDoubleTap,
onTap: that.onTap,
onLongPress: that.onLongPress,
onFling: that.onFling
})
},
onScroll(e, distanceX, distanceY, diffX, diffY, up) {
...
},
onTap(e) {
...
},
onDoubleTap(e) {
...
},
onLongPress(e) {
...
},
onFling(vx, vy, reached) {
...
},
down(e) {
this.gesture.onDown(e)
},
move(e) {
this.gesture.onMove(e)
},
up(e) {
this.gesture.onUp(e)
}
}
借助此类做惯性滚动动画
方法名 | 说明 | 返回值 | 参数说明 |
---|---|---|---|
runFling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY, task) | 开启惯性滚动动画 | void | 参考下方 |
startX 动画开始时的x坐标
startY 动画开始时的y坐标
velocityX x轴的初始速度
velocityY y轴的初始速度
minX x轴最小滚动距离
maxX x轴最大滚动距离
minY y轴最小滚动距离
maxY y轴最大滚动距离
task(x, y) 动画每一帧的回调
onFling(vx, vy, reached) {
const that = this
if (handler.handleFling(that, vx, vy) || !reached) return
const maxX = helper.calulateMaxTranslate(that.image.imageWidth, that.width)
const maxY = helper.calulateMaxTranslate(that.image.imageHeight, that.height)
that.fling.runFling(that.trans.transX, that.trans.transY, vx * 2, vy * 2, -maxX, maxX, -maxY, maxY, (x, y) => {
that.trans.transX = x
that.trans.transY = y
that.loadImage()
})
}
借助此类做缩放动画
方法名 | 说明 | 返回值 | 参数说明 |
---|---|---|---|
runScale(scale, startX, startY, finalX, finalY, task, duration) | 开启缩放动画 | void | 参考下方 |
setSize(w, h) | 设置一倍缩放下,容器大小 | void | w:宽,h:高 |
getWidth() | 获取一倍缩放下,容器的宽 | number | 无 |
getHeight() | 获取一倍缩放下,容器的高 | number | 无 |
onDoubleTap(e) {
const that = this
const x = e.touches[0].globalX
const y = e.touches[0].globalY
const maxScale = parseInt(that.maxScale)
let scale = that.image.currScale
scale++
if (scale > maxScale) scale = 1
that.image.currScale = scale
const reset = scale == 1
const lastX = that.trans.transX
const lastY = that.trans.transY
const finalX = reset ? 0 : helper.calculateScaleTranslate(x, scale, that.image.initImageWidth, that.width, lastX)
const finalY = reset ? 0 : helper.calculateScaleTranslate(y, scale, that.image.initImageHeight, that.height, lastY)
that.scale.runScale(scale, lastX, lastY, finalX, finalY, (w, h, x, y) => {
that.clearCanvas = reset
that.image.imageWidth = w
that.image.imageHeight = h
that.trans.transX = x
that.trans.transY = y
that.loadImage()
})
}
取一系列触摸事件的滑动速度,单位为像素/秒
方法名 | 说明 | 返回值 | 参数说明 | 是否异步 |
---|---|---|---|---|
addEvent(ac, x, y) | 添加触摸事件 | void | ac:触摸事件类型,取值为对应DOWN、MOVE、UP;x:手指的x坐标;y:手指的y坐标 | 否 |
getVelocity() | 获取速度 | { x: x轴速度, y: y轴速度 } | 无 | 是 |
clear() | 清除事件,在手指抬起并且计算过速度后调用 | void | 无 | 否 |
包含一些工具方法
方法名 | 说明 | 返回值 | 参数说明 | 是否异步 |
---|---|---|---|---|
getStatusBarHeight() | 获取状态栏高度 | number | 无 | 是 |
getScreenWidthAndHeight() | 获取屏幕的宽与高 | { width: 1, height: 1 } | 无 | 是 |
getPPI() | 获取ppi | number | 无 | 是 |
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。