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

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

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%') 
  } 
}
分享
微博
QQ
微信
回复
2024-05-27 20:07:18
相关问题
List组件如何设置两端渐变效果
660浏览 • 1回复 待解决
SideBarContainer如何设置透明度
817浏览 • 1回复 待解决
设置子窗口透明度未生效
521浏览 • 1回复 待解决
Scroll初始时自动滚动一段距离
338浏览 • 1回复 待解决
鸿蒙-如何实现播放一段音频
9636浏览 • 2回复 待解决
如何实现RSA的公钥PK加密一段文字
139浏览 • 1回复 待解决
是否可以实现滚动锚定的效果
382浏览 • 1回复 待解决
如何设置组件透明效果
541浏览 • 1回复 待解决
行文本省略的展开与显示
365浏览 • 1回复 待解决