Skip to content

Commit 2de2b65

Browse files
Android GIF fixes
1 parent 6718025 commit 2de2b65

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

source/FFImageLoading.Droid/Targets/ImageViewTarget.cs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace FFImageLoading.Targets
1010
{
1111
public class ImageViewTarget : ViewTarget<ImageView>
1212
{
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>>();
1414

1515
public ImageViewTarget(ImageView control) : base(control)
1616
{
@@ -20,32 +20,37 @@ private static void PlayAnimation(ImageView control, ISelfDisposingAnimatedBitma
2020
{
2121
lock (_runningAnimations)
2222
{
23-
StopAnimation(control);
23+
var hashCode = control.GetHashCode();
2424

2525
var timer = new HighResolutionTimer<ISelfDisposingAnimatedBitmapDrawable>(drawable, async (t, bitmap) =>
2626
{
2727
try
2828
{
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
3830
{
3931
if (control == null || control.Handle == IntPtr.Zero || !t.Enabled
4032
|| bitmap == null || bitmap.Handle == IntPtr.Zero || bitmap.IsRecycled
4133
|| !t.AnimatedDrawable.IsValidAndHasValidBitmap())
4234
{
43-
StopAnimation(control);
4435
return;
4536
}
4637

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+
}
4954
}
5055
catch (Exception ex)
5156
{
@@ -56,21 +61,21 @@ await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() =>
5661
DelayOffset = -2
5762
};
5863

59-
_runningAnimations.Add(control, timer);
64+
_runningAnimations.Add(hashCode, timer);
6065

6166
timer.Start();
6267
}
6368
}
6469

65-
private static void StopAnimation(ImageView control)
70+
private static void StopAnimation(int hashCode)
6671
{
6772
lock (_runningAnimations)
6873
{
69-
if (_runningAnimations.TryGetValue(control, out var timer) && timer != null)
74+
if (_runningAnimations.TryGetValue(hashCode, out var timer))
7075
{
76+
_runningAnimations.Remove(hashCode);
7177
timer?.Stop();
7278
UpdateDrawableDisplayedState(timer.AnimatedDrawable as Drawable, false);
73-
_runningAnimations.Remove(control);
7479
}
7580
}
7681
}
@@ -79,14 +84,15 @@ private static void Set(ImageView control, SelfDisposingBitmapDrawable drawable)
7984
{
8085
lock (control)
8186
{
82-
StopAnimation(control);
87+
StopAnimation(control.GetHashCode());
8388

8489
if (drawable == null)
8590
{
8691
control.SetImageResource(Android.Resource.Color.Transparent);
8792
return;
8893
}
89-
else if (drawable is ISelfDisposingAnimatedBitmapDrawable animatedBitmapDrawable)
94+
95+
if (drawable is ISelfDisposingAnimatedBitmapDrawable animatedBitmapDrawable)
9096
{
9197
UpdateDrawableDisplayedState(drawable, true);
9298
control.SetImageDrawable(animatedBitmapDrawable as Drawable);

0 commit comments

Comments
 (0)