qt之动画框架

 

一 动画框架:为了提供一种简单的方法来创建平滑,具有动画效果的GUI界面。动画框架中主要类以及关系如下图所示。

二 QPropertyAnimation类:如果一个对象需要实现动画效果就需要实现该类。

#include "widget.h"
#include <QApplication>
#include <QPushButton>
#include <QPropertyAnimation>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QPushButton button("animation");
    button.show();
    QPropertyAnimation animation(&button,"geometry");
    animation.setDuration(5000);
    //设置运行到的起点,和终点
    animation.setStartValue(QRect(100,100,100,100));
    animation.setEndValue(QRect(100,300,100,100));
    //使用缓和曲线
    animation.setEasingCurve(QEasingCurve::InOutSine);


    //将时间分段运行,0 表示起始时间点,1表示终止时间点
//    animation.setKeyValueAt(0,QRect(0,0,120,30));
//    animation.setKeyValueAt(0.1,QRect(250,250,120,30));
//    animation.setKeyValueAt(1,QRect(500,500,120,30));
    animation.start();
    return a.exec();
}

运行演示:(notes:网上下载 LICEcap 即可实现录屏操作并生成GIF文件)

动画效果
标题

三 动画组:使用QAnimationGroup 类可实现复杂的的动画,它的两个子类QSequentialAnimationGroup和QParallelAnimationGroup分别提供了串行动画组和并行动画组。

eg: QSequentialAnimationGroup类实现串行动作,动作顺序为依次加入动画组的顺序。

#include <QApplication>
#include <QPushButton>
#include <QPropertyAnimation>
#include <QSequentialAnimationGroup>

int main(int argc, char* argv[ ])
{
    QApplication app(argc, argv);

    QPushButton button("Animated Button");
    button.show();
    // 按钮部件的动画1
    QPropertyAnimation *animation1 = new QPropertyAnimation(&button, "geometry");
    animation1->setDuration(2000);
    animation1->setStartValue(QRect(250, 0, 120, 30));
    animation1->setEndValue(QRect(250, 300, 120, 30));
    animation1->setEasingCurve(QEasingCurve::OutBounce);
    // 按钮部件的动画2
    QPropertyAnimation *animation2 = new QPropertyAnimation(&button, "geometry");
    animation2->setDuration(1000);
    animation2->setStartValue(QRect(250, 300, 120, 30));
    animation2->setEndValue(QRect(250, 300, 200, 60));
    // 串行动画组
    QSequentialAnimationGroup group;
    group.addAnimation(animation1);
    group.addAnimation(animation2);
    group.start();

    return app.exec();
}

运行效果:

串行动画

eg: QParallelAnimationGroup类实现并行动作,多个动作同步运行。

#include <QApplication>
#include <QPushButton>
#include <QPropertyAnimation>
#include <QSequentialAnimationGroup>
#include <QParallelAnimationGroup>

int main(int argc, char* argv[ ])
{
    QApplication app(argc, argv);

    QPushButton button1("Animated Button");
    button1.show();
    QPushButton button2("Animated Button2");
    button2.show();
    // 按钮部件1的动画
    QPropertyAnimation *animation1 = new QPropertyAnimation(&button1, "geometry");
    animation1->setDuration(2000);
    animation1->setStartValue(QRect(250, 0, 120, 30));
    animation1->setEndValue(QRect(250, 300, 120, 30));
    animation1->setEasingCurve(QEasingCurve::OutBounce);
    // 按钮部件2的动画
    QPropertyAnimation *animation2 = new QPropertyAnimation(&button2, "geometry");
    animation2->setDuration(2000);
    animation2->setStartValue(QRect(400, 300, 120, 30));
    animation2->setEndValue(QRect(400, 300, 200, 60));
    // 并行动画组
    QParallelAnimationGroup group;
    group.addAnimation(animation1);
    group.addAnimation(animation2);
    group.start();

    return app.exec();
}

 运行效果:

并行动画组

 

四:在图形视图框架中使用动画

    注意图形项需要需继承QGraphicsObject,该类提供了多个常用的属性,比如位置pos,透明度opacity,旋转rotation和缩放scale等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值