Skip to content

Commit d6486db

Browse files
committed
支持gif
1 parent b54a88a commit d6486db

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

CustomWidgets/CAvatar.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from PyQt5.QtCore import QUrl, QRectF, Qt, QSize, QTimer, QPropertyAnimation,\
1515
QPointF, pyqtProperty
16-
from PyQt5.QtGui import QPixmap, QColor, QPainter, QPainterPath
16+
from PyQt5.QtGui import QPixmap, QColor, QPainter, QPainterPath, QMovie
1717
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkDiskCache,\
1818
QNetworkRequest
1919
from PyQt5.QtWidgets import QWidget, qApp
@@ -40,8 +40,10 @@ def __init__(self, *args, shape=0, url='', cacheDir=False, size=QSize(64, 64), a
4040
self._angle = 0 # 角度
4141
self.pradius = 0 # 加载进度条半径
4242
self.animation = animation # 是否使用动画
43+
self._movie = None # 动态图
4344
self._pixmap = QPixmap() # 图片对象
4445
self.pixmap = QPixmap() # 被绘制的对象
46+
self.isGif = url.endswith('.gif')
4547
# 进度动画定时器
4648
self.loadingTimer = QTimer(self, timeout=self.onLoading)
4749
# 旋转动画
@@ -92,7 +94,7 @@ def enterEvent(self, event):
9294
"""鼠标进入动画
9395
:param event:
9496
"""
95-
if not self.animation:
97+
if not (self.animation and not self.isGif):
9698
return
9799
self.rotateAnimation.stop()
98100
cv = self.rotateAnimation.currentValue() or self.StartAngle
@@ -106,7 +108,7 @@ def leaveEvent(self, event):
106108
"""鼠标离开动画
107109
:param event:
108110
"""
109-
if not self.animation:
111+
if not (self.animation and not self.isGif):
110112
return
111113
self.rotateAnimation.stop()
112114
cv = self.rotateAnimation.currentValue() or self.EndAngle
@@ -132,14 +134,21 @@ def onFinished(self):
132134
self.loadingTimer.stop()
133135
self.pradius = 0
134136
reply = self.sender()
135-
data = reply.readAll().data()
136-
reply.deleteLater()
137-
del reply
138-
self._pixmap.loadFromData(data)
139-
if self._pixmap.isNull():
140-
self._pixmap = QPixmap(self.size())
141-
self._pixmap.fill(QColor(204, 204, 204))
142-
self._resizePixmap()
137+
138+
if self.isGif:
139+
self._movie = QMovie(reply, b'gif', self)
140+
if self._movie.isValid():
141+
self._movie.frameChanged.connect(self._resizeGifPixmap)
142+
self._movie.start()
143+
else:
144+
data = reply.readAll().data()
145+
reply.deleteLater()
146+
del reply
147+
self._pixmap.loadFromData(data)
148+
if self._pixmap.isNull():
149+
self._pixmap = QPixmap(self.size())
150+
self._pixmap.fill(QColor(204, 204, 204))
151+
self._resizePixmap()
143152

144153
def onError(self, code):
145154
"""下载出错了
@@ -206,6 +215,14 @@ def _resizePixmap(self):
206215
self.width(), self.height(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
207216
self.update()
208217

218+
def _resizeGifPixmap(self, _):
219+
"""缩放动画图片
220+
"""
221+
if self._movie:
222+
self.pixmap = self._movie.currentPixmap().scaled(
223+
self.width(), self.height(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
224+
self.update()
225+
209226
def _initNetWork(self):
210227
"""初始化异步网络库
211228
"""
@@ -245,8 +262,14 @@ def _get(self, url):
245262
return
246263
self.pradius = 0
247264
if os.path.exists(url) and os.path.isfile(url):
248-
self._pixmap = QPixmap(url)
249-
self._resizePixmap()
265+
if self.isGif:
266+
self._movie = QMovie(url, parent=self)
267+
if self._movie.isValid():
268+
self._movie.frameChanged.connect(self._resizeGifPixmap)
269+
self._movie.start()
270+
else:
271+
self._pixmap = QPixmap(url)
272+
self._resizePixmap()
250273
else:
251274
self.onError('')
252275

TestCAvatar.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def __init__(self, *args, **kwargs):
3737
layout.addWidget(CAvatar(
3838
self, shape=CAvatar.Rectangle, url='TestData/example-3.jpg', size=QSize(48, 48)))
3939

40+
# 本地gif, 不支持animation
41+
layout.addWidget(CAvatar(
42+
self, shape=CAvatar.Circle, url='TestData/example-1.gif'))
43+
4044
# 网络头像
4145
layout.addWidget(CAvatar(
4246
self, shape=CAvatar.Rectangle,
@@ -48,6 +52,10 @@ def __init__(self, *args, **kwargs):
4852
layout.addWidget(CAvatar(
4953
self, shape=CAvatar.Rectangle,
5054
url='https://www.thiswaifudoesnotexist.net/example.jpg'))
55+
# 加载网络gif
56+
layout.addWidget(CAvatar(
57+
self, shape=CAvatar.Rectangle, size=CAvatar.SizeLarge,
58+
url='https://n.sinaimg.cn/tech/transform/761/w404h357/20190923/d71b-iewtena6804980.gif'))
5159

5260

5361
if __name__ == '__main__':

TestData/example-1.gif

710 KB
Loading

0 commit comments

Comments
 (0)