qml文章 qt
qml中PropertyAnimation的几种使用方法

动画应用场景有以下几种:

首先如果一个Rectangle。动画是要改变它的x和y值

1,Rectangle一旦被创建,就要移动到一个特定的位置

2,动画仅仅有在某一个特定的外部行为触发时候才会被触发,比如,鼠标单击某一个控件时候,产生动画。使目标移动到指定的位置

3,仅仅有在某一个特定的信号后才触发

4,做为一个独立的动画,尽管没有绑定rectangle的运动,可是能够在脚本中载入,開始和停止

5。仅仅有在状态改变时候才会触发

6,仅仅有在某一个属性改变时候才触发,不管这个属性是通过什么样的方法来改变的

7,在一个特定的信号处理器中创建,当接收到相应的信号时候就触发,类似于3

以下分别用代码来看几种实现方法:

【1】首先是对第一种场景

  Rectangle{
color:"red"
width:360
height:50 PropertyAnimation on x{to: 50 ;duration:1000; loops:Animation.Infinite }
PropertyAnimation on y{to: 250 ;duration:1000; loops:Animation.Infinite }
}

Rectangle一旦被创建,就立马从(0,0)坐标移动到(50。250),在一秒时间内

【2】另外一种场景代码。行为动画,在某一个属性值发生变化时候触发

    Rectangle{
color:"red"
width:360
height:50
id:rect
Behavior on x {
PropertyAnimation{ duration : 1000 }
} Behavior on y {
PropertyAnimation{ duration : 1000 }
} } MouseArea{
anchors.fill: parent
onClicked:{
rect.x=mouse.x;
rect.y=mouse.y;
}
}

这段代码实现了,在点击了屏幕上的一点后,rect会在一秒的时间内触发动画。到达鼠标所点击的位置。由于在onClicked里面,我们改动了rect的x和y值。

【3】在信号处理器中触发动画

   Rectangle{
color:"red"
width:360
height:50
id:rect MouseArea{
anchors.fill: parent
onClicked: PropertyAnimation{
target:rect ; properties:"y"
to:250
duration:1000
} }
}

当点击rect的时候,就会触发动画。使rect的y从0运动到250

【4】动画作为一个独立的动画,能够像创建普通的QML对象一样创建,而不须要绑定特定的对象和属性。

 Rectangle{
color:"red"
width:360
height:50
id:rect PropertyAnimation{
id:animation
target:rect
properties: "width"
duration: 1000 } MouseArea{
anchors.fill: parent
onClicked: {
animation.to=50
animation.running=true;
}
} }

一个独立的动画对象默认是没有执行的,必须使用running属性,或者start() stop()来执行它。

【5】切换。切换用来设置当状态发生改变时候。须要创建一个切换,Transition对象。然后把它加入到对象的transition属性以下,代码

 Rectangle{
Rectangle{
color:"gray"
y:100
width:360
height:80
id:rect1
} //切换状态
Rectangle{
color:"steelblue"
width:360
height:80
id:rect MouseArea{
anchors.fill: parent
onClicked: {
console.log("dddd")
rect.state="move"
rect1.height=50
rect1.state="move"
}
} states:[
State{
name:"move"
PropertyChanges{
target:rect
y:250
}
PropertyChanges{
target:rect1
y:330
}
} ] transitions: [
Transition {
PropertyAnimation{
properties: "y"
duration:5000
}
}
] }
}

当点击rect的时候,rect和rect1的状态切换到move状态。move状态中的两个PropertyChanges对象定义了rect和rect1的属性改变值。这时候Transition会自己主动被运行,Transition里面的PropertyAnimation对象会自己主动将rect和rect1的属性值y切换到相应的值,这里并没有设置from和to值,在状态開始和结束的时候已经设置了他们的值。

另外propertyAnimation并不须要指定target属性,这样不论什么对象的y值在状态切换时候都会使用这个动画。只是也能够指定一个target来使用这个动画,另外在Transition里面的东辉会并行运行,假设要串行运行,能够使用SequentiaAnimation.这个代码也能够这样来写:

