• QML Image、AnimatedImage 加载 Gif动图



    前言

    Image、AnimatedImage 加载 Gif动图,以及AnimatedImage加载Gif是否缓存会导致的问题


    一、Image

    使用Image加载Gif,显示的只是一张图片

    import QtQuick 2.15
    import QtQuick.Controls 2.5
    
    Item {
        id: form09
        width: 480
        height: 320
        Rectangle {
            id: text
            anchors.fill: parent
    
            // 用来显示一个等待图元
            BusyIndicator {
                id: busy
                running: true
                anchors.centerIn: parent
                z: 2
            }
    
            Text {
                id: stateLabel
                visible: false
                anchors.centerIn: parent
                z: 3
            }
    
            //AnimatedImage
            Image {
                id: imageViewer
                // 开启异步加载模式,专门使用一个线程来加载图片
                asynchronous: true
                // 图片较大的情况下,指定不缓存图像(cache默认为true)
                //cache: false
                anchors.fill: parent
                // 设置图片的填充模式为“等比缩放”
                fillMode: Image.PreserveAspectFit
                onStatusChanged: {
                    if (imageViewer.status === Image.Loading) {
                        busy.running = true;
                        stateLabel.visible = false
                    }
                    else if(imageViewer.status === Image.Ready){
                        busy.running = false;
                    }
                    else if(imageViewer.status === Image.Error) {
                        busy.running = false;
                        stateLabel.visible = true
                        stateLabel.text = "Error"
                    }
                    else if(imageViewer.status === Image.Null)
                    {
                        busy.running = false;
                        stateLabel.visible = true
                        stateLabel.text = "no image has been set"
                    }
                }
    
                // 组件加载完成再加载图片
                Component.onCompleted: {
                    imageViewer.source = "https://hbimg.b0.upaiyun.com/b5f77dca7e52921077df1adba22fb47aa4a9fae313818f-2kLYrw_fw658";
                }
            }
        }
    }
    
    • 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

    二、AnimatedImage

    1. cache = false

    gif正常显示,但仅显示一次,保持最后一帧的图像

    import QtQuick 2.15
    import QtQuick.Controls 2.5
    
    Item {
        id: form09
        width: 480
        height: 320
        Rectangle {
            id: text
            anchors.fill: parent
    
            // 用来显示一个等待图元
            BusyIndicator {
                id: busy
                running: true
                anchors.centerIn: parent
                z: 2
            }
    
            Text {
                id: stateLabel
                visible: false
                anchors.centerIn: parent
                z: 3
            }
    
            //AnimatedImage
            Image {
                id: imageViewer
                // 开启异步加载模式,专门使用一个线程来加载图片
                asynchronous: true
                // 图片较大的情况下,指定不缓存图像(cache默认为true)
                cache: false
                anchors.fill: parent
                // 设置图片的填充模式为“等比缩放”
                fillMode: Image.PreserveAspectFit
                onStatusChanged: {
                    if (imageViewer.status === Image.Loading) {
                        busy.running = true;
                        stateLabel.visible = false
                    }
                    else if(imageViewer.status === Image.Ready){
                        busy.running = false;
                    }
                    else if(imageViewer.status === Image.Error) {
                        busy.running = false;
                        stateLabel.visible = true
                        stateLabel.text = "Error"
                    }
                    else if(imageViewer.status === Image.Null)
                    {
                        busy.running = false;
                        stateLabel.visible = true
                        stateLabel.text = "no image has been set"
                    }
                }
    
                // 组件加载完成再加载图片
                Component.onCompleted: {
                    imageViewer.source = "https://hbimg.b0.upaiyun.com/b5f77dca7e52921077df1adba22fb47aa4a9fae313818f-2kLYrw_fw658";
                }
            }
        }
    }
    
    • 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

    2. cache = true(默认为true)

    gif 持续显示

    import QtQuick 2.15
    import QtQuick.Controls 2.5
    
    Item {
        id: form09
        width: 480
        height: 320
        Rectangle {
            id: text
            anchors.fill: parent
    
            // 用来显示一个等待图元
            BusyIndicator {
                id: busy
                running: true
                anchors.centerIn: parent
                z: 2
            }
    
            Text {
                id: stateLabel
                visible: false
                anchors.centerIn: parent
                z: 3
            }
    
            AnimatedImage
            //Image
            {
                id: imageViewer
                // 开启异步加载模式,专门使用一个线程来加载图片
                asynchronous: true
                // 图片较大的情况下,指定不缓存图像(cache默认为true)
                //cache: false
                anchors.fill: parent
                // 设置图片的填充模式为“等比缩放”
                fillMode: Image.PreserveAspectFit
                onStatusChanged: {
                    if (imageViewer.status === Image.Loading) {
                        busy.running = true;
                        stateLabel.visible = false
                    }
                    else if(imageViewer.status === Image.Ready){
                        busy.running = false;
                    }
                    else if(imageViewer.status === Image.Error) {
                        busy.running = false;
                        stateLabel.visible = true
                        stateLabel.text = "Error"
                    }
                    else if(imageViewer.status === Image.Null)
                    {
                        busy.running = false;
                        stateLabel.visible = true
                        stateLabel.text = "no image has been set"
                    }
                }
    
                // 组件加载完成再加载图片
                Component.onCompleted: {
                    imageViewer.source = "https://hbimg.b0.upaiyun.com/b5f77dca7e52921077df1adba22fb47aa4a9fae313818f-2kLYrw_fw658";
                }
            }
        }
    }
    
    • 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

    总结

  • 相关阅读:
    【C/C++】回调函数详解&注册窗口类&LRESULT & CALLBACK详解以及游戏中的应用
    德国金融监管机构网站遭遇大规模DDoS攻击后“瘫痪”
    1712A 300A嵌入式电源系统
    第二章:Cyber RT通信机制解析与实践
    计算机毕业设计Java无人智慧药柜系统设计(源码+系统+mysql数据库+Lw文档)
    uniapp 添加定位权限及弹出权限弹框
    设计模式——19. 访问者模式
    【vsCode + Arduino】在Visual Studio Code编译Arduino项目
    Oracle第二篇:删除索引提示ORA-01408:索引不存在
    Java新特性(2):Java 10以后
  • 原文地址:https://blog.csdn.net/qq_44084616/article/details/134533541