• 3022阅读
  • 0回复

Qt编写自定义控件33-图片切换动画 [复制链接]

上一主题 下一主题
离线liudianwu
 

只看楼主 倒序阅读 楼主  发表于: 2019-07-21

一、前言
在很多看图软件中,切换图片的时候可以带上动画过渡或者切换效果,显得更人性化,其实主要还是炫一些,比如百叶窗、透明度变化、左下角飞入等,无论多少种效果,核心都是围绕QPainter来进行,将各种动画效果对应的图片的区域动态计算并绘制出来,配合以QPropertyAnimation动画属性产生线性插值,比如渐入飞入时候,可以中间快速两端慢速。目前动画类型有9种,后期还会不断增加。
* 1:图像1渐渐变淡,图像2渐渐显现
* 2:百叶窗效果
* 3:图像从右向左翻转
* 4:从外到内水平分割
* 5:图像1从左至右退出可视区域,同时图像2从左至右进入可视区域
* 6:图像1从左至右退出可视区域,同时图像2从左至右进入可视区域
* 7:图像1从下至上退出可视区域,同时图像2从下至上进入可视区域
* 8:图像1从上至下退出可视区域,同时图像2从上至下进入可视区域
* 9:图像1不动,同时图像2从右下到左上

二、实现的功能
* 1:支持多种等待样式风格 圆弧状风格 旋转圆风格 三角圆弧 线条风格 圆环风格
* 2:可设置范围值和当前值
* 3:可设置前景色背景色
* 4:可设置顺时针逆时针旋转
* 5:支持任意大小缩放
* 6:支持设置旋转速度间隔


三、效果图



