QPropertyAnimation 幾行代碼快速制作流暢的動畫效果


  • QPropertyAnimation Class 官方英文文檔【點擊前往
  • QPropertyAnimation Class 中文譯文文檔【點擊前往
 

簡介

QPropertyAnimation Class 是一個控制動畫效果的類,誕生自 Qt 4.6 版本。 該類繼承自 QVarianAnimation,並支持其它基類相同的動畫類,例如:QAnimationGroup 動畫組類,該類僅支持繼承自 QObject 類的窗口部件。

以例代勞

用例子來講述各個功能,直觀,立竿見影。

頭文件

[cpp]  view plain  copy
 
  1. #ifndef MAINWINDOW_H  
  2. #define MAINWINDOW_H  
  3. #include <QMainWindow>  
  4. namespace Ui {  
  5. class MainWindow;  
  6. }  
  7.   
  8. class MainWindow : public QMainWindow  
  9. {  
  10.     Q_OBJECT  
  11. public:  
  12.     explicit MainWindow(QWidget *parent = 0);  
  13.     ~MainWindow();  
  14. private:  
  15.     Ui::MainWindow *ui;  
  16. };  
  17. #endif // MAINWINDOW_H  


cpp文件

[cpp]  view plain  copy
 
  1. #include <QPropertyAnimation>  
  2. #include "mainwindow.h"  
  3. #include "ui_mainwindow.h"  
  4.   
  5. MainWindow::MainWindow(QWidget *parent) :  
  6.     QMainWindow(parent),  
  7.     ui(new Ui::MainWindow)  
  8. {  
  9.     ui->setupUi(this);  
  10.   
  11.     /*  聲明動畫類,並將控制對象 this (this一定是繼承自QObject的窗口部件)  以及屬性名 "geometry" 傳入構造函數  */  
  12.     QPropertyAnimation* animation = new QPropertyAnimation(this, "geometry");  
  13.     /*  設置動畫持續時長為 2 秒鍾  */  
  14.     animation->setDuration(2000);  
  15.     /*  設置動畫的起始狀態 起始點 (1,2)  起始大小 (3,4)  */  
  16.     animation->setStartValue(QRect(1, 2, 3, 4));  
  17.     /*  設置動畫的結束狀態 結束點 (100,200)  結束大小 (300,400)  */  
  18.     animation->setEndValue(QRect(100, 200, 300, 400));  
  19.     /*  設置動畫效果  */  
  20.     animation->setEasingCurve(QEasingCurve::OutInExpo);  
  21.     /*  開始執行動畫 QAbstractAnimation::DeleteWhenStopped 動畫結束后進行自清理(效果就好像智能指針里的自動delete animation) */  
  22.     animation->start(QAbstractAnimation::DeleteWhenStopped);  
  23. }  
  24.   
  25. MainWindow::~MainWindow()  
  26. {  
  27.     delete ui;  
  28. }  


 
QPropertyAnimation 聲明的時候
可以傳入的屬性分別有  pos(位置)、windowOpacity(透明度)
 
位置示例
 
[cpp]  view plain  copy
 
  1. void OEasyWebNotice::onShow() {  
  2.     QRect rect = QApplication::desktop()->availableGeometry();  
  3.     const int &endy = rect.height() - height();  
  4.     QPropertyAnimation *animation= new QPropertyAnimation(this,"pos");  
  5.     animation->setDuration(2000);  
  6.     animation->setStartValue(QPoint(rect.width() - width(), rect.height()));  
  7.     animation->setEndValue(QPoint(rect.width() - width(), endy));  
  8.     animation->setEasingCurve(QEasingCurve::OutCubic);  
  9.   
  10.     connect(animation, SIGNAL(finished()),  
  11.             this, SLOT(animationFinished()));  
  12.     show();  
  13.     animation->start(QAbstractAnimation::DeleteWhenStopped);  
  14. }  


 
透明度示例
 
[cpp]  view plain  copy
 
  1. void OEasyWebNotice::onClose(void) {  
  2.     disconnect(closeButton_.get(),SIGNAL(clicked()),  
  3.             this, SLOT(onClose()));  
  4.   
  5.     QPropertyAnimation* animation = new QPropertyAnimation(this, "windowOpacity");  
  6.     animation->setDuration(1000);  
  7.     animation->setStartValue(1);  
  8.     animation->setEndValue(0);  
  9.     animation->setEasingCurve(QEasingCurve::InCirc);  
  10.     connect(animation, SIGNAL(finished()),  
  11.             this, SLOT(deleteLater()));  
  12.     show();  
  13.     animation->start(QAbstractAnimation::DeleteWhenStopped);  
  14. }  
 
 
關於 QPropertyAnimation 我為大家推薦一個我寫的項目


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM