HarmonyOS 在Column onAppear时,通过linearGradient和animation改变控件背景的动画

使用如下方式动画并不起作用,

Row()
  .alignItems(VerticalAlign.Center)
  .width('100%')
  .height('32lpx')
  .animatableBgColor(this.bgColor(item.type), this.animationVal)
  .onAppear(() => {
    if (index === 0) {
      animateTo({
        delay: 200, onFinish: () => {
          this.animationVal = 0;
        }
      }, () => {
        this.animationVal = 1;
      })
    }
  })



@AnimatableExtend(Row)
function animatableBgColor(colors: string[], position: number) {
  .linearGradient(position === 0 ? {
    colors: [
      ['#00000000', 0.0],
      ['#00000000', 1.0],
    ]
  } : {
    angle: 90,
    colors: [
      [colors[0], 0.0],
      [colors[1], position],
      [colors[2], 1.0],
    ]
  })
}
HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

参考demo如下:

@Entry
@Component
struct Page2 {
  @State linearGradientParam: number = 0.1
  @State animationVal: number = 1
  @State flage :boolean = false
  build() {
    Column() {
      Column() {
        Text('Test').fontSize('32lpx').fontWeight(FontWeight.Medium)
      }
      .width('228lpx')
      .height('88lpx')
      .justifyContent(FlexAlign.Center)
      .alignItems(HorizontalAlign.Center)
      // 将颜色设为透明
      .linearGradient({
        angle: '109.26deg',
        colors: [
          [this.animationVal === 0 ? Color.Transparent : (this.flage?Color.Transparent:'#008c8c'), 0.1],
          [this.animationVal === 0 ? Color.Transparent : (this.flage?Color.Transparent:'rgba(255, 255, 255, 0.4)'), 0.5],
        ]
      })
      // 变化颜色范围
      // .linearGradient({
      // angle: '109.26deg',
      // colors: [
      // ['#008c8c', this.animationVal === 0 ? 0 : 0.1],
      // [Color.Transparent, this.animationVal === 0 ? 0 : 1],
      // ]
      // })
      .onAppear(() => {
        console.log('console',this.animationVal);
        animateTo({
          duration: 2000, onFinish: () => {
            console.log("animation finish")
          }
        }, () => {
          this.animationVal = 0
        })
      })
      Button('click').onClick(() => {
        console.log('console',this.animationVal);

        console.log("aboutToAppear")
        if (this.animationVal === 1) {
          this.flage = false
          animateTo({
            duration: 2000, onFinish: () => {
              console.log("animation finish")

            }
          }, () => {
            this.animationVal = 0

          })
        } else {
          animateTo({
            duration: 2000, onFinish: () => {
              console.log("animation finish")
              this.flage = !this.flage
            }
          }, () => {
            this.animationVal = 1

          })
        }
      })
    }
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
XComponent组件中如何改变背景颜色
446浏览 • 1回复 待解决
如何实现背景跟随文字大小改变
584浏览 • 1回复 待解决