1
我想通過將其像素的顏色更改爲相同值來爲小圖像(100x20)製作動畫。例如,每幀將紅色通道值增加1,然後減小。圖像具有alpha通道,動畫速度爲30 ... 100 fps(依賴於平臺; 30對於Linux來說已經足夠了,但窗口需要〜70才能看起來平滑)。Qt中用於動畫的最快代碼更改像素
據我所知,在QImage中完成繪圖的速度更快,但QPixmap的顯示速度更快。
我想通過將其像素的顏色更改爲相同值來爲小圖像(100x20)製作動畫。例如,每幀將紅色通道值增加1,然後減小。圖像具有alpha通道,動畫速度爲30 ... 100 fps(依賴於平臺; 30對於Linux來說已經足夠了,但窗口需要〜70才能看起來平滑)。Qt中用於動畫的最快代碼更改像素
據我所知,在QImage中完成繪圖的速度更快,但QPixmap的顯示速度更快。
我喜歡QGraphicsEffect
s和QPropertyAnimation
s。白色不着色,但黑色。
#include <QLabel>
#include <QPixmap>
#include <QGraphicsColorizeEffect>
#include <QTimerEvent>
#include <QPropertyAnimation>
#include <QShowEvent>
#include <QDebug>
class Widget : public QLabel
{
Q_OBJECT
Q_PROPERTY(qreal redness READ getRedness WRITE setRedness)
public:
Widget(QWidget *parent = 0)
{
QPixmap p(300, 300);
p.fill(Qt::black);
this->setPixmap(p);
colorize = new QGraphicsColorizeEffect();
colorize->setColor(Qt::red);
redness = 0;
colorize->setStrength(redness);
this->setGraphicsEffect(colorize);
animation = new QPropertyAnimation(this,"redness");
animation->setDuration(2000);
animation->setLoopCount(10);
animation->setStartValue(0.0);
animation->setEndValue(1.0);
animation->setEasingCurve(QEasingCurve::CosineCurve);
animation->start();
}
~Widget(){}
qreal getRedness()
{
return redness;
}
void setRedness(qreal val)
{
redness = val;
colorize->setStrength(redness);
this->update();
// qDebug() << redness;
}
public slots:
void showEvent(QShowEvent *)
{
animation->start();
}
private:
qreal redness;
QGraphicsColorizeEffect * colorize;
QPropertyAnimation * animation;
};
而這裏的main.cpp
#include <QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
希望有所幫助。
如果你需要70fps的動畫看起來很流暢,你做錯了什麼。可能你正在撕裂圖像。 *最快*的方式是使用Qt + OpenGL並編寫像素着色器。 – paddy 2013-04-10 01:46:04