自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

新手必學(xué) QML入門教程 (3)

移動(dòng)開(kāi)發(fā)
本章講述的和前兩篇文章不同,本篇是實(shí)習(xí)一個(gè)簡(jiǎn)單的動(dòng)畫。transitions 是用于增加動(dòng)畫效果的,如果對(duì)transitions 感興趣,快來(lái)參考本文的資料。

QMLQt推出的Qt Quick技術(shù)的一部分,是一種新增的簡(jiǎn)便易學(xué)的語(yǔ)言。QML是一種陳述性語(yǔ)言,用來(lái)描述一個(gè)程序的用戶界面:無(wú)論是什么樣子,以及它如何表現(xiàn)。

經(jīng)過(guò)前面兩個(gè)教程,文字也能顯示,也能處理鼠標(biāo)事件了,來(lái)點(diǎn)動(dòng)畫吧。

新手必學(xué) QML入門教程 (3)

這個(gè)教程實(shí)現(xiàn)了當(dāng)鼠標(biāo)按住的時(shí)候,Hello,World從頂部到底部的一個(gè)旋轉(zhuǎn)過(guò)程,并帶有顏色漸變的效果。

完整的源代碼main.qml

  1. import Qt 4.7  
  2.  Rectangle {  
  3.      id: page  
  4.      width: 500; height: 200  
  5.      color: "lightgray"  
  6.      Text {  
  7.          id: helloText  
  8.          text: "Hello World!"  
  9.          y: 30  
  10.          anchors.horizontalCenter: page.horizontalCenter  
  11.          font.pointSize: 24; font.bold: true  
  12.  
  13.          MouseArea { id: mouseArea; anchors.fill: parent }  
  14.          states: State {  
  15.              name: "down"; when: mouseArea.pressed == true  
  16.              PropertyChanges { target: helloText; y: 160; rotation: 180; color: "red" }  
  17.          }  
  18.          transitions: Transition {  
  19.              from: ""; to: "down"; reversible: true  
  20.              ParallelAnimation {  
  21.                  NumberAnimation { properties: "y,rotation"; duration: 500; easing.type: Easing.InOutQuad }  
  22.                  ColorAnimation { duration: 500 }  
  23.              }  
  24.          }  
  25.      }  
  26.      Grid {  
  27.          id: colorPicker  
  28.          x: 4; anchors.bottom: page.bottom; anchors.bottomMargin: 4  
  29.          rows: 2; columns: 3; spacing: 3  
  30.          Cell { cellColor: "red"; onClicked: helloText.color = cellColor }  
  31.          Cell { cellColor: "green"; onClicked: helloText.color = cellColor }  
  32.          Cell { cellColor: "blue"; onClicked: helloText.color = cellColor }  
  33.          Cell { cellColor: "yellow"; onClicked: helloText.color = cellColor }  
  34.          Cell { cellColor: "steelblue"; onClicked: helloText.color = cellColor }  
  35.          Cell { cellColor: "black"; onClicked: helloText.color = cellColor }  
  36.      }  
  37.  } 

除了這個(gè)main.qml之外,還有一個(gè)Cell.qml也是需要的,和教程(2)中的完全一樣。下面來(lái)看一看比起教程(2)的代碼增加出來(lái)的內(nèi)容。

  1. Text{  
  2.          ...  
  3.          states: State {  
  4.              name: "down"; when: mouseArea.pressed == true  
  5.              PropertyChanges { target: helloText; y: 160; rotation: 180; color: "red" }  
  6.          }  
  7.          transitions: Transition {  
  8.              from: ""; to: "down"; reversible: true  
  9.              ParallelAnimation {  
  10.                  NumberAnimation { properties: "y,rotation"; duration: 500; easing.type: Easing.InOutQuad }  
  11.                  ColorAnimation { duration: 500 }  
  12.              }  
  13.          }  
  14.         ...  
  15.      } 

states內(nèi)嵌于Text之中,可以為Text元素添加多個(gè)狀態(tài),現(xiàn)在的這個(gè)例子只增加了一個(gè)狀態(tài)。該狀態(tài)的名為為”down”,然后由“when”指 定了什么時(shí)候觸發(fā)這個(gè)狀態(tài)。PropertyChanges則指定了哪個(gè)元素的哪些屬性會(huì)發(fā)生什么樣的變化。例子中PropertyChanges利用 “target”指定了id為”helloText”的元素會(huì)發(fā)生變化,包括其y,rotation,color等屬性。

transitions 是用于增加動(dòng)畫效果的(如果把transitions這一段代碼刪去,Hello,World的文字也會(huì)發(fā)生變化,但是看不到中間動(dòng)畫漸變效果)。同樣可以看到transitions是復(fù)數(shù)形式,意味著可以添加多個(gè)動(dòng)畫過(guò)程。“from”和”to”指明了當(dāng)前的動(dòng)畫作用于哪兩個(gè)狀態(tài)變化之間。 “from”和”to”的參數(shù)名來(lái)自于State中的”name”屬性。

ParalleAnimation則指定了有多個(gè) 有多個(gè)動(dòng)畫并行發(fā)生。NumberAnimation用于qreal類型的屬性變化,ColorAnimation則用于顏色變化。更多 Animation請(qǐng)?jiān)?strong>QML文檔中查找”Animation and Transitions”。

小結(jié):三篇關(guān)于QML教程到此結(jié)束。不管是文字顯示,還是簡(jiǎn)單的動(dòng)畫效果,相信你可以和諧的去完成,希望本篇文章對(duì)你有所幫助。

責(zé)任編輯:zhaolei 來(lái)源: Qt技術(shù)博客
相關(guān)推薦

2011-06-16 09:28:14

Qt QML 教程

2011-06-16 09:40:53

QML 教程

2010-07-27 15:53:15

2011-07-29 11:28:58

iPhone開(kāi)發(fā)

2010-08-02 09:36:22

Flex

2011-06-23 13:50:34

2011-07-04 17:26:00

Qt SQLite

2011-09-07 11:13:27

無(wú)線路由器無(wú)線路由器設(shè)置

2023-11-29 07:30:08

Python用戶界面

2010-08-03 13:06:15

Flex Builde

2013-08-29 14:12:52

Storm分布式實(shí)時(shí)計(jì)算

2009-07-08 15:12:48

Java Servle

2014-05-26 15:35:55

Web組件Web Compone

2022-02-18 09:39:51

Vue3.0Vue2.0Script Set

2011-06-16 11:04:07

Qt

2010-05-21 12:50:45

Subversion快

2011-07-21 10:29:18

iPhone 開(kāi)發(fā)

2010-08-03 14:37:30

Flex入門教程

2010-06-13 09:45:35

Widget開(kāi)發(fā)

2012-05-10 08:29:46

XcodeiOSPhoneGap
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)