RT,用QT实现的一个简单动画,比较简单
所以,直接上代码,呵呵。
//tqt.h
#ifndef TQT_H_
#define TQT_H_
#include <QtGui>
#include <QtCore>
class Widget : public QWidget
{
Q_OBJECT
private:
QFrame *frame[10];
QPushButton *prevButton;
QPushButton *nextButton;
QPropertyAnimation *animation1;
QPropertyAnimation *animation2;
QSequentialAnimationGroup *animationGroup;
QSize winSize;
int index;
bool isChanging;
protected:
void resizeEvent(QResizeEvent *event);
public:
Widget(QWidget *parent = 0);
~Widget();
public slots:
void clickedPrevButton();
void clickedNextButton();
void animationFinished();
};
#endif
//tqt.cpp
#include "tqt.h"
Widget::Widget(QWidget *parent /* = 0 */)
: QWidget(parent)
{
setWindowTitle("Widget");
resize(400, 300);
animation1 = new QPropertyAnimation(this);
animation2 = new QPropertyAnimation(this);
animationGroup = new QSequentialAnimationGroup;
prevButton = new QPushButton("prev", this);
nextButton = new QPushButton("next", this);
QHBoxLayout *subLayout = new QHBoxLayout;
QVBoxLayout *layout = new QVBoxLayout;
subLayout->addStretch();
subLayout->addWidget(prevButton);
subLayout->addWidget(nextButton);
subLayout->addStretch();
layout->addStretch();
layout->addLayout(subLayout);
setLayout(layout);
winSize = size();
index = 0;
for(int i=0; i<10; i++)
{
frame[i] = new QFrame(this);
frame[i]->setObjectName("avatar");
//0.jpg~9.jpg是当前目录下的10张图片
QString str = QString("QFrame#avatar{border-image:url(%1.jpg)}")
.arg( QString::number(i) );
frame[i]->setStyleSheet(str);
}
prevButton->setEnabled(false);
animation1->setStartValue( QPoint(winSize.width()/3, 10) );
animation1->setEndValue( QPoint(winSize.width(), 10) );
animation1->setDuration(2000);
animation1->setPropertyName("pos");
animation2->setStartValue( QPoint(-winSize.width()/3, 10) );
animation2->setEndValue( QPoint(winSize.width()/3, 10) );
animation2->setDuration(2000);
animation2->setPropertyName("pos");
animationGroup->addAnimation(animation1);
animationGroup->addAnimation(animation2);
index = 0;
animation1->setTargetObject(frame[index]);
isChanging = false;
connect(prevButton, SIGNAL(clicked()), this, SLOT(clickedPrevButton()));
connect(nextButton, SIGNAL(clicked()), this, SLOT(clickedNextButton()));
connect(animationGroup, SIGNAL(finished()), this, SLOT(animationFinished()));
}
Widget::~Widget()
{
}
void Widget::resizeEvent(QResizeEvent *event)
{
winSize = size();
for(int i=0; i<10; i++)
frame[i]->setGeometry(-winSize.width()/3, 10, winSize.width()/3, winSize.height()-50);
frame[index]->setGeometry(winSize.width()/3, 10, winSize.width()/3, winSize.height()-50);
animation1->setStartValue( QPoint(winSize.width()/3, 10) );
animation1->setEndValue( QPoint(winSize.width(), 10) );
animation2->setStartValue( QPoint(-winSize.width()/3, 10) );
animation2->setEndValue( QPoint(winSize.width()/3, 10) );
}
void Widget::clickedPrevButton()
{
if(isChanging)
return;
nextButton->setEnabled(true);
isChanging = true;
setFixedSize(winSize.width(), winSize.height());
index--;
animation2->setTargetObject(frame[index]);
animationGroup->start();
if(index <= 0)
prevButton->setEnabled(false);
}
void Widget::clickedNextButton()
{
if(isChanging)
return;
prevButton->setEnabled(true);
isChanging = true;
setFixedSize(winSize.width(), winSize.height());
index++;
animation2->setTargetObject(frame[index]);
animationGroup->start();
if(index >= 9)
nextButton->setEnabled(false);
}
void Widget::animationFinished()
{
isChanging = false;
setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
animation1->setTargetObject(frame[index]);
}
//main.cpp
#include "tqt.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Widget *widget = new Widget;
widget->show();
return app.exec();
}
10张图片,按prev, next可前后切换