Marquee组件使用问题有哪些?

使用MarQuee组件展示一行滚动文本时,我们的需求是当最后一个文字显示出来或者最后一个文字距离行尾指定距离后,文本就停止滚动。并且文本可以重新内容设置,并回到开始位置,如果文本超长就开始滚动。

HarmonyOS
2024-09-23 13:04:14
1165浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

可以使用Text组件和animation实现类似效果,参考下如下demo:

import measure from '@ohos.measure';  
  
@Entry  
@Component  
struct TextDemo {  
  string1: string = 'start|测试文本测试文本测试文本测试文本测试文本测试文本测|end'  
  string2: string ='start|测试文本2测试文本2测试文本2测试文本2测试文本2测试|end'  
  @State src: string = this.string1  
  @State textOffset: number = 0  
  @State textWidth: number = measure.measureText({  
    textContent: this.src,  
    fontWeight: FontWeight.Medium,  
  })  
  private columnWidth: number = 300;  
  
  @State finishFlag:boolean = false;  
  @State iterations:number = 1  
  
  build() {  
    Row() {  
      Column() {  
        Column() {  
          if(this.finishFlag){  
            Text(this.src)  
              .width(px2vp(this.textWidth))  
              .maxLines(1)  
              .fontWeight(FontWeight.Medium)  
              .textAlign(TextAlign.Center)  
              .offset({ x: 0 })  
              .onAppear(() => {  
                this.textOffset = 0  
                this.textWidth = measure.measureText({  
                  textContent: this.src,  
                  fontWeight: FontWeight.Medium,  
                })  
                this.finishFlag = !this.finishFlag  
              })  
          }else {  
            Text(this.src)  
              .width(px2vp(this.textWidth))  
              .maxLines(1)  
              .fontWeight(FontWeight.Medium)  
              .textAlign(TextAlign.Center)  
              .offset({ x: this.textOffset })  
              .animation({  
                duration: 5000,  
                iterations: this.iterations,  
                curve: Curve.Linear,  
                onFinish: () => {  
                  //动画播放完的回调  
                  if(this.src === this.string1){  
                    this.src = this.string2  
                  }else{  
                    this.src = this.string1  
                  }  
                  this.finishFlag = !this.finishFlag  
                }  
              })  
              .onAppear(() => {  
                this.textOffset = -px2vp(this.textWidth) + this.columnWidth  
              })  
          }  
        }  
        .alignItems(HorizontalAlign.Start)  
        .backgroundColor(Color.Pink)  
        .clip(true)  
        .width(this.columnWidth)  
      }  
      .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.
分享
微博
QQ
微信
回复
2024-09-23 17:51:51


相关问题
uiTest使用问题哪些
1191浏览 • 1回复 待解决
List使用问题哪些
1260浏览 • 1回复 待解决
Grid组件性能问题哪些
1240浏览 • 1回复 待解决
webview 问题哪些
1155浏览 • 1回复 待解决
OAID授权问题哪些
1629浏览 • 1回复 待解决
HarmonyOS Marquee组件引起点击事件冲突
675浏览 • 1回复 待解决
ArkTS生成密钥问题哪些
981浏览 • 1回复 待解决
Flutter集成问题哪些
1068浏览 • 1回复 待解决
启动页面配置问题哪些
1278浏览 • 1回复 待解决
签名证书配置问题哪些
1606浏览 • 1回复 待解决
bindPopup样式问题哪些啊?
946浏览 • 1回复 待解决
Tab控件切换问题哪些
1214浏览 • 1回复 待解决
背景属性的问题哪些
1095浏览 • 1回复 待解决
系统Tabs组件用法哪些
1356浏览 • 1回复 待解决
WaterFlow的使用范例哪些
1871浏览 • 1回复 待解决
LazyForEach的使用限制哪些
1470浏览 • 1回复 待解决
Tabs滑动距离的问题哪些
1356浏览 • 1回复 待解决
静态工程引用har问题哪些
1249浏览 • 1回复 待解决
Flutter刷新率问题哪些
1729浏览 • 1回复 待解决
组件复用生效的条件哪些
924浏览 • 1回复 待解决
公共事件哪些简单使用
1769浏览 • 1回复 待解决
使用具体的类型哪些
1166浏览 • 1回复 待解决
应用性能问题优化方式哪些
1662浏览 • 1回复 待解决