Rectangle{
Rectangle{
color:"gray"
y:100
width:360
height:80
id:rect1
} //切换状态
Rectangle{
color:"steelblue"
width:360
height:80
id:rect MouseArea{
anchors.fill: parent
onClicked: {
console.log("dddd")
rect.state="move"
rect1.height=50
rect1.state="move"
}
} states:[
State{
name:"move"
} ] transitions: [
Transition {
PropertyAnimation{
target:rect
from:0
to:250
properties: "y"
duration:5000
} PropertyAnimation{
target:rect1
properties: "y"
from:100
to:330
duration:2000
}
}
] }
}

[6]属性动画元素

PropertyAnimation元素是用来为属性提供动画最基本动画元素。他能够为real ,int ,color,rect,point,sized,vector3d来提供动画设置。它能够被NumberAnimation,ColorAnimation,RotationAnimation,Vector3dAnimation等继承,他们分别提供了更高效的属性动画实现方式。

而且不论什么基于PropertyAnimation的对象都能够设置easing属性来动画中使用的缓和曲线。

比如:

 Rectangle{
color:"gray"
y:100
width:360
height:80
id:rect1 ColorAnimation on color { from: "white"; to: "red"; duration: 5000 }
RotationAnimation on rotation{
from:0
to:360
direction: RotationAnimation.Clockwise
duration:5000
}
}

以下是代码总体合起来和执行效果:

import QtQuick 2.2
import QtQuick.Controls 1.1 ApplicationWindow {
visible: true
width: 360
height: 480
title: qsTr("Hello World") menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
} Rectangle{
Rectangle{
color:"gray"
y:100
width:360
height:80
id:rect1 ColorAnimation on color { from: "white"; to: "red"; duration: 5000 }
RotationAnimation on rotation{
from:0
to:360
direction: RotationAnimation.Clockwise
duration:5000
}
} //切换状态
Rectangle{
color:"steelblue"
width:360
height:80
id:rect MouseArea{
anchors.fill: parent
onClicked: {
console.log("dddd")
rect.state="move"
rect1.height=50
rect1.state="move"
}
} states:[
State{
name:"move"
// PropertyChanges{
// target:rect
// y:250
// }
// PropertyChanges{
// target:rect1
// y:330
// } } ] transitions: [
Transition {
PropertyAnimation{
target:rect
from:0
to:250
properties: "y"
duration:5000
easing.type: Easing.OutBounce
} PropertyAnimation{
target:rect1
properties: "y"
from:100
to:330
duration:2000
easing.type: Easing.OutBounce
}
}
] }
} /*
//初始化就触发的动画
Rectangle{
color:"red"
width:360
height:50 PropertyAnimation on x{to: 50 ;duration:1000; loops:Animation.Infinite }
PropertyAnimation on y{to: 250 ;duration:1000; loops:Animation.Infinite }
}
*/ /*
Rectangle{
color:"red"
width:360
height:50
id:rect
Behavior on x {
PropertyAnimation{ duration : 1000 }
} Behavior on y {
PropertyAnimation{ duration : 1000 }
} } MouseArea{
anchors.fill: parent
onClicked:{
rect.x=mouse.x;
rect.y=mouse.y;
}
} */
/*
Rectangle{
color:"red"
width:360
height:50
id:rect MouseArea{
anchors.fill: parent
onClicked:
PropertyAnimation{
target:rect ; properties:"y"
to:250
duration:1000
} }
}
*/
/*
Column{
Rectangle{
color:"blue"
width:360
height:50
TextInput{
anchors.fill: parent
}
} Rectangle{
color:"red"
width:360
height:50
id:rect PropertyAnimation{
id:animation
target:rect
properties: "width"
duration: 1000 } MouseArea{
anchors.fill: parent
onClicked: {
animation.to=50
animation.running=true;
}
} }
}
*/ Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
}

  

红色的巨型首先经过一个360旋转和变色。然后点击蓝色的巨型,就会像弹簧一样落下来。

刚刚提到Transition中的组合动画,ParalleAnimation和SequentialAnimation分别提供并行和串行的动画表现方案。

查看很多其它qml文章 qt
qml中PropertyAnimation的几种使用方法

