Qt学习笔记——动画2

上一次做的效果不符合老板意思,需要图片连续显示,这次修改了代码,实现图片连续播放,代码如下:

#ifndef ANIMATION_H
#define ANIMATION_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 


class AnimationWidget : public QWidget
{
    Q_OBJECT
public:
    explicit AnimationWidget(QWidget *parent = 0);
    ~AnimationWidget();

signals:

public slots:
    void nextImage();
    void previousImage();

protected:
    void resizeEvent(QResizeEvent *event);

private:
    void flushAnimationTargetNext(int currentImg);

    QSize windowsSize;

    QPropertyAnimation *nextAnimation[2];
    QParallelAnimationGroup *nextParallelGroup;

    QFrame *imageFrame[4];

    int currentAnimation;
};

#endif // ANIMATION_H


#include "animationwidget.h"
#include 
#include 
#include 

#define ANIMATION_DURATION 2000

AnimationWidget::AnimationWidget(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    windowsSize.setWidth(274);
    windowsSize.setHeight(143);

    currentAnimation = 0;

    nextParallelGroup = new QParallelAnimationGroup(this);
    for (int i = 0; i < 2; ++i)
    {
        nextAnimation[i] = new QPropertyAnimation(this);
        nextParallelGroup->addAnimation(nextAnimation[i]);
    }

    for (int i = 0; i < 4; ++i)
    {
        imageFrame[i] = new QFrame(this);
        imageFrame[i]->setObjectName("avatar");
        imageFrame[i]->setStyleSheet(QString("QFrame#avatar{border-image:url(:/image/photo%1)}").arg(i+1));
    }

    flushAnimationTargetNext(currentAnimation);

    resize(windowsSize);

    connect(nextParallelGroup, SIGNAL(finished()), this, SLOT(nextImage()));
    nextImage();
}

AnimationWidget::~AnimationWidget()
{
    if (nextParallelGroup != NULL)
    {
        delete nextParallelGroup;
        nextParallelGroup = NULL;
    }

    for (int i = 0; i < 2; ++i)
    {
        if (nextAnimation[i] != NULL)
        {
            delete nextAnimation[i];
            nextAnimation[i] = NULL;
        }
    }
}

void AnimationWidget::nextImage()
{
    nextParallelGroup->stop();
    imageFrame[currentAnimation]->setVisible(false);
    imageFrame[(currentAnimation+1)%4]->setVisible(true);

    currentAnimation = (currentAnimation + 1) % 4;

    imageFrame[currentAnimation]->setVisible(true);
    imageFrame[(currentAnimation+1)%4]->setVisible(true);

    flushAnimationTargetNext(currentAnimation);
    nextParallelGroup->start();
}

void AnimationWidget::previousImage()
{
    nextParallelGroup->stop();

    flushAnimationTargetNext(currentAnimation);
    nextParallelGroup->start();
}

void AnimationWidget::resizeEvent(QResizeEvent *event)
{
    for (int i = 0; i < 4; ++i)
        imageFrame[i]->setGeometry(-windowsSize.width(), 0, windowsSize.width(), windowsSize.height());
}

void AnimationWidget::flushAnimationTargetNext(int currentImg)
{
    nextAnimation[0]->setStartValue(QPoint(0, 0));
    nextAnimation[0]->setEndValue(QPoint(windowsSize.width(), 0));
    nextAnimation[0]->setPropertyName("pos");
    nextAnimation[0]->setTargetObject(imageFrame[currentImg]);
    nextAnimation[0]->setDuration(ANIMATION_DURATION);

    currentImg = (currentImg + 1) % 4;
    nextAnimation[1]->setStartValue(QPoint(-windowsSize.width(), 0));
    nextAnimation[1]->setEndValue(QPoint(0, 0));
    nextAnimation[1]->setPropertyName("pos");
    nextAnimation[1]->setTargetObject(imageFrame[currentImg]);
    nextAnimation[1]->setDuration(ANIMATION_DURATION);
}


你可能感兴趣的:(Qt,qt,动画,图片)