Qt 简约美观的动画 摆钟风格 第十季

2024-03-03 18:52

本文主要是介绍Qt 简约美观的动画 摆钟风格 第十季,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

😊 今天给大家分享一个摆钟风格的加载动画 😊
效果如下:
在这里插入图片描述
最近工作忙起来了 , 后续再分享其他有趣的加载动画吧.
一共三个文件 , 可以直接编译运行

//main.cpp
#include "LoadingAnimWidget.h"
#include <QApplication>
#include <QGridLayout>
int main(int argc, char *argv[])
{QApplication a(argc, argv);QWidget w;w.setWindowTitle("加载动画 第10季");QGridLayout * mainLayout = new QGridLayout;auto* anim1 = new ThreeMovingZoomingBalls;mainLayout->addWidget(anim1,0,0);auto* anim2 = new FourRotatingFireBall;mainLayout->addWidget(anim2,0,1);auto* anim3 = new CookieMonster;mainLayout->addWidget(anim3,0,2);auto* anim4 = new StickCircle;anim4->setStickColor("lime");anim4->setStickWidth(16);anim4->setStickLength(32);mainLayout->addWidget(anim4,1,0);auto* anim5 = new PendulumClock;mainLayout->addWidget(anim5,1,1);auto* anim6 = new AmpereMeter;mainLayout->addWidget(anim6,1,2);w.setLayout(mainLayout);w.show();anim1->start();anim2->start();anim3->start();anim4->start();anim5->start();anim6->start();return a.exec();
}
//LoadingAnimWidget.h
#ifndef LOADINGANIMWIDGET_H
#define LOADINGANIMWIDGET_H
#include <QPropertyAnimation>
#include <QWidget>
class LoadingAnimBase:public QWidget
{Q_OBJECTQ_PROPERTY(qreal angle READ angle WRITE setAngle)
public:LoadingAnimBase(QWidget* parent=nullptr);virtual ~LoadingAnimBase();qreal angle()const;void setAngle(qreal an);
public slots:virtual void exec();virtual void start();virtual void stop();
protected:QPropertyAnimation mAnim;qreal mAngle;
};
class StickCircle:public LoadingAnimBase
{Q_OBJECT
public:explicit StickCircle(QWidget* parent = nullptr);void setStickLength(qreal len);void setStickWidth(qreal len);void setStickColor(const QColor& color);
protected:void paintEvent(QPaintEvent *event);
private:qreal mStickLen;qreal mStickWidth;QColor mStickColor;
};
class ThreeMovingZoomingBalls:public LoadingAnimBase{//三个一边移动一边缩放的小球
public:ThreeMovingZoomingBalls(QWidget* parent = nullptr);
protected:void paintEvent(QPaintEvent*);
};
class FourRotatingFireBall:public LoadingAnimBase{//四个旋转的火球,说他是火球是因为快速转动的时候它有残影,看上去像弧线
public:FourRotatingFireBall(QWidget* parent = nullptr);
protected:void paintEvent(QPaintEvent*);
};
class CookieMonster:public LoadingAnimBase{//一个不停吃饼干的怪兽
public:CookieMonster(QWidget* parent = nullptr);
protected:void paintEvent(QPaintEvent*);
};
class PendulumClock:public LoadingAnimBase{//吊钟钟摆
public:PendulumClock(QWidget* parent = nullptr);
protected:void paintEvent(QPaintEvent*);
};
class AmpereMeter :public LoadingAnimBase{//电流表的指针
public:AmpereMeter(QWidget* parent = nullptr);
protected:void paintEvent(QPaintEvent*);
};#endif
//LoadingAnimWidget.cpp
#include "LoadingAnimWidget.h"
#include <QDebug>
#include <QPaintEvent>
#include <QPainter>
#include <QtMath>
#include <QRandomGenerator>
LoadingAnimBase::LoadingAnimBase(QWidget* parent):QWidget(parent){mAnim.setPropertyName("angle");mAnim.setTargetObject(this);mAnim.setDuration(2000);mAnim.setLoopCount(-1);//run forevermAnim.setEasingCurve(QEasingCurve::Linear);setFixedSize(200,200);mAngle = 0;
}
LoadingAnimBase::~LoadingAnimBase(){}
void LoadingAnimBase::exec(){if(mAnim.state() == QAbstractAnimation::Stopped){start();}else{stop();}
}
void LoadingAnimBase::start(){mAnim.setStartValue(0);mAnim.setEndValue(360);mAnim.start();
}
void LoadingAnimBase::stop(){mAnim.stop();
}
qreal LoadingAnimBase::angle()const{ return mAngle;}
void LoadingAnimBase::setAngle(qreal an){mAngle = an;update();
}
StickCircle::StickCircle(QWidget *parent) : LoadingAnimBase(parent),mStickLen(-1),mStickWidth(10),mStickColor("black"){}
void StickCircle::setStickLength(qreal len){if(len > 0 && len != mStickLen){mStickLen = len;update();}
}
void StickCircle::setStickWidth(qreal len){if(len > 0 && len != mStickWidth){mStickWidth = len;update();}
}
void StickCircle::setStickColor(const QColor& color){if(color.isValid() && mStickColor != color){mStickColor = color;update();}
}
void StickCircle::paintEvent(QPaintEvent *event){QPainter painter(this);painter.setRenderHints(QPainter::Antialiasing);int x = width();int y = height();qreal ang = mAngle;painter.setPen(Qt::NoPen);painter.translate(x/2,y/2);QColor lightColor = mStickColor;lightColor.setAlphaF(0.1);const int stickw = mStickWidth;//小棍子的宽度qreal stickl = x / 4;//小棍子的长度if(mStickLen > 0) stickl = mStickLen;QColor highlight = mStickColor;const int startIdx = ang / 30;//开头的深色小棍子的索引号const int arr[14] = {0,1,2,3,4,5,6,7,8,9,10,11,0,1};QList<int> hightlightList{arr[startIdx],arr[startIdx+1],arr[startIdx+2]};QList<qreal> alphaList{0.5,0.75,0.99};//三根深色的小棍子的透明度for(int i = 0;i < 12;++i){if(hightlightList.contains(i)){auto hc = highlight;hc.setAlphaF(alphaList[0]);painter.setBrush(QBrush(hc));alphaList.pop_front();}else {painter.setBrush(QBrush(lightColor));}painter.drawRoundedRect(QRectF(-stickw/2,0.375*y -stickl/2-1,stickw,stickl),stickw/2,stickw/2);auto transform = painter.transform();transform.rotate(30);painter.setTransform(transform);}
}
ThreeMovingZoomingBalls::ThreeMovingZoomingBalls(QWidget* parent):LoadingAnimBase(parent){}
void ThreeMovingZoomingBalls::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);painter.setPen(Qt::NoPen);painter.setBrush(QBrush("cadetblue"));qreal x = width();qreal y = height();painter.translate(0,y/2);const qreal delta = 0.08*x;const qreal minr = 0.02*x;for(int i = 0;i < 5;++i){qreal ang = mAngle + 90*i;qreal posx = ang/360*x;if(posx > x) posx -= x;qreal r = minr + delta * qSin(M_PI / x * posx);painter.drawEllipse(QPointF(posx,0),r,r);}
}
FourRotatingFireBall::FourRotatingFireBall(QWidget* parent){}
void FourRotatingFireBall::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);QPen pen;pen.setCapStyle(Qt::RoundCap);pen.setWidth(12);painter.setPen(pen);painter.setBrush(Qt::NoBrush);const qreal x = width();const qreal y = height();const qreal r = 0.4*y;const qreal maxArc = 30;for(int i = 0;i <4;++i){painter.resetTransform();painter.translate(x/2,y/2);painter.rotate(90*i);if(i % 2 == 0){pen.setColor("lightblue");painter.setPen(pen);}else{pen.setColor("lightpink");painter.setPen(pen);}if(mAngle < 30){ //启动火球,画出弧线qreal ratio = mAngle / 30;painter.drawArc(QRectF(-r,-r,2*r,2*r),16*90,16*-ratio*maxArc);}else if(mAngle < 330){//转动两圈qreal ratio = (mAngle - 30) / 300;painter.rotate((720-maxArc)*ratio);painter.drawArc(QRectF(-r,-r,2*r,2*r),16*90,16*-maxArc);}else{//收起弧线qreal ratio = (mAngle - 330) / 30;painter.drawArc(QRectF(-r,-r,2*r,2*r),16*90,16*(1-ratio)*maxArc);}}
}
CookieMonster::CookieMonster(QWidget* parent):LoadingAnimBase(parent){}void CookieMonster::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);const qreal x = width();const qreal y = height();const qreal r = 0.2*x;painter.setPen(Qt::NoPen);painter.setBrush(QBrush("royalblue"));painter.translate(4,y/2);qreal startAngle = 22.5 + 22.5*qSin(M_PI / 90 * mAngle + M_PI/2);qreal span = 360 - 2*startAngle;painter.drawPie(QRectF(0,-r,2*r,2*r),startAngle*16,span*16);painter.setBrush(QBrush("white"));painter.drawEllipse(QPointF(r*0.8,-r/2),4,4);//画移动的小饼干painter.setBrush(QBrush("lightgreen"));painter.resetTransform();painter.translate(x,y/2);qreal ratio = mAngle / 360;for(int i = 0;i <3;++i){ratio += 0.333;if(ratio > 1) ratio -= 1;painter.drawEllipse(QPointF( -(x-2*r)*ratio,0 ) , 8,8);}
}
PendulumClock::PendulumClock(QWidget* parent):LoadingAnimBase(parent){}void PendulumClock::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);const qreal x = width();const qreal y = height();painter.translate(x/2,y/8);QPen pen("silver");pen.setWidth(4);painter.setPen(pen);painter.rotate(30*qSin(M_PI/180*mAngle));painter.setBrush(QBrush("silver"));painter.drawLine(0,0,0,0.6*y);painter.translate(0,0.6*y);painter.drawEllipse(QPointF(0,0),16,16);
}
AmpereMeter::AmpereMeter(QWidget* parent):LoadingAnimBase(parent){}void AmpereMeter::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);const qreal x = width();const qreal y = height();painter.translate(x/2,y*0.6);QPen pen("dimgray");pen.setWidth(4);painter.setPen(pen);painter.setBrush(Qt::NoBrush);const qreal r = x*0.4;//step1 先画一个表框painter.drawArc(QRectF(-r,-r,2*r,2*r),0,180*16);//step2 再画半圈刻度qreal gap = 180 / 14.0;painter.rotate(-90);for(int i = 1;i <= 13;++i){painter.rotate(gap);painter.drawLine(0,-r + 6,0,-r + 8);}//step3 画指针painter.resetTransform();painter.translate(x/2,y*0.6);painter.rotate(80*qSin(M_PI/180*mAngle));painter.drawLine(0,0,0,-r*0.7);}

这篇关于Qt 简约美观的动画 摆钟风格 第十季的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/770559

相关文章

python编写朋克风格的天气查询程序

《python编写朋克风格的天气查询程序》这篇文章主要为大家详细介绍了一个基于Python的桌面应用程序,使用了tkinter库来创建图形用户界面并通过requests库调用Open-MeteoAPI... 目录工具介绍工具使用说明python脚本内容如何运行脚本工具介绍这个天气查询工具是一个基于 Pyt

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类