HarmonyOS鸿蒙Next中换成model后动画放大实现不了

HarmonyOS鸿蒙Next中换成model后动画放大实现不了

import curves from ‘@ohos.curves’; @Entry @Component struct AddFrequentlyUsedIndex { @State private arr: titleStrModel[] = [] @State dragItem: titleStrModel = new titleStrModel() @State scaleItem: titleStrModel = new titleStrModel() @State neighborItem: titleStrModel = new titleStrModel() @State neighborScale: number = -1 private dragRefOffset: number = 0 @State offsetX: number = 0 @State offsetY: number = 0 private ITEM_INTV: number = 120 aboutToAppear(): void { const d = new titleStrModel() d.name = ‘wewewe’ const b = new titleStrModel() b.name = ‘adadad’ const h = new titleStrModel() h.name = ‘dfdfdf’ this.arr.push(d) this.arr.push(b) this.arr.push(h) } scaleSelect(item: titleStrModel): number { if (this.scaleItem == item) { return 1.05 } else if (this.neighborItem == item) { return this.neighborScale } else { return 1 } } itemMove(index: number, newIndex: number): void { let tmp = this.arr.splice(index, 1) this.arr.splice(newIndex, 0, tmp[0]) } build() { Stack() { List({ space: 20, initialIndex: 0 }) { ForEach(this.arr, (item: titleStrModel) => { ListItem() { Row() { Text(’’ + item.name) .width(‘100%’) .height(100) .fontSize(16) .textAlign(TextAlign.Center) .borderRadius(10) .backgroundColor(0xFFFFFF)

          Image($r('app.media.qgb_warn_icon'))
            .width(100)
            .height(100)
        }
        .shadow(this.scaleItem.name == item.name ? { radius: 70, color: '#15000000', offsetX: 0, offsetY: 0 } :
          { radius: 0, color: '#15000000', offsetX: 0, offsetY: 0 })
        .animation({ curve: Curve.Sharp, duration: 300 })
      }
      .margin({ left: 12, right: 12 })
      .scale({ x: this.scaleSelect(item), y: this.scaleSelect(item) })
      .zIndex(this.dragItem.name == item.name ? 1 : 0)
      .translate(this.dragItem.name == item.name ? { y: this.offsetY } : { y: 0 })
      .gesture(
        // 以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件
        GestureGroup(GestureMode.Sequence,
          LongPressGesture({ repeat: true })
            .onAction((event?: GestureEvent) => {
              animateTo({ curve: Curve.Friction, duration: 300 }, () => {
                this.scaleItem = item
              })
            })
            .onActionEnd(() => {
              animateTo({ curve: Curve.Friction, duration: 300 }, () => {
                this.scaleItem = new titleStrModel()
              })
            }),
          PanGesture({ fingers: 1, direction: null, distance: 0 })
            .onActionStart(() => {
              this.dragItem = item
              this.dragRefOffset = 0
            })
            .onActionUpdate((event: GestureEvent) => {
              this.offsetY = event.offsetY - this.dragRefOffset
              // console.log('Y:' + this.offsetY.toString())
              this.neighborItem = new titleStrModel()
              let index = this.arr.indexOf(item)
              let curveValue = curves.initCurve(Curve.Sharp)
              let value: number = 0
              //根据位移计算相邻项的缩放
              if (this.offsetY < 0) {
                value = curveValue.interpolate(-this.offsetY / this.ITEM_INTV)
                this.neighborItem = this.arr[index-1]
                this.neighborScale = 1 - value / 20;
                console.log('neighborScale:' + this.neighborScale.toString())
              } else if (this.offsetY > 0) {
                value = curveValue.interpolate(this.offsetY / this.ITEM_INTV)
                this.neighborItem = this.arr[index+1]
                this.neighborScale = 1 - value / 20;
              }
              //根据位移交换排序
              if (this.offsetY > this.ITEM_INTV / 2) {
                animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                  this.offsetY -= this.ITEM_INTV
                  this.dragRefOffset += this.ITEM_INTV
                  this.itemMove(index, index + 1)
                })
              } else if (this.offsetY < -this.ITEM_INTV / 2) {
                animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                  this.offsetY += this.ITEM_INTV
                  this.dragRefOffset -= this.ITEM_INTV
                  this.itemMove(index, index - 1)
                })
              }
            })
            .onActionEnd((event: GestureEvent) => {
              animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                this.dragItem = new titleStrModel()
                this.neighborItem = new titleStrModel()
              })
              animateTo({
                curve: curves.interpolatingSpring(14, 1, 170, 17), delay: 150
              }, () => {
                this.scaleItem = new titleStrModel()
              })
            })
        )
          .onCancel(() => {
            animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
              this.dragItem = new titleStrModel()
              this.neighborItem = new titleStrModel()
            })
            animateTo({
              curve: curves.interpolatingSpring(14, 1, 170, 17), delay: 150
            }, () => {
              this.scaleItem = new titleStrModel()
            })
          })
      )
    })
  }
}
.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })

} }