四、头文件代码
  1. #ifndef IMAGEANIMATION_H
  2. #define IMAGEANIMATION_H
  3. /**
  4. * 图片切换动画控件 作者:赵彦博(QQ:408815041 zyb920@hotmail.com) 2019-6-10
  5. * 1:可设置动画类型,默认9种,后期会增加更多
  6. * FadeEffect = 0,             //图像1渐渐变淡,图像2渐渐显现
  7. * BlindsEffect = 1,           //百叶窗效果
  8. * FlipRightToLeft = 2,        //图像从右向左翻转
  9. * OutsideToInside = 3,        //从外到内水平分割
  10. * MoveLeftToRightEffect = 4,  //图像1从左至右退出可视区域,同时图像2从左至右进入可视区域
  11. * MoveRightToLeftEffect = 5,  //图像1从左至右退出可视区域,同时图像2从左至右进入可视区域
  12. * MoveBottomToUpEffect = 6,   //图像1从下至上退出可视区域,同时图像2从下至上进入可视区域
  13. * MoveUpToBottomEffect = 7,   //图像1从上至下退出可视区域,同时图像2从上至下进入可视区域
  14. * MoveBottomToLeftUpEffect = 8//图像1不动,同时图像2从右下到左上
  15. * 2:可设置两张图片的路径名称或者图片
  16. * 3:可设置动画因子
  17. */
  18. #include <QWidget>
  19. class QPropertyAnimation;
  20. #ifdef quc
  21. #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
  22. #include <QtDesigner/QDesignerExportWidget>
  23. #else
  24. #include <QtUiPlugin/QDesignerExportWidget>
  25. #endif
  26. class QDESIGNER_WIDGET_EXPORT ImageAnimation : public QWidget
  27. #else
  28. class ImageAnimation : public QWidget
  29. #endif
  30. {
  31.     Q_OBJECT
  32.     Q_ENUMS(AnimationType)
  33.     Q_PROPERTY(float factor READ getFactor WRITE setFactor)
  34.     Q_PROPERTY(QString imageName1 READ getImageName1 WRITE setImageName1)
  35.     Q_PROPERTY(QString imageName2 READ getImageName2 WRITE setImageName2)
  36.     Q_PROPERTY(QPixmap pixmap1 READ getPixmap1 WRITE setPixmap1)
  37.     Q_PROPERTY(QPixmap pixmap2 READ getPixmap2 WRITE setPixmap2)
  38.     Q_PROPERTY(AnimationType animationType READ getAnimationType WRITE setAnimationType)
  39. public:
  40.     enum AnimationType {
  41.         FadeEffect = 0,             //图像1渐渐变淡,图像2渐渐显现
  42.         BlindsEffect = 1,           //百叶窗效果
  43.         FlipRightToLeft = 2,        //图像从右向左翻转
  44.         OutsideToInside = 3,        //从外到内水平分割
  45.         MoveLeftToRightEffect = 4,  //图像1从左至右退出可视区域,同时图像2从左至右进入可视区域
  46.         MoveRightToLeftEffect = 5,  //图像1从左至右退出可视区域,同时图像2从左至右进入可视区域
  47.         MoveBottomToUpEffect = 6,   //图像1从下至上退出可视区域,同时图像2从下至上进入可视区域
  48.         MoveUpToBottomEffect = 7,   //图像1从上至下退出可视区域,同时图像2从上至下进入可视区域
  49.         MoveBottomToLeftUpEffect = 8//图像1不动,同时图像2从右下到左上
  50.     };
  51.     explicit ImageAnimation(QWidget *parent = 0);
  52.     ~ImageAnimation();
  53. protected:
  54.     void paintEvent(QPaintEvent *);
  55.     void fadeEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2);
  56.     void blindsEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2);
  57.     void flipRightToLeft(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2);
  58.     void outsideToInside(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2);
  59.     void moveLeftToRightEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2);
  60.     void moveRightToLeftEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2);
  61.     void moveBottomToUpEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2);
  62.     void moveUpToBottomEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2);
  63.     void moveBottomToLeftUpEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2);
  64. private:
  65.     float factor;                   //动画因子(0 - 1.0之间变化)
  66.     QString imageName1;             //图片1路径名称
  67.     QString imageName2;             //图片2路径名称
  68.     QPixmap pixmap1;                //图片1
  69.     QPixmap pixmap2;                //图片2
  70.     AnimationType animationType;    //动画效果类型
  71.     QPropertyAnimation *animation;  //动画属性
  72. public:
  73.     float getFactor()               const;
  74.     QString getImageName1()         const;
  75.     QString getImageName2()         const;
  76.     QPixmap getPixmap1()            const;
  77.     QPixmap getPixmap2()            const;
  78.     AnimationType getAnimationType()const;
  79.     QSize sizeHint()                const;
  80.     QSize minimumSizeHint()         const;
  81. public Q_SLOTS:
  82.     //设置动画因子
  83.     void setFactor(float factor);
  84.     //设置图片1+图片2路径名称
  85.     void setImageName1(const QString &imageName1);
  86.     void setImageName2(const QString &imageName2);
  87.     //设置图片1+图片2
  88.     void setPixmap1(const QPixmap &pixmap1);
  89.     void setPixmap2(const QPixmap &pixmap2);
  90.     //设置动画类型
  91.     void setAnimationType(const AnimationType &animationType);
  92.     //启动+停止动画
  93.     void start();
  94.     void stop();
  95. };
  96. #endif // IMAGEANIMATION_H

