当前位置:   article > 正文

qt-各种惊艳动画实例_qt动画

qt动画


一、演示效果

请添加图片描述

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

二、关键代码

#include "bounceindicator.h"

#include <QPainter>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QTime>

inline QColor randomColor()
{
    srand(QTime::currentTime().msecsSinceStartOfDay());
    return QColor(qrand() % 255, qrand() % 255, qrand() % 255);
}

static const QSize ItemSize = QSize(30, 30);
static const int TopMargin = 10;
static const int BounceDuration = 200;
static const QEasingCurve::Type EasingCurve = QEasingCurve::Linear;

class BounceItem : public QWidget
{
    Q_OBJECT
public:
    explicit BounceItem(QWidget* parent = nullptr);
    void setColor(const QColor& clr) { m_color = clr; update(); }
    QColor color() const { return m_color; }

protected:
    void paintEvent(QPaintEvent* event);

private:
    QColor m_color = Qt::blue;
};

BounceItem::BounceItem(QWidget *parent) : QWidget(parent)
{
    setFixedSize(ItemSize);
}

void BounceItem::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(m_color));
    painter.drawEllipse(rect());
}

class BounceIndicatorPrivate
{
public:
    void layoutItems();
    void makeBounce(int index);

    BounceIndicator* q_ptr;
    bool started = false;
    QList<BounceItem*> items;
    QPropertyAnimation* headAnimation = nullptr;
    QParallelAnimationGroup* parallelAnimGroup = nullptr;
};

void BounceIndicatorPrivate::layoutItems()
{
    int itemCount = items.size();
    static const int ItemSpace = 30;
    int horSpace = ItemSpace;
    for (int i = 0; i < items.size(); ++i) {
        items.at(i)->move(horSpace, (q_ptr->height() / 2)  - (ItemSize.height() / 2));
        horSpace += ItemSize.width() + ItemSpace;
    }
}

void BounceIndicatorPrivate::makeBounce(int index)
{
    if (index >= items.size())
        return;

    BounceItem* item = items.at(index);
    int x = item->x();
    int end = 20;
    int start = 50;

    QPropertyAnimation* anim = new QPropertyAnimation(item, "pos");
    anim->setEasingCurve(EasingCurve);
    anim->setDuration(BounceDuration);
    anim->setStartValue(QPoint(x, start));
    anim->setEndValue(QPoint(x, end));
    anim->start();

    if (index == 0 && !headAnimation)
        headAnimation = anim;

    q_ptr->connect(anim, &QAbstractAnimation::finished, [this, item, x, index, anim, start, end] () {
        QPropertyAnimation* reverseAnim = new QPropertyAnimation(item, "pos");
        reverseAnim->setEasingCurve(EasingCurve);
        reverseAnim->setDuration(BounceDuration);
        reverseAnim->setStartValue(QPoint(x, end));
        reverseAnim->setEndValue(QPoint(x, start));
        reverseAnim->start();

        makeBounce(index + 1);

        if (index == (items.size() - 1))
            q_ptr->connect(reverseAnim, SIGNAL(finished()), headAnimation, SLOT(start()));

        q_ptr->connect(reverseAnim, &QAbstractAnimation::finished, [anim] () {
//            anim->start();
        });
    });
}

BounceIndicator::BounceIndicator(QWidget *parent) : QWidget(parent)
{
    d_ptr = new BounceIndicatorPrivate;
    d_ptr->q_ptr = this;
    setNumberOfItems(7);
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
}

BounceIndicator::~BounceIndicator()
{
    delete d_ptr;
}

void BounceIndicator::setAnimationStarted(bool start)
{
    if (start) {

        if (d_ptr->items.isEmpty())
            return;

        int startIndex = 0;
        d_ptr->makeBounce(startIndex);

    } else {

    }
}

bool BounceIndicator::animationStarted() const
{
    return false;
}

void BounceIndicator::setNumberOfItems(int number)
{
    if (d_ptr->items.size() != number) {
        int delta = number - d_ptr->items.size();
        if (delta > 0) {
            for (int i = 0; i < delta; ++i) {
                BounceItem* item = new BounceItem(this);
                item->setColor(randomColor());
                d_ptr->items.append(item);
            }
        } else {
            for (int i = 0 ; i < qAbs(delta); ++i) {
                BounceItem* bi = d_ptr->items.takeLast();
                delete bi;
                d_ptr->items.pop_back();
            }
        }

        d_ptr->layoutItems();
    }
}

int BounceIndicator::numberOfItems() const
{
    return d_ptr->items.size();
}

QSize BounceIndicator::sizeHint() const
{
    return QSize(300, 80);
}

void BounceIndicator::resizeEvent(QResizeEvent* event)
{
    QWidget::resizeEvent(event);
    d_ptr->layoutItems();
}

#include "bounceindicator.moc"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185

三、下载链接

https://download.csdn.net/download/u013083044/88846392

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号