class titleStrModel { name:string = ‘’ }


更多关于HarmonyOS鸿蒙Next中换成model后动画放大实现不了的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

listitem内容为object,不能直接用 ==判断当前item,可以改用index确定当前点击的item,修改如下:(其他案例参考:https://gitee.com/harmonyos-cases/cases/tree/master/CommonAppDevelopment/feature/listexchange

import curves from '@ohos.curves';

@Entry
@Component
struct model {
  @State private arr: titleStrModel[] = []
  @State dragItem: titleStrModel = new titleStrModel()
  @State scaleItem: titleStrModel = new titleStrModel()
  @State neighborItem: titleStrModel = new titleStrModel()
  @State selectedIndex: number = -1
  @State neighborScale: number = -1
  private dragRefOffset: number = 0
  @State offsetX: number = 0
  @State offsetY: number = 0
  private ITEM_INTV: number = 120

  aboutToAppear(): void {
    const d = new titleStrModel()
    d.name = 'wewewe'
    const b = new titleStrModel()
    b.name = 'adadad'
    const h = new titleStrModel()
    h.name = 'dfdfdf'
    this.arr.push(d)
    this.arr.push(b)
    this.arr.push(h)
  }

  //根据当前index确定item是否缩放
  scaleSelect(item: titleStrModel): void {
    this.selectedIndex = this.arr.indexOf(item)
    this.arr[this.selectedIndex].scale = 1.05
  }

  itemMove(index: number, newIndex: number): void {
    let tmp = this.arr.splice(index, 1)
    this.arr.splice(newIndex, 0, tmp[0])
  }

  build() {
    Stack() {
      List({ space: 20, initialIndex: 0 }) {
        ForEach(this.arr, (item: titleStrModel) => {
          ListItem() {
            Row() {
              Text('' + item.name)
                .width('100%')
                .height(100)
                .fontSize(16)
                .textAlign(TextAlign.Center)
                .borderRadius(10)
                .backgroundColor(0xFFFFFF)
              Image($r('app.media.app_icon'))
                .width(100)
                .height(100)
            }
            .shadow(this.scaleItem.name == item.name ? {
              radius: 70,
              color: Color.Blue,
              offsetX: 0,
              offsetY: 0
            } :
              {
                radius: 0,
                color: Color.Blue,
                offsetX: 0,
                offsetY: 0
              })
            .animation({ curve: Curve.Sharp, duration: 300 })
          }
          .margin({ left: 12, right: 12 })
          .scale({ x: item.scale, y: item.scale })
          .zIndex(this.dragItem.name == item.name ? 1 : 0)
          .translate(this.dragItem.name == item.name ? { y: this.offsetY } : { y: 0 })
          .gesture(
            //以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件
            GestureGroup(GestureMode.Sequence,
              LongPressGesture({ repeat: true })
                .onAction((event?: GestureEvent) => {
                  animateTo({ curve: Curve.Friction, duration: 300 }, () => {
                    this.scaleItem = item
                    this.scaleSelect(item)
                  })
                })
                .onActionEnd(() => {
                  animateTo({ curve: Curve.Friction, duration: 300 }, () => {
                    this.scaleItem = new titleStrModel()
                  })
                }),
              PanGesture({ fingers: 1, direction: null, distance: 0 })
                .onActionStart(() => {
                  this.dragItem = item
                  this.dragRefOffset = 0
                })
                .onActionUpdate((event: GestureEvent) => {
                  this.offsetY = event.offsetY - this.dragRefOffset
                  // console.log('Y:' + this.offsetY.toString())
                  this.neighborItem = new titleStrModel()
                  let index = this.arr.indexOf(item)
                  let curveValue = curves.initCurve(Curve.Sharp)
                  let value: number = 0
                  //根据位移计算相邻项的缩放
                  if (this.offsetY < 0) {
                    value = curveValue.interpolate(-this.offsetY / this.ITEM_INTV)
                    this.neighborItem = this.arr[index-1]
                    this.neighborScale = 1 - value / 20;
                    console.log('neighborScale:' + this.neighborScale.toString())
                  } else if (this.offsetY > 0) {
                    value = curveValue.interpolate(this.offsetY / this.ITEM_INTV)
                    this.neighborItem = this.arr[index+1]
                    this.neighborScale = 1 - value / 20;
                  }
                  //根据位移交换排序
                  if (this.offsetY > this.ITEM_INTV / 2) {
                    animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                      this.offsetY -= this.ITEM_INTV
                      this.dragRefOffset += this.ITEM_INTV
                      this.itemMove(index, index + 1)
                    })
                  } else if (this.offsetY < -this.ITEM_INTV / 2) {
                    animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                      this.offsetY += this.ITEM_INTV
                      this.dragRefOffset -= this.ITEM_INTV
                      this.itemMove(index, index - 1)
                    })
                  }
                })
                .onActionEnd((event: GestureEvent) => {
                  animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                    this.dragItem = new titleStrModel()
                    this.neighborItem = new titleStrModel()
                    //动画结束将缩放还原
                    this.arr[this.arr.indexOf(this.scaleItem)].scale = 1
                  })
                  animateTo({
                    curve: curves.interpolatingSpring(14, 1, 170, 17),
                    delay: 150
                  }, () => {
                    this.scaleItem = new titleStrModel()
                  })
                })
            )
              .onCancel(() => {
                animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                  this.dragItem = new titleStrModel()
                  this.neighborItem = new titleStrModel()
                })
                animateTo({
                  curve: curves.interpolatingSpring(14, 1, 170, 17),
                  delay: 150
                }, () => {
                  this.scaleItem = new titleStrModel()
                })
              })
          )
        })
      }
    }
    .width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
  }
}

