13
13
14
14
from PyQt5 .QtCore import QUrl , QRectF , Qt , QSize , QTimer , QPropertyAnimation ,\
15
15
QPointF , pyqtProperty
16
- from PyQt5 .QtGui import QPixmap , QColor , QPainter , QPainterPath
16
+ from PyQt5 .QtGui import QPixmap , QColor , QPainter , QPainterPath , QMovie
17
17
from PyQt5 .QtNetwork import QNetworkAccessManager , QNetworkDiskCache ,\
18
18
QNetworkRequest
19
19
from PyQt5 .QtWidgets import QWidget , qApp
@@ -40,8 +40,10 @@ def __init__(self, *args, shape=0, url='', cacheDir=False, size=QSize(64, 64), a
40
40
self ._angle = 0 # 角度
41
41
self .pradius = 0 # 加载进度条半径
42
42
self .animation = animation # 是否使用动画
43
+ self ._movie = None # 动态图
43
44
self ._pixmap = QPixmap () # 图片对象
44
45
self .pixmap = QPixmap () # 被绘制的对象
46
+ self .isGif = url .endswith ('.gif' )
45
47
# 进度动画定时器
46
48
self .loadingTimer = QTimer (self , timeout = self .onLoading )
47
49
# 旋转动画
@@ -92,7 +94,7 @@ def enterEvent(self, event):
92
94
"""鼠标进入动画
93
95
:param event:
94
96
"""
95
- if not self .animation :
97
+ if not ( self .animation and not self . isGif ) :
96
98
return
97
99
self .rotateAnimation .stop ()
98
100
cv = self .rotateAnimation .currentValue () or self .StartAngle
@@ -106,7 +108,7 @@ def leaveEvent(self, event):
106
108
"""鼠标离开动画
107
109
:param event:
108
110
"""
109
- if not self .animation :
111
+ if not ( self .animation and not self . isGif ) :
110
112
return
111
113
self .rotateAnimation .stop ()
112
114
cv = self .rotateAnimation .currentValue () or self .EndAngle
@@ -132,14 +134,21 @@ def onFinished(self):
132
134
self .loadingTimer .stop ()
133
135
self .pradius = 0
134
136
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 ()
143
152
144
153
def onError (self , code ):
145
154
"""下载出错了
@@ -206,6 +215,14 @@ def _resizePixmap(self):
206
215
self .width (), self .height (), Qt .IgnoreAspectRatio , Qt .SmoothTransformation )
207
216
self .update ()
208
217
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
+
209
226
def _initNetWork (self ):
210
227
"""初始化异步网络库
211
228
"""
@@ -245,8 +262,14 @@ def _get(self, url):
245
262
return
246
263
self .pradius = 0
247
264
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 ()
250
273
else :
251
274
self .onError ('' )
252
275
0 commit comments