@ComponentV2export struct CropVideoTimeCom { //需要传入的参数 //视频总时长 @Param videoDuration: number = 0 //视频当前时长 @Param videoCurrentDuration: number = 0 //是否播放 @Param isPlay: boolean = false //最小间距//可不传 @Param minSpace: number = 26 //传入视频时间,可控制最小间距为1秒 //======================================================================= //左侧手势开始的位移 @Local startLOffset: number = 0 //左侧中间位移 @Local updateLOffset: number = 0 //左侧手势结束的位移 @Local endLOffset: number = 0 //右侧手势开始的位移 @Local startROffset: number = 0 //右侧中间位移 @Local updateROffset: number = 260 //右侧手势结束的位移 @Local endROffset: number = 260 //线宽度 @Local lineWidth: number = 260 //======================================================================= //视频进度 @Local progress: number = this.endLOffset //视频开始时间 @Local startTime: number = 0 //视频结束时间 @Local endTime: number = 0 //======================================================================= //左右边框宽度 frameWidth: number = 15 //最大宽度 maxCropWidth: number = 260 //拖动位移 @Local dragOffsetRx: number = this.updateLOffset + this.lineWidth private LPanOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Right | PanDirection.Left, distance: 1, }) private RPanOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left | PanDirection.Right, distance: 1, }) //初始化时间//组件创建触发回调 @Event initUpdateTime: (startTime: number, endTime: number) => void = () => { } //左侧手势结束触发回调 @Event onLPanActionUpdate: (startTime: number, endTime: number) => void = () => { } //右侧手势结束触发回调 @Event onRPanActionUpdate: (startTime: number, endTime: number) => void = () => { }
@Builder defaultBuilder() { Row().width(this.maxCropWidth).height(46).backgroundColor(Color.Blue) }
//传入视频帧图片UI结构 @BuilderParam imageBuilder: () => void = this.defaultBuilder
//当视频总时长改变时赋值传出,直接赋值传出可能获取不到视频时间 @Monitor('videoDuration') videoDurationChange() { this.endTime = this.videoDuration this.initUpdateTime(this.startTime, this.endTime) }
//监听视频当前进度 @Monitor('videoCurrentDuration') videoCurrentDurationChange() { if (this.videoCurrentDuration > 0) { this.progress = this.videoCurrentDuration / this.videoDuration * this.maxCropWidth } }
//左侧手势更新逻辑 leftGestureLogic(event: GestureEvent) { if (event && this.updateLOffset >= 0 && this.updateLOffset <= this.maxCropWidth) { //中间位移 this.updateLOffset = this.endLOffset + event.offsetX console.log('updateL' + this.updateLOffset) } if (this.updateLOffset <= 0) { this.updateLOffset = 0 } if (this.updateLOffset >= this.updateROffset - this.minSpace) { this.updateLOffset = this.updateROffset - this.minSpace } this.lineWidth = Math.abs(this.dragOffsetRx - this.updateLOffset) >= this.maxCropWidth ? this.maxCropWidth : Math.abs(this.dragOffsetRx - this.updateLOffset) }
//右侧手势更新逻辑 rightGestureLogic(event: GestureEvent) { if (event && this.updateROffset >= 0 && this.updateROffset <= this.maxCropWidth) { this.updateROffset = this.endROffset + event.offsetX console.log('updateR' + this.updateROffset) } if (this.updateROffset <= 0) { this.updateROffset = 0 } if (this.updateROffset <= this.minSpace + this.updateLOffset) { this.updateROffset = this.minSpace + this.updateLOffset } this.dragOffsetRx = this.updateROffset this.lineWidth = Math.abs(this.updateROffset - this.updateLOffset) >= this.maxCropWidth ? this.maxCropWidth : Math.abs(this.updateROffset - this.updateLOffset) }
leftEndLogic(event: GestureEvent) { this.endLOffset = this.updateLOffset this.progress = this.endLOffset this.startTime = this.endLOffset / this.maxCropWidth * this.videoDuration this.onLPanActionUpdate(this.startTime, this.endTime) console.log('endLL' + this.endLOffset) }
rightEndLogic(event: GestureEvent) { this.endROffset = this.updateROffset this.progress = this.endROffset this.endTime = this.endROffset / this.maxCropWidth * this.videoDuration this.onRPanActionUpdate(this.startTime, this.endTime) console.log('endRR' + this.endROffset) }
//上下边框线 @Builder TBBorderBuilder() { Column() { //状态改变 Row().width(this.lineWidth).height(3).backgroundColor('#FF5A57') Row().width(this.lineWidth).height(3).backgroundColor('#FF5A57') } .height(52) .position({ x: this.updateLOffset + this.frameWidth }) .width(this.lineWidth) .justifyContent(FlexAlign.SpaceBetween) }
//进度条 @Builder ProgressBuilder() { Row() .width(4) .height(60) .backgroundColor(Color.White) .borderRadius(2) .position({ x: this.progress + this.frameWidth, y: -3 }) .zIndex(1) }
//左边边框 @Builder LRBorderBuilder() { Row() { //左边手势 Column() { Text().width(1).height(11).backgroundColor('#FFFFFF') } .width(this.frameWidth) .height(52) .borderRadius({ topLeft: 6, bottomLeft: 6 }) .backgroundColor('#FF5A57') .justifyContent(FlexAlign.Center) .offset({ x: this.updateLOffset }) .gesture( PanGesture(this.LPanOption) .onActionUpdate((event: GestureEvent) => { this.leftGestureLogic(event) }) .onActionEnd((event: GestureEvent) => { this.leftEndLogic(event) }) )
//右边手势 Column() { Text().width(1).height(11).backgroundColor('#FFFFFF') } .width(this.frameWidth) .height(52) .borderRadius({ topRight: 6, bottomRight: 6 }) .backgroundColor('#FF5A57') .justifyContent(FlexAlign.Center) .offset({ x: this.updateROffset }) .gesture( PanGesture(this.RPanOption) .onActionUpdate((event: GestureEvent) => { this.rightGestureLogic(event) }) .onActionEnd((event: GestureEvent) => { this.rightEndLogic(event) }) ) } .width('100%') }
build() { Stack({ alignContent: Alignment.Center }) { this.TBBorderBuilder() this.imageBuilder() this.ProgressBuilder() this.LRBorderBuilder() } .width(290) .height(52) }}
评论