五、核心代码
  1. void ImageAnimation::paintEvent(QPaintEvent *)
  2. {
  3.     if (pixmap1.isNull() || pixmap2.isNull()) {
  4.         return;
  5.     }
  6.     QPainter painter(this);
  7.     painter.setRenderHint(QPainter::Antialiasing, true);
  8.     switch (animationType) {
  9.     case 0:
  10.         fadeEffect(&painter, geometry(), factor, pixmap1, pixmap2);
  11.         break;
  12.     case 1:
  13.         blindsEffect(&painter, geometry(), factor, pixmap1, pixmap2);
  14.         break;
  15.     case 2:
  16.         flipRightToLeft(&painter, geometry(), factor, pixmap1, pixmap2);
  17.         break;
  18.     case 3:
  19.         outsideToInside(&painter, geometry(), factor, pixmap1, pixmap2);
  20.         break;
  21.     case 4:
  22.         moveLeftToRightEffect(&painter, geometry(), factor, pixmap1, pixmap2);
  23.         break;
  24.     case 5:
  25.         moveRightToLeftEffect(&painter, geometry(), factor, pixmap1, pixmap2);
  26.         break;
  27.     case 6:
  28.         moveBottomToUpEffect(&painter, geometry(), factor, pixmap1, pixmap2);
  29.         break;
  30.     case 7:
  31.         moveUpToBottomEffect(&painter, geometry(), factor, pixmap1, pixmap2);
  32.         break;
  33.     case 8:
  34.         moveBottomToLeftUpEffect(&painter, geometry(), factor, pixmap1, pixmap2);
  35.         break;
  36.     default:
  37.         break;
  38.     }
  39. }
  40. void ImageAnimation::fadeEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2)
  41. {
  42.     int w = rect.width();
  43.     int h = rect.height();
  44.     int alpha = 255 * (1.0f - factor);
  45.     QPixmap alphaPixmap(pixmap1.size());
  46.     alphaPixmap.fill(Qt::transparent);
  47.     QPainter p1(&alphaPixmap);
  48.     p1.setCompositionMode(QPainter::CompositionMode_Source);
  49.     p1.drawPixmap(0, 0, pixmap1);
  50.     p1.setCompositionMode(QPainter::CompositionMode_DestinationIn);
  51.     p1.fillRect(alphaPixmap.rect(), QColor(0, 0, 0, alpha));
  52.     p1.end();
  53.     int x = (w - pixmap1.width()) / 2;
  54.     int y = (h - pixmap1.height()) / 2;
  55.     painter->drawPixmap(x, y, alphaPixmap);
  56.     alpha = 255 * (factor);
  57.     alphaPixmap.fill(Qt::transparent);
  58.     QPainter p2(&alphaPixmap);
  59.     p2.setCompositionMode(QPainter::CompositionMode_Source);
  60.     p2.drawPixmap(0, 0, pixmap2);
  61.     p2.setCompositionMode(QPainter::CompositionMode_DestinationIn);
  62.     p2.fillRect(alphaPixmap.rect(), QColor(0, 0, 0, alpha));
  63.     p2.end();
  64.     painter->drawPixmap(x, y, alphaPixmap);
  65. }
  66. void ImageAnimation::blindsEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2)
  67. {
  68.     int i, n, w, h, x1, y1, x2, y2, dh, ddh;
  69.     w = rect.width();
  70.     h = rect.height();
  71.     x1 = (w - pixmap1.width()) / 2;
  72.     y1 = (h - pixmap1.height()) / 2;
  73.     x2 = (w - pixmap2.width()) / 2;
  74.     y2 = (h - pixmap2.height()) / 2;
  75.     painter->drawPixmap(x1, y1, pixmap1);
  76.     n = 10;
  77.     dh = pixmap2.height() / n;
  78.     ddh = factor * dh;
  79.     if (ddh < 1) {
  80.         ddh = 1;
  81.     }
  82.     for(i = 0; i < n; i++) {
  83.         painter->drawPixmap(x2, y2 + i * dh, pixmap2, 0, i * dh, pixmap2.width(), ddh);
  84.     }
  85. }
  86. void ImageAnimation::flipRightToLeft(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2)
  87. {
  88.     int x1, y1, x2, y2, w, h;
  89.     float rot;
  90.     QTransform trans;
  91.     w = rect.width();
  92.     h = rect.height();
  93.     x1 = (w - pixmap1.width()) / 2;
  94.     y1 = (h - pixmap1.height()) / 2;
  95.     x2 = (w - pixmap2.width()) / 2;
  96.     y2 = (h - pixmap2.height()) / 2;
  97.     rot = factor * 90.0f;
  98.     trans.translate(w * (1 - factor), h / 2);
  99.     trans.rotate(rot, Qt::YAxis);
  100.     trans.translate(-w, -h / 2);
  101.     painter->setTransform(trans);
  102.     painter->drawPixmap(x1, y1, pixmap1);
  103.     painter->resetTransform();
  104.     trans.reset();
  105.     rot = 90 * (factor - 1);
  106.     trans.translate(w * (1 - factor), h / 2);
  107.     trans.rotate(rot, Qt::YAxis);
  108.     trans.translate(0, -h / 2);
  109.     painter->setTransform(trans);
  110.     painter->drawPixmap(x2, y2, pixmap2);
  111.     painter->resetTransform();
  112. }
  113. void ImageAnimation::outsideToInside(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2)
  114. {
  115.     int   w, h, x1, y1, x2, y2, x3, y3, dh, ddh;
  116.     w = rect.width();
  117.     h = rect.height();
  118.     x1 = (w - pixmap1.width()) / 2;
  119.     y1 = (h - pixmap1.height()) / 2;
  120.     painter->drawPixmap(x1, y1, pixmap1);
  121.     dh = pixmap2.height() / 2;
  122.     ddh = factor * dh;
  123.     if (ddh < 1) {
  124.         ddh = 1;
  125.     }
  126.     x2 = (w - pixmap2.width()) / 2;
  127.     y2 = (h - pixmap2.height()) / 2;
  128.     painter->drawPixmap(x2, y2, pixmap2, 0, 0, pixmap2.width(), ddh);
  129.     x3 = (w - pixmap2.width()) / 2;
  130.     y3 =  dh * (1.0f - factor) + h / 2;
  131.     if(y3 != h / 2) {
  132.         y3 += 1;
  133.     }
  134.     painter->drawPixmap(x3, y3, pixmap2, 0, pixmap2.height() - ddh, pixmap2.width(), ddh);
  135. }
  136. void ImageAnimation::moveLeftToRightEffect(QPainter *painter, const QRect &rect, float factor, const QPixmap &pixmap1, const QPixmap &pixmap2)
  137. {
  138.     int x, y, w, h, x1, y1, x2, y2;
  139.     w = rect.width();
  140.     h = rect.height();
  141.     x1 = (w - pixmap1.width()) / 2;
  142.     y1 = (h - pixmap1.height()) / 2;
  143.     x2 = (w - pixmap2.width()) / 2;
  144.     y2 = (h - pixmap2.height()) / 2;
  145.     x = x1 + w * factor;
  146.     y = y1;
  147.     painter->drawPixmap(x, y, pixmap1);
  148.     x = x2 + w * (factor - 1);
  149.     y = y2;
  150.     painter->drawPixmap(x, y, pixmap2);
  151. }

