动画框架
创建平滑的、具有动画效果的GUI界面
帮助关键字The Animation Framework;
使用QPropertyAnimation类
头文件 < QPropertyAnimation>
QApplication app(argc, argv);
QPushButton button("Animated Button");
button.show();
QPropertyAnimation animation(&button, "geometry");
animation.setDuration(10000);
// animation.setStartValue(QRect(0, 0, 120, 30));
// animation.setEndValue(QRect(250, 250, 200, 60));
animation.setKeyValueAt(0, QRect(0, 0, 120, 30));
animation.setKeyValueAt(0.8, QRect(250, 250, 200, 60));
animation.setKeyValueAt(1, QRect(0, 0, 120, 30));
animation.start();
return app.exec();
动画中可以使用pause()来暂停动画;使用resume()结束暂停;使用stop()来停止动画;setDirection()设置动画方向;setLoopCount()设置动画重复次数。
使用缓和曲线
QEasingCurve::OutBounce缓和曲线。
实现弹跳效果:
QApplication app(argc, argv);
QPushButton button("Animated Button");
button.show();
QPropertyAnimation animation(&button, "geometry");
animation.setDuration(2000);
animation.setStartValue(QRect(250, 0, 120, 30));
animation.setEndValue(QRect(250, 300, 120, 30));
animation.setEasingCurve(QEasingCurve::OutBounce);
animation.start();
return app.exec();
动画组
QParallelAnimationGroup并行动画组
QSequentialAnimationGroup串行动画组
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();