1、概述
源码放在文章末尾
该项目实现了微动背景,界面看似静态实则动态,风动,幡动,仁者心动,所以到底是什么在动?哈哈~
界面会偷偷一点一点改动文字颜色的颜色填充。
虽然是动态,但是慢到难以发觉,最适合大段文字的长期阅读与写作。
不是简单的定时改变的纯色/渐变填充,还可以设置颜色变化边界、颜色数量、角度、渐变比例等。
下面是demo显示:
项目部分代码如下所示:
#ifndef DYNAMICBACKGROUNDPURE_H
#define DYNAMICBACKGROUNDPURE_H#include "dynamicbackgroundinterface.h"class DynamicBackgroundPure : public DynamicBackgroundInterface
{
public:DynamicBackgroundPure(QWidget *parent,QColor color1=QColor(255, 250, 240),/*花卉白*/QColor color2=QColor(253, 245, 230)/*舊蕾絲色*/);void setColor(QColor color1, QColor color2);void draw(QPainter& painter);protected:void timeout();private:ColorArray c1, c2, c;ColorArray d;ColorDirects a;QColor draw_color;
};#endif // DYNAMICBACKGROUNDPURE_H
#include "dynamicbackgroundpure.h"DynamicBackgroundPure::DynamicBackgroundPure(QWidget* parent, QColor color1, QColor color2): DynamicBackgroundInterface (parent)
{setColor(color1, color2);
}void DynamicBackgroundPure::setColor(QColor color1, QColor color2)
{// 阈值colorToArray(c1, color1);colorToArray(c2, color2);// 设置各种属性for (int i = 1; i <= 4; i++){a[i] = randBool(); // 随机变化方向c[i] = randRange(c1[i], c2[i]); // 初始随机颜色d[i] = intToUnity(c2[i]-c1[i]); // 每次变化的方向,false向1变化,true向2变化if (c[i] == c1[i]) a[i] = true;if (c[i] == c2[i]) a[i] = false;}draw_color = QColor(c[cR], c[cG], c[cB], c[cA]);
}void DynamicBackgroundPure::draw(QPainter &painter)
{painter.save();{// 绘制矩形painter.setBrush(draw_color);painter.drawRect(getGeometry());}painter.restore();
}void DynamicBackgroundPure::timeout()
{// 随机变化int t = randRange(1, 4);c[t] += a[t] ? d[t] : -d[t];if (c[t] == c1[t]) a[t] = true;if (c[t] == c2[t]) a[t] = false;draw_color = QColor(c[cR], c[cG], c[cB], c[cA]);DynamicBackgroundInterface::timeout();
}