组件透明度变化 HarmonyOS 鸿蒙Next

组件透明度变化 HarmonyOS 鸿蒙Next 【设备信息】Mate60

【API版本】Api13

【DevEco Studio版本】5.0.7.200

【问题描述】

怎么实现组件透明度变化,根据滑动距离设置不同透明度

2 回复

可以使用opacity设置透明度,参考链接如下:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-opacity-V5#示例

动态设置透明度,参考简易demo

@Entry
@Component
struct Index{
  @State opacityNumber:number = 0
  build(){
    Stack(){ Column(){
    }
    .width('100%')
    .height('100%')
    .backgroundColor(Color.Red)
    .opacity(this.opacityNumber)
      Column(){
        Button('背景色加深')
          .onClick(()=>{ this.opacityNumber+=1 })
        Button('背景色变浅')
          .onClick(()=>{ this.opacityNumber-=1
          })
      }
    }
  }
}

更多关于组件透明度变化 HarmonyOS 鸿蒙Next的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,组件透明度的变化可以通过设置alpha属性来实现。alpha属性用于控制组件的透明度,取值范围为0到1,其中0表示完全透明,1表示完全不透明。开发者可以通过在布局文件中直接设置ohos:alpha="0.5"来设置组件的透明度为50%。在代码中,可以通过调用组件的setAlpha(float alpha)方法来动态调整透明度。这种机制适用于所有继承自Component的UI组件,如TextButton等。

回到顶部