qml Animation三种实例

shyer0527 2015-06-02 10:05:37
先创建一个块,方便复用:
import QtQuick 2.0

Item {
id: root
width: container.childrenRect.width + 16
height: container.childrenRect.height + 16
property alias text: label.text
property alias source: image.source
property alias iwidth: image.width
property alias iheight: image.height
signal clicked

property bool framed : false

Rectangle {
anchors.fill: parent
color: "white"
visible: root.framed
}

Column {
x: 8; y: 8
id: container
Image {
id: image
}
Text {
id: label
width: image.width
horizontalAlignment: Text.AlignHCenter
wrapMode: Text.WordWrap
color: "#e0e0e0"
}
}

MouseArea {
anchors.fill: parent
onClicked: root.clicked()
}
}
//三种用法
//TryAnimation
import QtQuick 2.0

Image {
id: root
source: "images/bg.gif"
//用法1:图像向y方向移动,从原始位置200到40,经过4000毫秒
Tanimation{
id: rocket1
x: 40;y: 200
iwidth: 60
iheight: 60
source: "images/rocketh.png"
text: "animation on property"
NumberAnimation on y{
to : 40;duration: 4000
}
}
//用法2:在用法1的基础上增加鼠标点击事件触发图像移动,同样位置从200到40
Tanimation{
id: rocket2
x:152;y:200
source: "images/rocketh.png"
iwidth: 60
iheight: 60
text: "behavior on property"
Behavior on y {
NumberAnimation{duration: 4000}
}
onClicked: y = 40
// onClicked: y = 40+Math.random()*(200-40)
// enabled: false
}
//用法3:这种方法是最全面的,将所有的变化都放进Animation对象中,通过start()或restart()等可对图像进行重复执行
Tanimation{
id: rocket3
x:262;y:200
source: "images/rocketh.png"
iwidth: 60
iheight: 60
text: "standalone animation"
// onClicked: anim.restart()
onClicked: anim.start()
NumberAnimation{
id: anim
target: rocket3
properties: "y"
from: 200
to:40
duration: 4000
running: area.pressed
}
MouseArea{
id: area
}
}
}
//以上只是笔者学习animation中做的三个例子,并非全部用法。
...全文
276 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
彩阳 2015-08-03
  • 打赏
  • 举报
回复
Qt 5.5除了Animation这个基类,又出现了Animator这个类,而这个类和前者在实现上有所不同。

16,213

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