文档管理中心

使用0x八位颜色设置渐变透明度为什么与#八位资源颜色值不同

HarmonyOS支持0x开头加八位或六位的写法。当透明度设为00时,前两位透明度不再借位,即0x00333333等于0x333333,相当于没有设置透明度,因此没有透明效果。建议使用rgba方式明确颜色。参考代码如下:

收起
自动换行
深色代码主题
复制
  1. @Entry
  2. @Component
  3. struct ColorGradientExample {
  4. @State transparent: number | string = '#00333333';
  5. private bool: boolean = true;
  6. build() {
  7. Column({ space: 5 }) {
  8. Text('linearGradient')
  9. .fontSize(12)
  10. .width('90%')
  11. .fontColor(0xCCCCCC)
  12. Row()
  13. .width('90%')
  14. .height(150)
  15. .linearGradient({
  16. direction: GradientDirection.Bottom,
  17. colors: [[this.transparent, 0.0], [0x80000000, 1.0]]
  18. })
  19. Button('Switch color resources')
  20. .onClick(() => {
  21. if (this.bool) {
  22. this.transparent = 0x00333333;
  23. this.bool = false;
  24. } else {
  25. this.transparent = '#00333333';
  26. this.bool = true;
  27. }
  28. })
  29. }
  30. .justifyContent(FlexAlign.Center)
  31. .width('100%')
  32. .height('100%')
  33. .padding({ top: 5 })
  34. }
  35. }

效果如图所示: