1

我想通过将其像素的颜色更改相同的值来为小(100x20)图像设置动画。例如,将红色通道值每帧增加 1,然后再减少。图像具有 alpha 通道,动画速度为 30...100 fps(取决于平台;30 对于 linux 就足够了,但 windows 需要 ~70 才能看起来流畅)。

据我所知,在 QImage 中完成绘图更快,但使用 QPixmap 显示更快。

4

1 回答 1

2

我喜欢QGraphicsEffects和QPropertyAnimations。白色不会着色,但黑色会。

#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();
}

希望有帮助。

于 2013-04-10T03:02:28.990 回答