我需要在屏幕上从右到左连续创建移动文本,我已经使用它实现了QML
Timer
and Text
元素。
下面的代码工作正常,但我担心下面的代码会导致更多的 cpu 或内存使用,主要是因为计时器每 33 毫秒触发一次。我必须在应用程序中的位置和多个实例中使用它,例如在许多网格窗口内。
这是正确的做法吗?或者还有比这更好的东西吗?
Rectangle{
width:parent.width
height:parent.height
color: "#333333"
Timer {
id: resetTimer
interval: 33
repeat: true
running: true
onTriggered: {
console.log("triggred");
moving_text.x = moving_text.x-1
if(moving_text.x<-1*moving_text.paintedWidth)
moving_text.x=parent.width
}
}
Text{
id:moving_text
x:parent.width
text:"Moving text"
color: "white"
}
}
为什么要把事情弄得这么复杂。你可以使用NumberAnimation
on x
如下:
import QtQuick 2.0
Rectangle{
id: root
width:250
height:250
color: "#333333"
Text{
id:moving_text
x:parent.width
text:"Moving text"
color: "white"
NumberAnimation on x{
from: root.width
to: -1*moving_text.width
loops: Animation.Infinite
duration: 3000
}
}
}
至于您关心的内存和CPU使用情况,您应该比较这两种方法并检查哪一种适合您。但我个人的建议是使用NumberAnimation
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)