qt qml中PropertyAnimation的几种使用方法的更多相关文章

  1. Jquery中each的三种遍历方法

    Jquery中each的三种遍历方法 $.post("urladdr", { "data" : "data" }, function(dat ...

  2. JS 中深拷贝的几种实现方法

    JS 中深拷贝的几种实现方法1.使用递归的方式实现深拷贝 //使用递归的方式实现数组.对象的深拷贝 function deepClone1(obj) { //判断拷贝的要进行深拷贝的是数组还是对象,是 ...

  3. stl中map的四种插入方法总结

    stl中map的四种插入方法总结方法一:pair例:map<int, string> mp;mp.insert(pair<int,string>(1,"aaaaa&q ...

  4. python中常用的九种预处理方法

    本文总结的是我们大家在python中常见的数据预处理方法,以下通过sklearn的preprocessing模块来介绍; 1. 标准化(Standardization or Mean Removal ...

  5. Qt Creator 中的段落 注释的 快捷方法【转载】

    原文网址:http://jingyan.baidu.com/article/d5c4b52bc2bd1dda560dc5bb.html 作为一名合格的程序员,漂漂亮亮的注释是必须的!!怎么在Qt Cr ...

  6. Android中定时执行任务的3种实现方法

    在Android开发中,定时执行任务的3种实现方法: 一.采用Handler与线程的sleep(long)方法(不建议使用,java的实现方式)二.采用Handler的postDelayed(Runn ...

  7. Android中定时器的3种实现方法

    原文:http://blog.csdn.net/wulianghuan/article/details/8507221 在Android开发中,定时器一般有以下3种实现方法: 一.采用Handler与 ...

  8. 【转】Android中定时器的3种实现方法

    原文网址:http://www.android-study.com/pingtaikaifa/508.html 在Android开发中,定时器一般有以下3种实现方法: 一.采用Handler与线程的s ...

  9. js中this的四种使用方法

    0x00:js中this的四种调用模式 1,方法调用模式 2,函数调用模式 3,构造器调用模式 4,apply.call.bind调用模式 0x01:第一种:方法调用模式 (也就是用.调用的)this ...

随机推荐

  1. python 求值表达式解析

    采用中缀转后缀的算法. 注意我输入的格式. #注意格式 def suffix(st): listopt=[" "] listnum=[" "] for i in ...

  2. codeforces 487E Tourists

    如果不是uoj上有的话(听说这是China Round),我有可能就错过这道题目了(这是我有史以来为oi写的最长的代码,用了我一天TAT!). 题目 传送门. 一个连通无向图,点上有权,支持两种操作: ...

  3. boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET

    boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET boost:regex分割字符串(带有'\'字符) 分类: C++ 2011-08- ...

  4. apache一键安装脚本

    近期在玩apache,首先安装apace要配置apr.apr-util,pcre,而配置这些基本都是千篇一律.所谓程序猿的精神就是降低反复性的劳动,以下请看我写的apache安装脚本: 这个脚本我也放 ...

  5. PHP - 表单与验证

    第11章 表单与验证 学习要点: 1.Header()函数 2.接收及验证数据 我们对Web感兴趣,认为它有用的原因是其主要通过基于HTML的表单发布和收集信息的能力.这些表单用来鼓励网站的反馈.进行 ...

  6. QObject,有一个生存线程(慢慢体会)

     moveToThread本来就是操作QObject的    比如把当前对象移到主线程里: myObject->moveToThread(QApplication::instance()-> ...

  7. 动态Pivot(2)

    原文  http://book.51cto.com/art/200710/58875.htm 存储过程sp_pivot的实现包含糟糕的编程习惯和安全隐患.就像我在本章的前面提到的,微软强烈建议不要在用 ...

  8. notepad++ 配置笔记

    0.notepad++简单介绍 Notepad++是一套很有特色的自由软件的纯文字编辑器,有完整的中文化接口及支援多国语言撰写的功能.它的功能比 Windows 中的 Notepad更强大.Notep ...

  9. RFS的web自动化验收测试——第14讲 万能的evaluate

    引言:什么是RFS——RobotFramework+Selenium2library,本系列主要介绍web自动化验收测试方面. ( @齐涛-道长 新浪微博) 这一讲我们重点来介绍一下一个常用的关键字e ...

  10. (摘录)MSMQ的简单介绍

    MSMQ(MicroSoft  Message  Queue,微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间中的任 ...