zoukankan      html  css  js  c++  java
  • Qt自定义圆周动画(360 10.0 的模仿作者写的)

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

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

    下面把代码贴出来

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

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

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

  • 相关阅读:
    Linux命令之_Cut(转)
    Use “error_messages” in Rails 3.2? (raises “undefined method” error)
    ruby错误
    Linux SvN操作
    Linux 安装SVN服务器 (转)
    删除 vim 命令
    vscode 配置java开发
    java json
    svn
    采样率和比特率
  • 原文地址:https://www.cnblogs.com/findumars/p/4926684.html
Copyright © 2011-2022 走看看