【QML】简单动画实现
  TEZNKK3IfmPf 2024年03月29日 93 0

**示例1:**动画作为属性值的来源

import QtQuick 2.0

//动画作为属性值的来源
//语法: 动画on属性
//easing属性来实现缓和曲线
Rectangle{
width: 100
height: 100
color: "red"
PropertyAnimation on x{
to:50
duration: 1000
loops: Animation.Infinite //无限循环
easing.type: Easing.OutBounce//反弹效果
}
PropertyAnimation on y{
to:50
duration: 1000
loops: Animation.Infinite
easing.type: Easing.OutBounce
}
}

**示例2:**行为动画

import QtQuick 2.0

//行为动画
//Behavior为一个属性来指定默认的动画
Item{
width: 100
height: 100

Rectangle{
id:rect1
width: 100
height: 100
color:"green"

Behavior on x{
PropertyAnimation{duration: 500}
}
Behavior on y{
PropertyAnimation{duration: 500}
}
}

MouseArea{
anchors.fill:parent
onClicked: {
rect1.x = mouse.x
rect1.y = mouse.y

}

}
}

**示例3:**信号处理器中的动画

import QtQuick 2.0

//行为动画
//Behavior为一个属性来指定默认的动画

Rectangle{
id:rect1
width: 100
height: 100
color:"green"

MouseArea{
anchors.fill:parent
onClicked:PropertyAnimation{
target: rect1
properties: "x,y"
to:50
duration:2000
}
}
}

**示例4:**独立动画

import QtQuick 2.0

//独立动画(动画作为普通的QML对象来创建)
Rectangle{
id:rect1
width: 100
height: 100
color:"green"
PropertyAnimation{
id:rect1_animation
target:rect1
properties: "x,y"
duration: 1000

}
MouseArea{
anchors.fill: parent
onClicked: {
rect1_animation.to = 50
rect1_animation.running = true
}
}
}

**示例5:**状态切换

import QtQuick 2.0

//状态切换
Rectangle{
id:rect1
width: 100
height: 100
color:"green"

MouseArea{
anchors.fill: parent
onClicked: {
rect1.state = "moved"
}
onReleased: {
rect1.state = "moved_2"
}
}

//状态列表
//states:[]
states:[
State {
name:"moved"
PropertyChanges {
target: rect1
x:50
y:50
}
},
State {
name:"moved_2"
PropertyChanges {
target: rect1
x:20
y:20
}
}
]

transitions: Transition {
PropertyAnimation{
properties:"x,y"
duration: 1000

}
}
}

**示例6:**动画元素

import QtQuick 2.0

//动画元素
Rectangle{
width: 200
height: 200


//颜色
ColorAnimation on color{
from:"yellow"
to:"red"
duration: 2000
}

//旋转
RotationAnimation on rotation {
to:90
duration: 3000
direction: RotationAnimation.Clockwise//顺时针方向
}
}

**示例7:**组合动画

import QtQuick 2.0

//组合动画
Rectangle{
width: 100
height: 100

Image{
source:"picture.jpg"
anchors.horizontalCenter: parent.horizontalCenter
y:0
SequentialAnimation on y{
loops:Animation.Infinite
NumberAnimation{
to:250
easing.type: Easing.OutBounce
duration: 1000
}

PauseAnimation {
duration: 1000
}

NumberAnimation {
to:0
easing.type: Easing.OutBounce
duration: 1000
}
}
}
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年03月29日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   19天前   24   0   0 C++
  TEZNKK3IfmPf   19天前   22   0   0 指针C++
TEZNKK3IfmPf