实现滚动展示一段单行文本两端透明度渐变效果

滚动展示一段单行文本实现文字内容滚动消失的平滑过渡,有些场景需要两端都实现透明度渐变,或者是其中一边(左边或者右边)实现透明度渐变,实现文字内容出现和消失的平滑过渡。

HarmonyOS
2024-05-26 15:08:02
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
dickhome

使用的核心API

核心代码解释

使用linearGradient + BlendMode.SOURCE_IN 实现以上层图像的透明度作为权重,将其与下层图像的颜色值进行混合,以达到透明度渐变的效果。

@Entry 
@Component 
struct BlendModeDemo { 
  @State start: boolean = false 
  private fromStart: boolean = true 
  private step: number = 10 
  private loop: number = Number.POSITIVE_INFINITY 
  private src: string = "恭祝黄晓明和Angelababy喜得贵子" 
  
  build() { 
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
      Marquee({ 
        start: this.start, 
        step: this.step, 
        loop: this.loop, 
        fromStart: this.fromStart, 
        src: this.src 
      }) 
        .overlay('两边透明渐变', { 
          align: Alignment.Bottom, 
          offset: { x: 0, y: -35 } 
        }) 
        .width('90%') 
        .fontColor('#000000') 
        .fontSize(30) 
        .fontWeight(700) 
        .linearGradient({ 
          angle: 90, 
          // rgba(0, 0, 0, 0) 表示一种完全透明的颜色,其中最后一个参数 alpha(透明度)值为 0,表示该颜色是完全透明的 
          colors: [['rgba(0, 0, 0, 0)', 0], ['rgba(0, 0, 0, 1)', 0.2], ['rgba(0, 0, 0, 1)', 0.8], ['rgba(0, 0, 0, 0)', 1]] 
        }) 
        .blendMode(BlendMode.SOURCE_IN) 
        .margin({ bottom: 40 }) 
  
  
      Marquee({ 
        start: this.start, 
        step: this.step, 
        loop: this.loop, 
        fromStart: this.fromStart, 
        src: this.src 
      }) 
        .overlay('右边透明渐变', { 
          align: Alignment.Bottom, 
          offset: { x: 0, y: -35 } 
        }) 
        .width('90%') 
        .fontColor('#000000') 
        .fontSize(30) 
        .fontWeight(700) 
        .linearGradient({ 
          angle: 90, 
          colors: [['rgba(0, 0, 0, 1)', 0], ['rgba(0, 0, 0, 1)', 0.2], ['rgba(0, 0, 0, 1)', 0.8], ['rgba(0, 0, 0, 0)', 1]] 
        }) 
        .blendMode(BlendMode.SOURCE_IN) 
        .margin({ bottom: 40 }) 
  
      Marquee({ 
        start: this.start, 
        step: this.step, 
        loop: this.loop, 
        fromStart: this.fromStart, 
        src: this.src 
      }) 
        .overlay('左边透明渐变', { 
          align: Alignment.Bottom, 
          offset: { x: 0, y: -35 } 
        }) 
        .width('90%') 
        .fontColor('#000000') 
        .fontSize(30) 
        .fontWeight(700) 
        .linearGradient({ 
          angle: 90, 
          colors: [['rgba(0, 0, 0, 0)', 0], ['rgba(0, 0, 0, 1)', 0.2], ['rgba(0, 0, 0, 1)', 0.8], ['rgba(0, 0, 0, 1)', 1]] 
        }) 
        .blendMode(BlendMode.SOURCE_IN) 
        .margin({ bottom: 40 }) 
  
      Button('开始/结束') 
        .onClick(() => { 
          this.start = !this.start 
        }) 
    } 
    .width('100%') 
    .height('100%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
分享
微博
QQ
微信
回复
2024-05-27 20:07:18
相关问题
HarmonyOS 如何实现透明度渐变效果
702浏览 • 1回复 待解决
List组件如何设置两端渐变效果
2446浏览 • 1回复 待解决
如何在list组件中实现两端渐变
1042浏览 • 1回复 待解决
Text如何实现文本段落的两端对齐?
936浏览 • 1回复 待解决
SideBarContainer如何设置透明度
2984浏览 • 1回复 待解决
自定义颜色透明度如何实现
991浏览 • 1回复 待解决
HarmonyOS Text两端对齐未实现
911浏览 • 1回复 待解决
HarmonyOS 如何设置背景透明度
681浏览 • 1回复 待解决
HarmonyOS 如何设置颜色透明度
1651浏览 • 1回复 待解决
HarmonyOS color颜色怎么指定透明度
1687浏览 • 1回复 待解决
设置子窗口透明度未生效
2227浏览 • 1回复 待解决
HarmonyOS 如何实现滚动渐变效果
1062浏览 • 1回复 待解决
HarmonyOS 有没有设置颜色透明度的方法
1546浏览 • 1回复 待解决