RT,
用QT实现的一个简单动画,比较简单
所以,直接上代码,呵呵。
main.cpp
1 2 3 4 5 6 7 8 9 10 11 |
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); } |
widget.h
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 |
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QFrame> #include <QPushButton> #include <QPropertyAnimation> #include <QSequentialAnimationGroup> 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 // WIDGET_H |
widget.cpp
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 |
#include "widget.h" #include <QHBoxLayout> 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]); } |
本程序有10张图片,按prev, next可前后切换
发表评论
要发表评论,您必须先登录。