• 初识Qt简单动画


    Qt提供了类QPropertyAnimation来实现图片的一些简单的动画操作效果。

    1、新建一个Qt空项目,同时添加资源文件,并在资源文件中添加图片路径。之后在main.cpp函数中添加以下代码

     1 #include<QApplication>
     2 #include<QLabel>
     3 #include<QPixmap>
     4 #include<QPropertyAnimation>
     5 
     6 int main(int argc, char *argv[])
     7 {
     8     QApplication app(argc, argv);
     9     QWidget *w= new QWidget;//定义窗口部件
    10     w->resize(200, 200);
    11 
    12     QPixmap pixmap = QPixmap("linux.png").scaled(30, 40);//定义图片的宽高比例
    13     QLabel *label = new QLabel(w);//将之前设置的窗口定义为标签
    14     label->setPixmap(pixmap);//标签中放置图片
    15 
    16     QPropertyAnimation *anim=new QPropertyAnimation(label,"pos");//pos为label的位置属性
    17     anim->setDuration(5000);//移动时间为5000ms
    18     anim->setStartValue(QPoint(0, 160));//起始坐标,该坐标对应图片左上角
    19     anim->setEndValue(QPoint(170, 0));//终止坐标,该坐标对应图片左上角
    20     anim->setEasingCurve(QEasingCurve::OutBounce);//setEasingCurve ()函数选择运行轨迹曲线。该行删除后,运动轨迹为直线,无特效
    21     anim->start();//开始移动
    22 
    23     w->show();
    24     return app.exec();
    25 }

    2、运行程序之后,显示如下

    3、扩展:多个动画效果的实现

     当涉及到两幅图片甚者多幅图片的动画效果时,需要使用动画组类QSequentialAnimationGroup或QParallelAnimationGroup。前者实现的是串行动画效果,即多个动画一个接一个的发生,后者实现动画并行效果,即多个动画同时发生。

    1 QSequentialAnimationGroup group;//串行动画
    2 //QParallelAnimationGroup group;//并行动画
    3 group.addAnimation(anim1);//动画1
    4 group.addAnimation(anim2);//动画2
    5 group.start();

    文章参考来源:http://www.hnmade.com/bbs/thread-12332-1-1.html,感谢分享!

  • 相关阅读:
    HDU_5057_分块
    HYSBZ_2002_分块
    HDU_1166_树状数组
    HDU_5692_dfs序+线段树
    多重背包
    二进制中一的个数
    康托展开
    vector, map, queue,set常用总结
    错误票据
    高精度计算
  • 原文地址:https://www.cnblogs.com/peter-czhang/p/3418946.html
Copyright © 2020-2023  润新知