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";
            }
        }
    }
}

二、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";
            }
        }
    }
}

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";
            }
        }
    }
}

总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值