QML動畫概述(幾十篇相關博客)


QML提供了豐富的動畫元素,說起動畫,無非是給UI增光添彩罷了。在QML中,動畫常常與State和Transition聯系在一起,這幾個概念(下面的例子中都用到了)都比較簡單,相關介紹可查看Qt官方文檔,網址如下:

http://doc.qt.io/qt-5/qtquick-statesanimations-topic.html

下面先列舉幾個QML動畫元素,動畫效果可“忘文生意”:

PropertyAnimation(常用)

AnchorAnimation

ColorAnimation

NumberAnimation

ParentAnimation

PathAnimation

RotationAnimation

Vector3dAnimation

SequentialAnimation

ParalleAnimation

PauseAnimation

SmoothedAnimation

SpringAnimation

PropertyAction(無動畫效果)

ScriptAction

Behavior(設置默認動畫)

常見的QML動畫有三種表示方式,下面一一說明。

1、使用State和Transition

State改變屬性值,Transition實現動畫,例子如下:

 

[plain]  view plain  copy
 
  1. import QtQuick 2.2  
  2.   
  3. Item {  
  4.     id: container  
  5.     width: 360  
  6.     height: 360  
  7.   
  8.     Rectangle {  
  9.         id: rect  
  10.         width: 100  
  11.         height: 100  
  12.         color: "blue"  
  13.   
  14.         MouseArea {  
  15.             anchors.fill: parent  
  16.             // state屬性值為空字符串時('')即默認狀態  
  17.             onClicked: container.state == 'right' ? container.state = '' : container.state = 'right'  
  18.         }  
  19.     }  
  20.   
  21.     states: State {  
  22.         name: "right"  
  23.         // rect水平移動  
  24.         PropertyChanges {  
  25.             target: rect  
  26.             x: 260  
  27.         }  
  28.     }  
  29.   
  30.     transitions: Transition {  
  31.         // 數字(x坐標)動畫,設置了easing的回彈效果和動畫時間  
  32.         NumberAnimation {  
  33.             property: "x"  
  34.             easing.type: Easing.InOutBounce  
  35.             duration: 500  
  36.         }  
  37.     }  
  38. }  

 

2、使用Behavior

直接修改上面的例子,實現同樣的動畫效果,結果如下:

[plain]  view plain  copy
 
  1. import QtQuick 2.2  
  2.   
  3. Item {  
  4.     id: container  
  5.     width: 360  
  6.     height: 360  
  7.   
  8.     Rectangle {  
  9.         id: rect  
  10.         width: 100  
  11.         height: 100  
  12.         color: "blue"  
  13.   
  14.         // 看這里  
  15.         Behavior on x {  
  16.             NumberAnimation {  
  17.                 easing.type: Easing.InOutBounce  
  18.                 duration: 500  
  19.             }  
  20.         }  
  21.   
  22.         MouseArea {  
  23.             anchors.fill: parent  
  24.             // 改變rect的x坐標  
  25.             onClicked: rect.x = (rect.x == 0 ? 260 : 0)  
  26.         }  
  27.     }  
  28. }  

3、其它

還是在上面例子的基礎上修改以實現同樣的效果,代碼如下:

[plain]  view plain  copy
 
  1. import QtQuick 2.2  
  2.   
  3. Item {  
  4.     id: container  
  5.     width: 360  
  6.     height: 360  
  7.   
  8.     Rectangle {  
  9.         id: rect  
  10.         width: 100  
  11.         height: 100  
  12.         color: "blue"  
  13.   
  14.         // rect水平右移  
  15.         NumberAnimation on x {  
  16.             id: right  
  17.             running: false // false  
  18.             to: 260  
  19.             easing.type: Easing.InOutBounce  
  20.             duration: 500  
  21.         }  
  22.         // rect水平左移  
  23.         NumberAnimation on x {  
  24.             id: left  
  25.             running: false // false  
  26.             to: 0  
  27.             easing.type: Easing.OutInBounce // 換個easing動畫效果  
  28.             duration: 500  
  29.         }  
  30.   
  31.         MouseArea {  
  32.             anchors.fill: parent  
  33.             // 判斷移動方向  
  34.             onClicked: rect.x == 0 ? right.running = true : left.running = true  
  35.         }  
  36.     }  
  37. }  

下面再來看一個有意思的例子,parallel和sequential動畫:

[plain]  view plain  copy
 
  1. import QtQuick 2.2  
  2.   
  3. Item {  
  4.     id: container  
  5.     width: 360  
  6.     height: 360  
  7.   
  8.     Rectangle {  
  9.         id: up  
  10.         width: 100  
  11.         height: 100  
  12.         color: "blue"  
  13.   
  14.         // 並行動畫,水平移動和顏色變化同時進行  
  15.         ParallelAnimation {  
  16.             id: parallel  
  17.             running: false  
  18.   
  19.             PropertyAnimation {  
  20.                 target: up  
  21.                 property: "x"  
  22.                 to: 260  
  23.                 duration: 500  
  24.             }  
  25.             PropertyAnimation {  
  26.                 target: up  
  27.                 property: "color"  
  28.                 to: "red"  
  29.                 duration: 500  
  30.             }  
  31.         }  
  32.   
  33.         MouseArea {  
  34.             anchors.fill: parent  
  35.             onClicked: parallel.running = true  
  36.         }  
  37.     }  
  38.   
  39.     Rectangle {  
  40.         id: down  
  41.         width: 100  
  42.         height: 100  
  43.         color: "red"  
  44.         anchors.top: up.bottom  
  45.   
  46.         // 串行動畫,先進行水平移動,后進行顏色變化  
  47.         SequentialAnimation {  
  48.             id: sequential  
  49.             running: false  
  50.   
  51.             PropertyAnimation {  
  52.                 target: down  
  53.                 property: "x"  
  54.                 to: 260  
  55.                 duration: 500  
  56.             }  
  57.             PropertyAnimation {  
  58.                 target: down  
  59.                 property: "color"  
  60.                 to: "blue"  
  61.                 duration: 500  
  62.             }  
  63.         }  
  64.         MouseArea {  
  65.             anchors.fill: parent  
  66.             onClicked: sequential.running = true  
  67.         }  
  68.     }  
  69. }  

關於QML動畫,Qt官網文檔也做了詳細的介紹:

http://doc.qt.io/qt-5/qtquick-usecase-animations.html

http://doc.qt.io/qt-5/qtquick-statesanimations-animations.html

 

http://blog.csdn.net/ieearth/article/details/43986559


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM