HarmonyOS NEXT练习:跑马灯

Marquee跑马灯组件,用于滚动展示一段单行文本。仅当文本内容宽度超过跑马灯组件宽度时滚动,不超过时不滚动。

接口

typescript 复制代码
Marquee(value: { start: boolean, step?: number, loop?: number, fromStart?: boolean, src: string })

value:配置跑马灯组件的参数。

  • start:控制跑马灯是否进入播放状态。
  • step:滚动动画文本滚动步长,当step大于Marquee的文本宽度时,取默认值。默认值:6,单位vp
  • loop:设置重复滚动的次数,小于等于零时无限循环。默认值:-1
  • fromStart:设置文本从头开始滚动或反向滚动。默认值:true
  • src:需要滚动的文本。

组件属性:

typescript 复制代码
.fontColor(value: ResourceColor) //设置字体颜色。
.fontSize(value: Length) //设置字体大小。
.fontWeight(value: number | FontWeight | string) //设置文本的字体粗细,设置过大可能会在不同字体下有截断。默认值:FontWeight.Normal
.fontFamily(value: string | Resource) //设置字体列表。默认字体'HarmonyOS Sans'。
.allowScale(value: boolean) //设置是否允许文本缩放。默认值:false
.marqueeUpdateStrategy(value: MarqueeUpdateStrategy) //跑马灯组件属性更新后,跑马灯的滚动策略。(当跑马灯为播放状态,且文本内容宽度超过跑马灯组件宽度时,该属性生效)。默认值: MarqueeUpdateStrategy.DEFAULT

组件事件:

typescript 复制代码
.onStart(event: () => void) //当滚动的文本内容变化或者开始滚动时触发回调。
.onBounce(event: () => void) //完成一次滚动时触发,若循环次数不为1,则该事件会多次触发。
.onFinish(event: () => void) //滚动全部循环次数完成时触发回调。

跑马灯练习Demo:

typescript 复制代码
@Entry
@Component
struct MarqueePage {
  @State start: boolean = true;
  @State marqueeText: string = '跑马灯练习:当文字超出一屏幕宽时,通过跑马灯滚动展示文字...';
  private fromStart: boolean = true;
  private step: number = 10;
  private loop: number = Number.POSITIVE_INFINITY;

  build() {
    Column({ space: 20 }) {
      Text('跑马灯练习')
      Marquee({
        start: this.start,
        step: this.step,
        loop: this.loop,
        fromStart: this.fromStart,
        src: this.marqueeText
      })
        .marqueeUpdateStrategy(MarqueeUpdateStrategy.PRESERVE_POSITION)
        .width('100%')
        .height('80')
        .fontColor('#FFFFFF')
        .fontSize('48fp')
        .fontWeight(700)
        .backgroundColor('#182431')
        .margin({ bottom: '40vp' })
        .onStart(() => {
          console.info('Succeeded in completing the onStart callback of marquee animation');
        })
        .onBounce(() => {
          console.info('Succeeded in completing the onBounce callback of marquee animation');
        })
        .onFinish(() => {
          console.info('Succeeded in completing the onFinish callback of marquee animation');
        })
    }
    .width('100%')
    .height('100%')
  }
}
相关推荐
御承扬2 小时前
鸿蒙NDK UI之文本自定义样式
ui·华为·harmonyos·鸿蒙ndk ui
前端不太难2 小时前
HarmonyOS 游戏上线前必做的 7 类极端场景测试
游戏·状态模式·harmonyos
大雷神3 小时前
HarmonyOS智慧农业管理应用开发教程--高高种地--第29篇:数据管理与备份
华为·harmonyos
讯方洋哥3 小时前
HarmonyOS App开发——关系型数据库应用App开发
数据库·harmonyos
巴德鸟4 小时前
华为手机鸿蒙4回退到鸿蒙3到鸿蒙2再回退到EMUI11 最后关闭系统更新
华为·智能手机·harmonyos·降级·升级·回退·emui
一起养小猫4 小时前
Flutter for OpenHarmony 实战_魔方应用UI设计与交互优化
flutter·ui·交互·harmonyos
一只大侠的侠5 小时前
Flutter开源鸿蒙跨平台训练营 Day7Flutter+ArkTS双方案实现轮播图+搜索框+导航组件
flutter·开源·harmonyos
一只大侠的侠6 小时前
Flutter开源鸿蒙跨平台训练营 Day9分类数据的获取与渲染实现
flutter·开源·harmonyos
一只大侠的侠6 小时前
Flutter开源鸿蒙跨平台训练营 Day 5Flutter开发鸿蒙电商应用
flutter·开源·harmonyos
不爱吃糖的程序媛7 小时前
Capacitor:跨平台Web原生应用开发利器,现已全面适配鸿蒙
前端·华为·harmonyos