• 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

    总结

  • 相关阅读:
    IDEA的使用(五)IDEA中关联数据库(IntelliJ IDEA 2022.1.3版本)
    在 Python 中创建具有当前日期和时间的文件名
    基于java+SpringBoot+HTML+SqlServer游戏饰品交易网站的设计与实现(程序+论文)
    如何有效的禁止Google Chrome自动更新?
    公网使用PLSQL远程连接Oracle数据库【内网穿透】
    Go并发:使用sync.Pool来性能优化
    解决若依框架多次list查询时,分页失效问题
    每日汇评:捍卫 2318美元的支撑位对于黄金至关重要
    使用 Transformers 进行图分类
    Java实现拼图小游戏(4)—— 打乱图片(含二维数组知识点)
  • 原文地址:https://blog.csdn.net/qq_44084616/article/details/134533541