class titleStrModel {
  name: string = ''
  scale: number = 1 //增加scale属性, 每个item都有对应的scale值
}

更多关于HarmonyOS鸿蒙Next中换成model后动画放大实现不了的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,如果换成model后动画放大无法实现,可能是由于以下几个原因:

  1. 模型配置问题:检查模型的配置文件,确保动画相关的参数正确设置,包括缩放比例、关键帧等。

  2. 资源文件缺失:确认动画所需的资源文件(如图片、纹理等)是否已正确导入,并且路径无误。

  3. 代码逻辑错误:查看与动画相关的代码逻辑,确保没有遗漏或错误,特别是在模型切换时动画状态的更新。

  4. 系统兼容性:确认使用的鸿蒙Next版本是否支持所需的动画功能,以及是否有已知的bug或限制。

  5. 硬件性能:检查设备硬件性能是否足够支持动画的执行,特别是在高分辨率或复杂模型情况下。

  6. 调试工具:使用鸿蒙提供的调试工具,如DevEco Studio,查看日志和性能分析,定位问题所在。

  7. 文档参考:查阅鸿蒙官方文档,确认是否有关于模型切换后动画实现的特定说明或限制。

通过以上步骤,可以逐步排查并解决鸿蒙Next中模型切换后动画放大无法实现的问题。

在HarmonyOS鸿蒙Next中,如果更换model后动画放大效果无法实现,可能原因包括:

  1. 动画参数未正确配置:确保在model切换时,动画的scaleXscaleY等参数已正确设置。
  2. 布局问题:检查model的布局是否支持动画效果,确保widthheight属性允许缩放。
  3. 动画监听器缺失:确认已为动画添加正确的监听器,确保动画在model切换后能够触发。

建议检查代码逻辑,确保动画相关配置与model切换同步更新。

回到顶部