技术更新(GIF图像刷新解析)
1、最开始用的QMovie做了个demo,效果差强人意。
2、最后通过几个qt大佬的指点总结,用定时器来完成。
感谢
雨田大佬和
lgao622的技术指点
具体操作步骤:
1、首先文本(包括表情)数据肯定是用QTextDocument来缓存。
2、判断文本数据是否包含gif图像资源,如果有,用QImageReader将图像文件每帧图像缓存到map映射表中,同时需要注意将每下一帧的延迟累加做成一个时间轴,每个时间点插入当前帧的图像,完事用QMap缓存。
3、列表里面启动一个定时器,间隔10毫秒。
4、遍历item的gif缓存数据,每到一个时间点抓取当前帧的图像,修改textdocumet的resouce资源,然后重新update下列表完成。
代码片段:
图像解析
- /**
- * @brief BubbleItemInfo::ReadGifPixmaps
- * 读取Gif图像文件每帧的图像数据,缓存到队列
- * @param strName
- */
- void BubbleItemInfo::ReadGifPixmaps(const QString &strName)
- {
- if (strName.isEmpty()) return;
- QImageReader reader;
- reader.setFileName(strName);
- if (reader.supportsAnimation())
- {
- QMap<int,QPixmap> pixmaps;
- int nCount = reader.imageCount();
- int ndelay = 0;
- for (int i = 0; i < nCount; i++) {
- // 跳到顺序号为i的图像
- reader.jumpToImage(i);
- // 读取图像
- if (reader.canRead())
- {
- QImage image = reader.read();
- ndelay += reader.nextImageDelay();
- // image.save(QString("ttt_%1.png").arg(ndelay));
- pixmaps.insert(ndelay, QPixmap::fromImage(image));
- }
- }
- m_gifIndex.insert(strName, 0);
- m_gifInfos.insert(strName, pixmaps);
- }
- }
遍历刷新:
- /**
- * @brief BubbleItemInfo::RefreshDocumentResource
- * @param bOk 返回true,表示有刷新
- */
- void BubbleItemInfo::RefreshDocumentResource(bool &bOk)
- {
- for (int i = 0; i < m_listEmotionUrl.size(); i++) {
- QString strFile = m_listEmotionUrl.at(i);
- if (!m_gifInfos.contains(strFile)) continue;
- int index = m_gifIndex.value(strFile);
- QMap<int, QPixmap> pixmaps = m_gifInfos.value(strFile);
- if (index > pixmaps.lastKey()) {
- index = 0;
- }
- // 更新time id,累加,步进10
- m_gifIndex.insert(strFile, index + 10);
- if (pixmaps.contains(index)) {
- QPixmap pixmap = pixmaps.value(index);
- if (pixmap.isNull()) { continue; }
- bOk = true;
- m_document->addResource(QTextDocument::ImageResource, QUrl(strFile), pixmap);
- } /*else if (0 == index && pixmaps.size() > 2) {
- bOk = true;
- m_document->addResource(QTextDocument::ImageResource, QUrl(strFile), pixmaps.first());
- }*/
- }
- }