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

在编写代码的过程中,由于太长时间没用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. Qt构建工具QBS之零 —— QBS 概览

    本系列文章起因 自己非常喜欢 QT 这个框架, 使用 QT 这几年, IDE 一直是使用的 QT 自带的 Qt Creator, 这个 IDE 本身比较轻巧, 同事相关的语法提示之类的也算够用, 但是 ...

  2. url中的jsessionid解释

    (1) 这是一个保险措施 因为Session默认是需要Cookie支持的 但有些客户浏览器是关闭Cookie的 这个时候就需要在URL中指定服务器上的session标识,也就是5F4771183629 ...

  3. 【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题

    原文网址:http://blog.csdn.net/ueryueryuery/article/details/20607845 我写了一个Dialog,Dialog中有一个ListView,想要点Li ...

  4. bzoj1619[Usaco2008 Nov]Guarding the Farm 保卫牧场

    Description The farm has many hills upon which Farmer John would like to place guards to ensure the ...

  5. 手游与App测试如何快速转型? —— 过来人科普手游与App测试四大区别

    随着智能设备的普及和移动互联网的兴起,各家互联网巨头纷纷在往移动端布局和转型,同时初创的移动互联网公司也都盯着这个市场希望分一杯羹.在这个大环境下,互联网的重心已经慢慢从Web端转向了移动端,而移动端 ...

  6. phpcms:七、list.html

    1.列表页{pc:content action="lists" catid="$catid" num="25" order="id ...

  7. loadrunner java 缺少必要的导入包报错

    loadrunner 运行从eclipse中做好的脚本,ctrl + A 复制到loadrunner中来, 添加参数化的的语句:verifyCode =    lr.eval_string (&quo ...

  8. ubuntu14.04 + cocos2d-x-2.2.6 + eclipse发布android + Qt Creator4

    先把需要的东西准备好,打开控制台,执行以下语句: sudo apt--jdk lib32z1 lib32ncurses5 lib32bz2- 接下来,准备好cocos2d-x-2.2.6和 andro ...

  9. (转载)XML Tutorial for iOS: How To Choose The Best XML Parser for Your iPhone Project

    There are a lot of options when it comes to parsing XML on the iPhone. The iPhone SDK comes with two ...

  10. Linux基础知识(一)

    1. Unix 和 Linux之间有什么关系? Linux可以说是Unix衍生过来的,它借鉴了很多Unix的设计理念,应该说,它们类似于父子关系,Linux又被称为类Unix系统. 2. BSD是什么 ...