@@ -10,7 +10,7 @@ namespace FFImageLoading.Targets
10
10
{
11
11
public class ImageViewTarget : ViewTarget < ImageView >
12
12
{
13
- private static readonly Dictionary < ImageView , HighResolutionTimer < ISelfDisposingAnimatedBitmapDrawable > > _runningAnimations = new Dictionary < ImageView , HighResolutionTimer < ISelfDisposingAnimatedBitmapDrawable > > ( ) ;
13
+ private static readonly Dictionary < int , HighResolutionTimer < ISelfDisposingAnimatedBitmapDrawable > > _runningAnimations = new Dictionary < int , HighResolutionTimer < ISelfDisposingAnimatedBitmapDrawable > > ( ) ;
14
14
15
15
public ImageViewTarget ( ImageView control ) : base ( control )
16
16
{
@@ -20,32 +20,37 @@ private static void PlayAnimation(ImageView control, ISelfDisposingAnimatedBitma
20
20
{
21
21
lock ( _runningAnimations )
22
22
{
23
- StopAnimation ( control ) ;
23
+ var hashCode = control . GetHashCode ( ) ;
24
24
25
25
var timer = new HighResolutionTimer < ISelfDisposingAnimatedBitmapDrawable > ( drawable , async ( t , bitmap ) =>
26
26
{
27
27
try
28
28
{
29
- if ( control == null || control . Handle == IntPtr . Zero || ! t . Enabled
30
- || bitmap == null || bitmap . Handle == IntPtr . Zero || bitmap . IsRecycled
31
- || ! t . AnimatedDrawable . IsValidAndHasValidBitmap ( ) )
32
- {
33
- StopAnimation ( control ) ;
34
- return ;
35
- }
36
-
37
- await ImageService . Instance . Config . MainThreadDispatcher . PostAsync ( ( ) =>
29
+ try
38
30
{
39
31
if ( control == null || control . Handle == IntPtr . Zero || ! t . Enabled
40
32
|| bitmap == null || bitmap . Handle == IntPtr . Zero || bitmap . IsRecycled
41
33
|| ! t . AnimatedDrawable . IsValidAndHasValidBitmap ( ) )
42
34
{
43
- StopAnimation ( control ) ;
44
35
return ;
45
36
}
46
37
47
- control . SetImageBitmap ( bitmap ) ;
48
- } ) . ConfigureAwait ( false ) ;
38
+ await ImageService . Instance . Config . MainThreadDispatcher . PostAsync ( ( ) =>
39
+ {
40
+ if ( control == null || control . Handle == IntPtr . Zero || ! t . Enabled
41
+ || bitmap == null || bitmap . Handle == IntPtr . Zero || bitmap . IsRecycled
42
+ || ! t . AnimatedDrawable . IsValidAndHasValidBitmap ( ) )
43
+ {
44
+ return ;
45
+ }
46
+
47
+ control . SetImageBitmap ( bitmap ) ;
48
+ } ) . ConfigureAwait ( false ) ;
49
+ }
50
+ catch ( ObjectDisposedException )
51
+ {
52
+ StopAnimation ( hashCode ) ;
53
+ }
49
54
}
50
55
catch ( Exception ex )
51
56
{
@@ -56,21 +61,21 @@ await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() =>
56
61
DelayOffset = - 2
57
62
} ;
58
63
59
- _runningAnimations . Add ( control , timer ) ;
64
+ _runningAnimations . Add ( hashCode , timer ) ;
60
65
61
66
timer . Start ( ) ;
62
67
}
63
68
}
64
69
65
- private static void StopAnimation ( ImageView control )
70
+ private static void StopAnimation ( int hashCode )
66
71
{
67
72
lock ( _runningAnimations )
68
73
{
69
- if ( _runningAnimations . TryGetValue ( control , out var timer ) && timer != null )
74
+ if ( _runningAnimations . TryGetValue ( hashCode , out var timer ) )
70
75
{
76
+ _runningAnimations . Remove ( hashCode ) ;
71
77
timer ? . Stop ( ) ;
72
78
UpdateDrawableDisplayedState ( timer . AnimatedDrawable as Drawable , false ) ;
73
- _runningAnimations . Remove ( control ) ;
74
79
}
75
80
}
76
81
}
@@ -79,14 +84,15 @@ private static void Set(ImageView control, SelfDisposingBitmapDrawable drawable)
79
84
{
80
85
lock ( control )
81
86
{
82
- StopAnimation ( control ) ;
87
+ StopAnimation ( control . GetHashCode ( ) ) ;
83
88
84
89
if ( drawable == null )
85
90
{
86
91
control . SetImageResource ( Android . Resource . Color . Transparent ) ;
87
92
return ;
88
93
}
89
- else if ( drawable is ISelfDisposingAnimatedBitmapDrawable animatedBitmapDrawable )
94
+
95
+ if ( drawable is ISelfDisposingAnimatedBitmapDrawable animatedBitmapDrawable )
90
96
{
91
97
UpdateDrawableDisplayedState ( drawable , true ) ;
92
98
control . SetImageDrawable ( animatedBitmapDrawable as Drawable ) ;
0 commit comments