Qt之动画效果

Qt之动画效果,第1张

动画框架由基类QAbstractAnimation以及它的两个子类QVariantAnimation、QAnimationGroup组成。基础动画由QVariantAnimation的子前滚类QPropertyAnimation来设置,再通过将多个QPropertyAnimation和QPauseAnimation组合成为动画组(QParallelAnimationGroup、QSequentialAnimationGroup),完成一个连续的动画。

QPropertyAnimation类能够修改Qt的属性值,如pos、geometry等属性。设置好动画的初值和末值,以及持续的时间后,一个属性动画就基本完成了。

通过修改控件的geometry属性可以实现缩放效果,也可以实现位移慧简余的动画,该属性的前两个值确定了控件左上角的位置,后两个值确定了控件的大小。

如果只是需要位移动画的话,修改控件的pos属性即可。pos属性就是控件的左上角所在的位置。

Qt的控件没有单独的透明度属性,要修改控件的透明度可以通过QGraphicsOpacityEffect类来实现。

动画还可以设置时间的插值曲线,默认是linear,即线性运动,通过设置QEasingCurve即可。Qt提供了40种已经定义好的曲线(如果有需要也可以自定义曲线):

通过将QPropertyAnimation或者QPauseAnimation加入,构成一个按加入顺序依次播放的动画组,动画组的总时长是各个加入动画的总和。

Qt的动画可以设置循环次数,默认的循环是从头再播放一遍,往返运动可以在一个串行动画组中加入初值末值相反的一组动画来实现。咐亏

加入并行动画组的动画会同时播放,动画组的总时长是最长的动画所需的时间。

在串行动画组的开始先加入一个QPauseAnimation,再将Pause不同的串行动画组加入并行动画组就可以实现延时效果了。

默认动画是从开始到结束这个方向播放的, 可以设置为从结束到开始播放。

综合几个小栗子:

举个栗子:->Github链接地址

用Qt中QPropertyAnimation 类实现简单的小动画耐桐;

参考代码如下:

.h文件

#ifndef MOVEWIDGET_H

#define MOVEWIDGET_H

#include <QtGui/带厅QWidget>

#include <qpropertyanimation.h>

#include <QLabel>

#include <QApplication>

class MoveWidget : public QWidget

{

Q_OBJECT

public:

MoveWidget(QWidget *parent = 0)

~MoveWidget()

private:

void star()

private:

QPropertyAnimation *animation,

*animation2

QLabel *myWidget,

*myWidget2

QString path

}

#endif // MOVEWIDGET_H

.cpp文件

#include "movewidget.h"

#include "qsequentialanimationgroup.h"

#include "qparallelanimationgroup.h"

MoveWidget::MoveWidget(QWidget *parent)

: QWidget(parent)

{

this->path = QApplication::applicationDirPath()

this->myWidget = new QLabel(this)

this->myWidget2 = new QLabel(this)

this->animation = new QPropertyAnimation(myWidget,"geometry")

this->animation2 = new QPropertyAnimation(myWidget2,"geometry")

//常见QPropertyAnimation对象,关联了myWidget这个窗体的几何属性

this->star()

}

void MoveWidget::star()

{

this->myWidget->setPixmap(QPixmap(this->path + "/hello.png"))

this->myWidget2->setPixmap(QPixmap(this->path + "/hello1.png"))

this->animation->setDuration(2000)//速度,越小越快

this->animation->昌行坦setStartValue(QRect(0,0,158,168))

this->animation->setEndValue(QRect(250,250,158,168))

// this->animation->setEasingCurve(QEasingCurve::OutBounce)//outbounce有一个缓冲的现象

// this->animation->setEasingCurve(QEasingCurve::OutBack)//outback缓慢降落

// this->animation->setEasingCurve(QEasingCurve::OutCirc)//outcirc和outback差不多

this->animation->setEasingCurve(QEasingCurve::OutInQuart)

this->animation->start()

this->animation2->setDuration(2000)

this->animation2->setStartValue(QRect(0,360,158,168))

this->animation2->setEndValue(QRect(110,180,158,168))

this->animation2->setEasingCurve(QEasingCurve::OutBounce)

this->animation2->start()

}

MoveWidget::~MoveWidget()

{

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11983868.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存