【QML】简单动画实现

简介: 【QML】简单动画实现

QML动画

示例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
            }
        }
    }
}
相关文章
|
3天前
|
前端开发 JavaScript 开发者
【QML进阶 进度条设计】打造动态弧形进度条特效
【QML进阶 进度条设计】打造动态弧形进度条特效
100 1
|
缓存 安全 Linux
强大的动画插件——DOTween介绍(Unity3D)
DOTween是一个用于Unity的快速、高效、完全类型安全的面向对象动画引擎,为c#用户进行了优化,是免费和开源的,具有大量高级特性 DOTween兼容Unity 2019至4.6版本。 适用于:Win, Mac, Linux, Unity WebPlayer, WebGL, iOS, Android, Windows Phone, Windows Store, PS Vita (PSM), PS3/PS4, Xbox 360/One,任天堂Switch + more(没有测试额外的平台,但除了Flash导出,它应该可以在任何地方工作)
|
3天前
|
Java Android开发
Android开发之使用OpenGL实现翻书动画
本文讲述了如何使用OpenGL实现更平滑、逼真的电子书翻页动画,以解决传统贝塞尔曲线方法存在的卡顿和阴影问题。作者分享了一个改造后的外国代码示例,提供了从前往后和从后往前的翻页效果动图。文章附带了`GlTurnActivity`的Java代码片段,展示如何加载和显示书籍图片。完整工程代码可在作者的GitHub找到:https://github.com/aqi00/note/tree/master/ExmOpenGL。
40 1
Android开发之使用OpenGL实现翻书动画
|
3天前
|
算法 程序员 人机交互
【QML 设置颜色】QML中的色彩魔法:从取色器到用户界面
【QML 设置颜色】QML中的色彩魔法:从取色器到用户界面
116 0
|
7月前
QT中的动画类(QPropertyAnimation)
QT中的动画类(QPropertyAnimation)
84 0
|
7月前
|
存储 UED
Qt自定义控件之动画文本
Qt自定义控件之动画文本
|
5月前
|
图形学 iOS开发
Unity——动效与缓动动画
Unity——动效与缓动动画
|
图形学
unity动画之帧动画使用
使用unity实现lol寒冰帧动画
unity动画之帧动画使用
|
算法 数据可视化
【QT】图形视图、动画框架
【QT】图形视图、动画框架
【QT】图形视图、动画框架
Qt-QML-给我的导航条写一个动画-State-Transition
上篇中,我已经写出一个导航条的,虽然太丑了,不过功能是有了,这次我将要给我的导航条加一个动画,先看下演示效果
126 0
Qt-QML-给我的导航条写一个动画-State-Transition