zoukankan      html  css  js  c++  java
  • Qt 动画快速入门(一)

    Qt-4.6动画Animation快速入门三字决


    Qt-4.6新增了Animation Framework(动画框架),让我们能够方便的写一些生动的程序。不必像以前的版本一样,所有的控件都枯燥的呆在伟大光荣的QLayout里,也许它们可以唱个歌,跳个舞。
        所谓动画就是在一个时间段内的不同时间点有不同的状态,只要定义好这样状态,实现动画就是水到渠成的事情。当然做这件事情,最好用的就是状态机,没错Qt-4.6.0提供了QStateMachine类,不过今天我要讲的三字决要简单一些。

    第一决:QPropertyAnimation

    QPropertyAnimation用于和QObject中的属性properties进行通信,比如QWidget的大小,坐标等。来看代码

    QPropertyAnimation *animation = new QPropertyAnimation(myWidget, “geometry”);
    animation->setDuration(10000);
    animation->setStartValue(QRect(0, 0, 100, 30));
    animation->setEndValue(QRect(250, 250, 100, 30));
    animation->start();


    第一行创建的QPropertyAnimation对象关联了myWidget这个窗体的几何属性。后面的几句分别设置了这个动画的时长,起始坐标和结束坐标。剩下的事情就交改QProperAnimation去做就行了。然后调用start()启动它。没错,五行代码就完成了一个完成了一个自动从一个坐标点移动到另一个坐标点的窗体。下面我给出一个可以运行的代码,是一只小鸟从下角移到中间的一个小动画,当然你得自己准备这个同名的图片:)

    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QPixmap>
    4. #include <QPropertyAnimation>
    5. int main(int argc,char *argv[]){
    6.     QApplication app(argc,argv);
    7.     QWidget *w=new QWidget();
    8.     w->resize(300,400);
    9.     QPixmap birdimg=QPixmap(”twitter-bird.png”).scaled(40,40);
    10.     QLabel *bird_1=new QLabel(w);
    11.     bird_1->setPixmap(birdimg);
    12.     QPropertyAnimation *anim1=new QPropertyAnimation(bird_1, “pos”);
    13.     anim1->setDuration(2000);
    14.     anim1->setStartValue(QPoint(0, 360));
    15.     anim1->setEndValue(QPoint(110, 180));
    16.     anim1->start();
    17.     bird_1->move(-40,-40);
    18.     w->show();
    19.     return app.exec();
    20. }


    上面的例子使用了label的位置属性pos。当然你可以在自己的类里增加其它property的,比如让颜色在变。

    第二决:setEasingCurve

    上面那个例子中小鸟的移动是线性的,未免太单调了点。QPropertyAnimation中的void setEasingCurve (const QEasingCurve & easing)函数正是用于实现不同的曲率变化的,QEasingCurve可用的参数列表(包括函数曲线图)可在文档中查到 。将上面动画相关的代码部分改成

    QPropertyAnimation *anim1=new QPropertyAnimation(bird_1, “pos”);
    anim1->setDuration(2000);
    anim1->setStartValue(QPoint(0, 360));
    anim1->setEndValue(QPoint(110, 180));
    anim1->setEasingCurve(QEasingCurve::OutBounce);
    anim1->start();


    注意,新增的第四句。并且试试其它曲线参数,然后运行,看到的动态效果是不是不一样了。如果你对列表里已经有的曲线都不满意,你还可以继承QEasingCurve,实现你需要的效果。

    第三决:QAnimationGroup

    前面的例子是只有一个动画在运行,如果想多个动画一起运行的话,那就要用到动画组QAnimationGroup了。动画组分为两种分别为串行和并行,对应于QAnimationGroup的两个子类QSequentialAnimationGroup和QParallelAnimationGroup。其用法很简单

    QSequentialAnimationGroup group;
    //QParallelAnimationGroup group;
    group.addAnimation(anim1);
    group.addAnimation(anim2);
    group.start();


    上面的代码,如果是串行的话,那么动画anim1运行之后,才会运行anim2。如果是并行的话,两个动画是同时运行的。如果加了动画组,那么单个anim1->start()就没必要再单独调用了,由动画组来管理。 下面是一个可运行的代码,两只小鸟分别从窗体左上角和右下角移动到中间。

    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QLabel>
    4. #include <QPixmap>
    5. #include <QPropertyAnimation>
    6. #include <QSequentialAnimationGroup>
    7. #include <QParallelAnimationGroup>
    8. int main(int argc,char *argv[]){
    9.     QApplication app(argc,argv);
    10.     QWidget *w=new QWidget();
    11.     w->resize(300,400);
    12.     QPixmap birdimg=QPixmap(”twitter-bird.png”).scaled(40,40);
    13.     QLabel *bird_1=new QLabel(w);
    14.     bird_1->setPixmap(birdimg);
    15.     QPropertyAnimation *anim1=new QPropertyAnimation(bird_1, “pos”);
    16.     anim1->setDuration(2000);
    17.     anim1->setStartValue(QPoint(0, 360));
    18.     anim1->setEndValue(QPoint(110, 180));
    19.     //anim1->setEasingCurve(QEasingCurve::OutBounce);
    20.     anim1->start();
    21.     QLabel *bird_2=new QLabel(w);
    22.     bird_2->setPixmap(birdimg);
    23.     QPropertyAnimation *anim2=new QPropertyAnimation(bird_2, “pos”);
    24.     anim2->setDuration(2000);
    25.     anim2->setStartValue(QPoint(0, 0));
    26.     anim2->setEndValue(QPoint(150, 180));
    27.     anim2->setEasingCurve(QEasingCurve::OutBounce);
    28.     QSequentialAnimationGroup group;
    29.     //QParallelAnimationGroup group;
    30.     group.addAnimation(anim1);
    31.     group.addAnimation(anim2);
    32.     group.start();
    33.     bird_1->move(-40,-40);
    34.     bird_2->move(-40,-40);
    35.     w->show();
    36.     return app.exec();
    37. }


    文章的源在:http://docs.google.com/View?id=dhhvrcmh_100m5xs7wf3,如有更新可能会反映在那边

    http://www.qtcn.org/bbs/read-htm-tid-34061.html

  • 相关阅读:
    SQL Server, Timeout expired.all pooled connections were in use and max pool size was reached
    javascript 事件调用顺序
    Best Practices for Speeding Up Your Web Site
    C语言程序设计 使用VC6绿色版
    破解SQL Prompt 3.9的几步操作
    Master page Path (MasterPage 路径)
    几个小型数据库的比较
    CSS+DIV 完美实现垂直居中的方法
    由Response.Redirect引发的"Thread was being aborted. "异常的处理方法
    Adsutil.vbs 在脚本攻击中的妙用
  • 原文地址:https://www.cnblogs.com/findumars/p/6181747.html
Copyright © 2011-2022 走看看