由于项目需求,需要把一张图片做圆周运动,用到了属性动画,坐标计算等。

在编写代码的过程中,由于太长时间没用sin,cos函数忘了是用弧度为单位,汗呀

下面把代码贴出来

  1. /*
  2. * 圆周运动动画
  3. * */
  4. #ifndef CircleAnimationWidget_H
  5. #define CircleAnimationWidget_H
  6. #include <QWidget>
  7. #define PI  3.1415
  8. class QPropertyAnimation;
  9. class CircleAnimationWidget : public QWidget
  10. {
  11. Q_OBJECT
  12. //注册属性,用于动画
  13. Q_PROPERTY(qreal percent READ percent WRITE setPercent)
  14. public:
  15. explicit CircleAnimationWidget(QWidget *parent = 0);
  16. explicit CircleAnimationWidget(const QString &icon, \
  17. const qreal &radius, QWidget *parent = 0);
  18. void setCircleInfo(const QString &icon, const qreal &radius);
  19. void startAnimation();
  20. void stopAnimation();
  21. void setPercent(const qreal &per);
  22. const qreal &percent()
  23. {
  24. return m_percent;
  25. }
  26. protected:
  27. void paintEvent(QPaintEvent *);
  28. private slots:
  29. //更新坐标值
  30. void updatePos();
  31. private:
  32. //计算坐标值
  33. QPoint mathPoint();
  34. QPoint mathPoint(const QPoint ¢erPos, const qreal &percent, const qreal &radius);
  35. void initAnimation();
  36. private:
  37. qreal m_percent;//百分比
  38. qreal m_radius;//半径
  39. QPoint m_centerPos;//圆点坐标
  40. QPropertyAnimation *m_percentAnimation;
  41. QPixmap m_pix;
  42. QPoint m_point;//图片坐标
  43. QPoint m_originPoint;//图片原始坐标
  44. };
  45. #endif // CircleAnimationWidget_H
    1. #include "circleanimationwidget.h"
    2. #include <QPainter>
    3. #include <QPropertyAnimation>
    4. #include <qmath.h>
    5. CircleAnimationWidget::CircleAnimationWidget(QWidget *parent) :
    6. QWidget(parent)
    7. {
    8. this->setAttribute(Qt::WA_TranslucentBackground);
    9. }
    10. CircleAnimationWidget::CircleAnimationWidget(const QString &icon,\
    11. const qreal &radius, QWidget *parent) : QWidget(parent)
    12. {
    13. this->setAttribute(Qt::WA_TranslucentBackground);
    14. this->setCircleInfo(icon, radius);
    15. }
    16. void CircleAnimationWidget::initAnimation()
    17. {
    18. m_percentAnimation = new QPropertyAnimation(this, "percent");
    19. m_percentAnimation->setDuration(2000);
    20. m_percentAnimation->setStartValue(0.0);
    21. m_percentAnimation->setEndValue(1.0);
    22. m_percentAnimation->setLoopCount(-1); //无限循环,只有调用stop才会停止
    23. }
    24. void CircleAnimationWidget::startAnimation()
    25. {
    26. m_percentAnimation->start();
    27. }
    28. void CircleAnimationWidget::stopAnimation()
    29. {
    30. m_percentAnimation->stop();
    31. m_point = m_originPoint;
    32. m_percent = 0;
    33. update();
    34. }
    35. void CircleAnimationWidget::setCircleInfo(const QString &icon, const qreal &radius)
    36. {
    37. m_pix.load(icon);
    38. m_percent = 0;
    39. m_radius = radius;
    40. int pixW = m_pix.width();
    41. int pixH = m_pix.height();
    42. m_centerPos.setX(radius);
    43. m_centerPos.setY(radius);
    44. m_originPoint.setX(radius*2);
    45. m_originPoint.setY(radius);
    46. m_point = m_originPoint;
    47. this->setFixedSize(pixW + radius*2, pixH + radius*2);
    48. this->initAnimation();
    49. }
    50. void CircleAnimationWidget::setPercent(const qreal &per)
    51. {
    52. m_percent = per;
    53. updatePos();
    54. }
    55. void CircleAnimationWidget::updatePos()
    56. {
    57. m_point = mathPoint();
    58. update();
    59. }
    60. QPoint CircleAnimationWidget::mathPoint()
    61. {
    62. return this->mathPoint(m_centerPos, m_percent, m_radius);
    63. }
    64. QPoint CircleAnimationWidget::mathPoint(const QPoint ¢erPos, const qreal &percent, const qreal &radius)
    65. {
    66. qreal dx = radius * qCos(percent * ( 2 * PI)) + centerPos.x();//计算x坐标
    67. qreal dy = radius * qSin(percent * ( 2 * PI)) + centerPos.y(); // 计算y坐标
    68. return QPoint(dx, dy);
    69. }
    70. void CircleAnimationWidget::paintEvent(QPaintEvent *)
    71. {
    72. QPainter painter(this);
    73. painter.drawPixmap(m_point, m_pix);
    74. }

