QML-动画-移动字符串

本文介绍了一个使用QML创建的可滚动文本组件,该组件能够根据文本长度自动滚动显示,并支持重置文本位置的功能。通过调整参数可以轻松地改变文本的字体大小、颜色和滚动速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//在这里新建一个QML文件,文件名为MoveableText.qml

Item {

id: wrapper
width: 120; height: text.paintedHeight
clip: true
   
property alias text: text.text
property int fontSize: 24
property int duration: 2000
property alias color: text.color
property alias font: text.font
property bool marqueeEnable: true
property real targetLeftMost: wrapper.width - text.paintedWidth
property alias xpos: text.x
property int delayScroll: 0
property bool reset: false // add this for reset the text location 
   
Text {
 id: text
 x: 0
 anchors.verticalCenter: parent.verticalCenter
 font.pixelSize: wrapper.fontSize
 clip: true
  
 // add this for reset the text location 
 SequentialAnimation on x {
 running: wrapper.reset
 loops: 1
 NumberAnimation { to: 0; duration: 0 }
 }
  
   
 SequentialAnimation on x {
 running: wrapper.marqueeEnable && (text.paintedWidth > wrapper.width)
 loops: Animation.Infinite 
   
 PauseAnimation { duration: delayScroll }
 NumberAnimation { to: wrapper.targetLeftMost; duration: wrapper.duration }
 PauseAnimation { duration: 500 }
 NumberAnimation { to: 0; duration: 0 }
 PauseAnimation { duration: 500 }
 }
   
   
   
}
}
  
  
// test code
Item { 
width: 360 
height: 360 
focus:true 
   
property int flag: 0 
   
MarqueeText { 
 anchors.centerIn: parent 
 text: "Hello World......" 
 NumberAnimation on x { to: 50; duration: 1000 } 
 marqueeEnable:(flag == 0) ? true : false; 
 reset:(marqueeEnable == true) ? false : true; 
} 
   
MouseArea { 
 anchors.fill: parent 
 onClicked: { 
 if (flag == 1) 
 { 
 flag = 0; 
 } 
 else 
 { 
 flag = 1; 
 } 
 } 
} 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值