QML官方系列教程——Use Case - Animations In QML

附网址:http://qt-project.org/doc/qt-5/qtquick-usecase-animations.html


Use Case - Animations In QML —— 用例 - QML中的动画

Qt Quick为应用程序提供了动画能力。动画属性使得属性值可以不用立刻变换到目标值,而是通过一系列中间值发生改变。要使一个项目的位置产生动画,你可以动态改变控制这个项目位置的属性的值,比如x和y,这样该项目将会在它到达目标地点的每一帧发生改变。


Fluid UIs —— 流畅的UI界面

QML旨在更便捷的流畅UI的创建,比如UI组件的出现、消失或是跳跃等等。Qt Quick提供了两种简单的方式来通过动画改变UI组件的位置。


States and Transitions —— 状态和转换

Qt Quick允许你在State对象中声明多个UI状态。这些状态由基于基态的一组改变组成,并且可以作为组织你的UI逻辑的一个很有用的方式。我们可以将状态的变换放置在需要改变的Item中,并在其中定义当状态改变时属性动画的具体实现。

一个item的状态和转换可以被声明在Item::statesItem::transitions属性中。item的多个状态在states属性中以列表形式一一声明,通常来讲这个item即为根项目。我们同样在这个item内定义Transitions,它将被用来为状态的改变添加动画。

这里是一个例子:

Item {
    id: container
    width: 320
    height: 120

    Rectangle {
        id: rect
        color: "red"
        width: 120
        height: 120

        MouseArea {
            anchors.fill: parent
            onClicked: container.state == 'other' ? container.state = '' : container.state = 'other'
        }
    }
    states: [
        // This adds a second state to the container where the rectangle is farther to the right

        State { name: "other"

            PropertyChanges {
                target: rect
                x: 200
            }
        }
    ]
    transitions: [
        // This adds a transition that defaults to applying to all state changes

        Transition {

            // This applies a default NumberAnimation to any changes a state change makes to x or y properties
            NumberAnimation { properties: "x,y" }
        }
    ]
}

·

Animating Property Changes —— 为属性改变添加动画

当一个属性值发生改变时,Behaviors可以被用来为其指定一个动画。这适用于所有改变,无论其来源(意思是不管你是长、宽、高、颜色...)。下面的例子演示了一个使用behaviors的动态按钮如何在屏幕在进行移动:

Item {
    width: 320
    height: 120

    Rectangle {
        color: "green"
        width: 120
        height: 120

        // This is the behavior, and it applies a NumberAnimation to any attempt to set the x property
        Behavior on x {

            NumberAnimation {
                //This specifies how long the animation takes
                duration: 600
                //This selects an easing curve to interpolate with, the default is Easing.Linear
                easing.type: Easing.OutBounce
            }
        }

        MouseArea {
            anchors.fill: parent
            onClicked: parent.x == 0 ? parent.x = 200 : parent.x = 0
        }
    }
}

·

Other Animations —— 其它动画

并非所有的动画都系于明确的属性或是状态。你也可以创建更为通用的animations,然后声明它的目标项目和以及需要产生这种动画的属性。这里使用两种不同的方式演示了这一点:

// 这段代码类似behaviors,将动画作用于父对象的某个属性上
Item {
    width: 320
    height: 120

    Rectangle {
        color: "blue"
        width: 120
        height: 120

        // By setting this SequentialAnimation on x, it and animations within it will automatically animate
        // the x property of this element
        SequentialAnimation on x {
            id: xAnim
            // Animations on properties start running by default
            running: false
            loops: Animation.Infinite // The animation is set to loop indefinitely
            NumberAnimation { from: 0; to: 200; duration: 500; easing.type: Easing.InOutQuad }
            NumberAnimation { from: 200; to: 0; duration: 500; easing.type: Easing.InOutQuad }
            PauseAnimation { duration: 250 } // This puts a bit of time between the loop
        }

        MouseArea {
            anchors.fill: parent
            // The animation starts running when you click within the rectangle
            onClicked: xAnim.running = true
        }
    }
}

// 与上一段代码分开,SequentialAnimation与Rectangle属于同级,通过target和property指定动画对象
Item {
    width: 320
    height: 120

    Rectangle {
        id: rectangle
        color: "yellow"
        width: 120
        height: 120

        MouseArea {
            anchors.fill: parent
            // The animation starts running when you click within the rectange
            onClicked: anim.running = true;
        }
    }

    // This animation specifically targets the Rectangle's properties to animate
    SequentialAnimation {
        id: anim
        // Animations on their own are not running by default
        // The default number of loops is one, restart the animation to see it again

        NumberAnimation { target: rectangle; property: "x"; from: 0; to: 200; duration: 500 }

        NumberAnimation { target: rectangle; property: "x"; from: 200; to: 0; duration: 500 }
    }
}

·

 

更多关于动画的信息可以前往Important Concepts in Qt Quick - States, Transitions and Animations页面。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值