http://blog.csdn.net/zhjun5337/article/details/40789979

http://www.qtcn.org/bbs/read-htm-tid-57817-page-1.html

Qt自定义圆周动画(360 10.0 的模仿作者写的)的更多相关文章

  1. paip.提升用户体验---c++ qt自定义窗体(1)---标题栏的绘制

    源地址:http://blog.csdn.net/attilax/article/details/12343625 paip.提升用户体验---c++ qt自定义窗体(1)---标题栏的绘制 效果图: ...

  2. kafka_2.11-0.10.0.1生产者producer的Java实现

    转载自:http://blog.csdn.net/qq_26479655/article/details/52555283 首先导入包 将kafka目录下的libs中的jar包导入 用maven建立 ...

  3. iOS 10.0 更新点(开发者视角)

    html, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin: 0px; padding: 0 ...

  4. ViewPager无限轮播与自定义切换动画

    一直在寻求一个能用得长久的ViewPager,寻寻觅觅终于发现,ViewPager有这一个就够了. 注:并非完全原创 先看一下效果: 淡入淡出: 旋转: 无限轮播的ViewPager 主要设计思路(以 ...

  5. 还在期待安卓9.0吗?Android 10.0要来了

    目前,美国 Google公司的 AndroidP (安卓9.0),已经正式全面推出有几个多月了.众多手机品牌厂商也都在积极的进行更新适配 Android 9.0 系统(修改UI界面也算是二次开发,嗯) ...

  6. Qt 创建按钮动画

    1 封装自定义按钮 myPushBttton 2 构造函数 (默认图片,按下后显示图片) 3 测试开始按钮 4 开始制作特效 5 zoom1 向下弹跳 6 zoom2 向上弹跳 代码如下 main.h ...

  7. 自定义带动画的Toast

    一.style样式: 1.  // 移动和透明渐变结合的动画 <style name="anim_view">        <item name="@ ...

  8. Android 自定义波浪动画 --"让进度浪起来~"

    原文链接:http://www.jianshu.com/p/0e25a10cb9f5 一款效果不错的动画,实现也挺简单的,推荐阅读学习~ -- 由 傻小孩b 分享 waveview <Andro ...

  9. Kafka-0.10.0.0入门

    搭建环境略(伪集群即可以),但要注意Kafka的配置必须配置的,少配了也一样可以用,但是只能单机使用,外部机器无法连接,网上也有说. host.name=192.168.1.30 advertised ...

随机推荐

  1. PowerShell 中使用json对象的性能比较

    PowerShell v3 – Creating Objects With [pscustomobject] – it’s fast! September 19, 2011powershell, v3 ...

  2. MVC3路由设置访问后缀 html jsp

     C# Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546   publ ...

  3. #include"*.c" 文件问题

    一般我们学习C语言的时候,include预编译的一般是.h头文件,虽然来说#include却是可以包含任意扩展名的文件,因为考虑到接口与实现分离的问题,头文件里面一般放函数,变量等声明,大家一般都推荐 ...

  4. Android获取当前时间与星期几

    public class DataString { private static String mYear; private static String mMonth; private static ...

  5. MySQL所有函数及操作符

    参考:Function and Operator Reference Name Description ABS() Return the absolute value ACOS() Return th ...

  6. java cannot find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"

    出现java cannot find the tag library descriptor for "http://java.sun.com/jsp/jstl/core" 主要的愿 ...

  7. hdu 4790 Just Random (思路+分类计算+数学)

    Problem Description Coach Pang and Uncle Yang both love numbers. Every morning they play a game with ...

  8. 删除ubuntu旧内核

    ubuntu的内核经常升级,而老内核并不自动卸载.时间长了,就有一大堆内核垃圾,需要我们手动去清理. 先用uname -a 查看当前内核版本: xzc@xzc-HP-ProBook-4446s:~$ ...

  9. [置顶] STM32移植contiki进阶之三(中):timer 中文版

    鉴于自己英语水平不高,在这里,将上一篇关于contiki 的timer的文章翻译为中文,让自己在学习的时候,更方便点.文中有许多不是很通顺的地方,将就吧. Timers Contiki系统提供了一套时 ...

  10. 502 bad gateway是什么意思

    通俗解释一下 1.什么是502 bad gateway 报错? 简单来说 502 是报错类型代码 bad gateway 错误的网关 2.产生错误的原因 连接超时 我们向server器发送请求 因为s ...