手机酷狗音乐在播放的时候,中间有一个旋转图标,显示播放进度,如下所示。
Qt模仿酷狗音乐的播放图标效果图:
头文件
#ifndef QWHKUGOUMUSICLABEL_H
#define QWHKUGOUMUSICLABEL_H
/*
* 模仿酷狗音乐的音乐播放图标
* 支持缩放
*/
#include <QWidget>
#include <QPainter>
#include <QPropertyAnimation>
#include <QTimer>
#include <QPixmap>
class QWHKuGouMusicLabel : public QWidget
{
Q_OBJECT
Q_ENUMS(State)
Q_PROPERTY(int m_rotate READ getRotate WRITE setRotate)
public:
enum State
{
Stopped, //停止
Paused, //暂停
Running, //运行
};
explicit QWHKuGouMusicLabel(QWidget *parent = nullptr);
~QWHKuGouMusicLabel();
public:
//设置音乐时长
void setMusicDuration(int secs);
//设置背景图片旋转周期
void setBgPixmapDuration(int secs);
//设置每多少毫秒进度条更新一次进度
void setBarInterval(int msecs);
//设置当前音乐毫秒数
void setCurMusicSecs(int msecs);
//设置背景边缘宽度百分比(0-100)
void setBorderWidth(int borderWidth);
//设置背景图片当前旋转角度
void setRotate(int rotate);
//设置背景图片
void setBgPixmap(const QPixmap &pixmap);
//设置背景边缘颜色
void setBorderColor(QColor borderColor);
//设置背景图片底色
void setBgColor(QColor bgColor);
//设置进度条颜色
void setBarColor(QColor barColor);
//设置进度条圆圈颜色
void setCircleColor(QColor circleColor);
//获取音乐时长
int getMusicDuration() const;
//获取背景图片旋转周期
int getBgPixmapDuration() const;
//获取进度条更新频率(每多少毫秒更新一次进度)
int getBarInterval() const;
//获取当前音乐毫秒数
int getCurMusicSecs() const;
//获取背景边缘宽度百分比(0-100)
int getBorderWidth() const;
//获取背景图片角度
int getRotate() const;
//获取背景图片
QPixmap getBgPixmap() const;
//获取背景边缘颜色
QColor getBorderColor() const;
//获取背景图片底色
QColor gtBgColor() const;
//获取进度条颜色
QColor getBarColor() const;
//获取进度条圆圈颜色
QColor getCircleColor() const;
//开始音乐动画
void start();
//暂停音乐动画
void pause();
//继续音乐动画
void resume();
//停止音乐动画
void stop();
protected:
void paintEvent(QPaintEvent *event);
//绘制背景
void drawBackground(QPainter *painter);
//绘制进度条
void drawProgressBar(QPainter *painter);
//绘制进度条圆圈
void drawProgressBarCircle(QPainter *painter);
private:
//角度转弧度
double degreeToRadius(double degree);
signals:
//状态改变时发送信号
void stateChanged(State state);
private slots:
void onTimeOut();
private:
int m_musicDuration; //音乐时长
int m_pixmapDuration; //背景图片旋转周期
int m_barInterval; //每几秒进度条更新一次进度
int m_curMusicSecs; //当前音乐毫秒数
int m_borderWidth; //背景边缘宽度,百分比
int m_rotate; //背景图片当前旋转角度
QPixmap m_pixmap; //背景图片
QColor m_borderColor; //背景边缘颜色
QColor m_bgColor; //背景图片底色
QColor m_barColor; //进度条颜色
QColor m_circleColor; //进度条圆圈颜色
QPropertyAnimation *m_animation; //属性动画,旋转背景图片
QTimer *m_timer; //定时器,更新进度条
};
#endif // QWHKUGOUMUSICLABEL_H