QPropertyAnimation(动画类)
本源码主要用到的类↑,利用其可实现各种关于控件的动画
好的,废话不多说,上效果图
大家看到图片这样的排列方式也能大概明白他的动画效果是怎样的了吧,没错,只要按下">"按钮就可以将第一张图片放大并移动到第二张的位置而第二张则放大并移动到第三张的位置,第三张,也就是主要展示的图片了,对于它的操作与前面相反,是缩小并移动到第四张的位置,第四张灰到第五张处,第五张屁颠屁颠的补到第一张的位置.
无码不欢,来人,上码!
mainwindow.cpp:
#include "mainwindow.h"
#include
#include
#include
#include
#include
#define num 350
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->resize(500,600);
w=QApplication::desktop()->availableGeometry().width();
h=QApplication::desktop()->availableGeometry().height();
w=500;//放到移动端时删除此句和下一句
h=600;
//按比例计算label大小 位置↓
int x=(w-(2.5/11*h))/2;
int y=(3.0/11)*h;
double wh=2.5/11*h;
r3.setRect(x,y,wh,wh);
label3=new QLabel(this);
label3->setGeometry(x,y,wh,wh);
label3->setPixmap(QPixmap(QString(":/image/007-2.png")));
label3->setScaledContents(true);
qDebug()<setGeometry(x,y,wh,wh);
label2->setPixmap(QPixmap(QString(":/image/007-1.png")));
label2->setScaledContents(true);
r4.setRect(x+2.5/11*h,y,wh,wh);
label4=new QLabel(this);
label4->setGeometry(x+2.5/11*h,y,wh,wh);
label4->setPixmap(QPixmap(QString(":/image/007-3.png")));
label4->setScaledContents(true);
qDebug()<setGeometry(x,y,int(wh),int(wh));
label1->setPixmap(QPixmap(QString(":/image/007-4.png")));
label1->setScaledContents(true);
r5.setRect(x+2.5/11*h+2.5/11*h*0.8,y,wh,wh);
label5=new QLabel(this);
label5->setGeometry(x+2.5/11*h+2.5/11*h*0.8,y,(int)wh,(int)wh);
label5->setPixmap(QPixmap(QString(":/image/007-5.png")));
label5->setScaledContents(true);
label2->raise();
label4->raise();
label3->raise();
//设置Button的位置
btn_left= new QPushButton("<",this);
btn_left->setGeometry(r3.x(),r3.y()+r3.height()*1.3,r3.height()/3.0,r3.height()/3.0);
btn_right= new QPushButton(">",this);
btn_right->setGeometry(r3.x()+r3.height()*2/3.0,r3.y()+r3.height()*1.3,r3.height()/3.0,r3.height()/3.0);
connect(btn_left,SIGNAL(clicked(bool)),this,SLOT(move_left()));
connect(btn_right,SIGNAL(clicked(bool)),this,SLOT(move_right()));
mainPhoto=3;
}
MainWindow::~MainWindow()
{
}
void MainWindow::move_right()//动画开始
{
mainPhoto+=1;
qDebug()<<"move_left";
moveaction=new QPropertyAnimation(label1,"geometry");//设置移动对象
moveaction->setDuration(num);//完成时间
moveaction->setStartValue(r1);//开始位置
moveaction->setEndValue(r5);//结束位置
moveaction->setEasingCurve(QEasingCurve::Linear);//设置移动方式
moveaction->start();//开始移动
moveaction2=new QPropertyAnimation(label5,"geometry");
moveaction2->setDuration(num);
moveaction2->setStartValue(r5);
moveaction2->setEndValue(r4);
moveaction2->setEasingCurve(QEasingCurve::Linear);
moveaction2->start();
moveaction3=new QPropertyAnimation(label4,"geometry");
moveaction3->setDuration(num);
moveaction3->setStartValue(r4);
moveaction3->setEndValue(r3);
moveaction3->setEasingCurve(QEasingCurve::Linear);
moveaction3->start();
moveaction4=new QPropertyAnimation(label3,"geometry");
moveaction4->setDuration(num);
moveaction4->setStartValue(r3);
moveaction4->setEndValue(r2);
moveaction4->setEasingCurve(QEasingCurve::Linear);
moveaction4->start();
moveaction5=new QPropertyAnimation(label2,"geometry");
moveaction5->setDuration(num);
moveaction5->setStartValue(r2);
moveaction5->setEndValue(r1);
moveaction5->setEasingCurve(QEasingCurve::Linear);
moveaction5->start();
QRect rect(r5);
r5=r4;
r4=r3;
r3=r2;
r2=r1;
r1=rect;
//以下用作调整label显示次序
if(mainPhoto>=6)
mainPhoto=1;
if(mainPhoto==4)
{
qDebug()<<"4";
label5->raise();
label3->raise();
label4->raise();
}else if(mainPhoto==5)
{
qDebug()<<"5";
label1->raise();
label4->raise();
label5->raise();
}else if(mainPhoto==1)
{
qDebug()<<"1";
label2->raise();
label5->raise();
label1->raise();
}else if(mainPhoto==2)
{
qDebug()<<"2";
label3->raise();
label1->raise();
label2->raise();
}else if(mainPhoto==3)
{
qDebug()<<"3";
label4->raise();
label2->raise();
label3->raise();
}
}
void MainWindow::move_left()//动画开始
{
mainPhoto-=1;
qDebug()<<"move_right";
moveaction=new QPropertyAnimation(label1,"geometry");
moveaction->setDuration(num);
moveaction->setStartValue(r1);
moveaction->setEndValue(r2);
moveaction->setEasingCurve(QEasingCurve::Linear);
moveaction->start();
moveaction2=new QPropertyAnimation(label5,"geometry");
moveaction2->setDuration(num);
moveaction2->setStartValue(r5);
moveaction2->setEndValue(r1);
moveaction2->setEasingCurve(QEasingCurve::Linear);
moveaction2->start();
moveaction3=new QPropertyAnimation(label4,"geometry");
moveaction3->setDuration(num);
moveaction3->setStartValue(r4);
moveaction3->setEndValue(r5);
moveaction3->setEasingCurve(QEasingCurve::Linear);
moveaction3->start();
moveaction4=new QPropertyAnimation(label3,"geometry");
moveaction4->setDuration(num);
moveaction4->setStartValue(r3);
moveaction4->setEndValue(r4);
moveaction4->setEasingCurve(QEasingCurve::Linear);
moveaction4->start();
moveaction5=new QPropertyAnimation(label2,"geometry");
moveaction5->setDuration(num);
moveaction5->setStartValue(r2);
moveaction5->setEndValue(r3);
moveaction5->setEasingCurve(QEasingCurve::Linear);
moveaction5->start();
QRect rect(r1);
r1=r2;
r2=r3;
r3=r4;
r4=r5;
r5=rect;
//以下用作调整label显示次序
if(mainPhoto<=0)
mainPhoto=5;
if(mainPhoto==4)
{
qDebug()<<"4";
label5->raise();
label3->raise();
label4->raise();
}else if(mainPhoto==5)
{
qDebug()<<"5";
label1->raise();
label4->raise();
label5->raise();
}else if(mainPhoto==1)
{
qDebug()<<"1";
label2->raise();
label5->raise();
label1->raise();
}else if(mainPhoto==2)
{
qDebug()<<"2";
label3->raise();
label1->raise();
label2->raise();
}else if(mainPhoto==3)
{
qDebug()<<"3";
label4->raise();
label2->raise();
label3->raise();
}
}
源码下载地址:http://pan.baidu.com/s/1nu3Xyhb