六、控件介绍
1. 超过149个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,flatui、高亮按钮、滑动选择器、农历等。远超qwt集成的控件数量。
2. 每个类都可以独立成一个单独的控件,零耦合,每个控件一个头文件和一个实现文件,不依赖其他文件,方便单个控件以源码形式集成到项目中,较少代码量。qwt的控件类环环相扣,高度耦合,想要使用其中一个控件,必须包含所有的代码。
3. 全部纯Qt编写,QWidget+QPainter绘制,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等编译器,支持任意操作系统比如windows+linux+mac+嵌入式linux等,不乱码,可直接集成到Qt  Creator中,和自带的控件一样使用,大部分效果只要设置几个属性即可,极为方便。
4. 每个控件都有一个对应的单独的包含该控件源码的DEMO,方便参考使用。同时还提供一个所有控件使用的集成的DEMO。
5. 每个控件的源代码都有详细中文注释,都按照统一设计规范编写,方便学习自定义控件的编写。
6. 每个控件默认配色和demo对应的配色都非常精美。
7. 超过130个可见控件,6个不可见控件。
8. 部分控件提供多种样式风格选择,多种指示器样式选择。
9. 所有控件自适应窗体拉伸变化。
10.  集成自定义控件属性设计器,支持拖曳设计,所见即所得,支持导入导出xml格式。
11. 自带activex控件demo,所有控件可以直接运行在ie浏览器中。
12. 集成fontawesome图形字体+阿里巴巴iconfont收藏的几百个图形字体,享受图形字体带来的乐趣。
13. 所有控件最后生成一个dll动态库文件,可以直接集成到qtcreator中拖曳设计使用。
14. 目前已经有qml版本,后期会考虑出pyqt版本,如果用户需求量很大的话。

七、SDK下载
- SDK下载链接:https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ 提取码:877p
- 下载链接中包含了各个版本的动态库文件,所有控件的头文件,使用demo,自定义控件+属性设计器。
- 自定义控件插件开放动态库dll使用(永久免费),无任何后门和限制,请放心使用。
- 目前已提供26个版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
- 不定期增加控件和完善控件,不定期更新SDK,欢迎各位提出建议,谢谢!
- widget版本(QQ:517216493)qml版本(QQ:373955953)三峰驼(QQ:278969898)。
- 涛哥的知乎专栏 Qt进阶之路 https://zhuanlan.zhihu.com/TaoQt
- 欢迎关注微信公众号【高效程序员】,C++/Python、学习方法、写作技巧、热门技术、职场发展等内容,干货多多,福利多多!
  
欢迎关注微信公众号:Qt实战/Qt入门和进阶(各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发) QQ:517216493  WX:feiyangqingyun  QQ群:751439350
快速回复
限100 字节
 
上一个 下一个