gpt4 book ai didi

c++ - Qt 动画中最快的代码更改像素

转载 作者:行者123 更新时间:2023-11-30 04:19:19 28 4
gpt4 key购买 nike

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

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

最佳答案

我喜欢QGraphicsEffectQPropertyAnimation。白色不会着色,但黑色会着色。

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

希望对您有所帮助。

关于c++ - Qt 动画中最快的代码更改像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15915520/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com