This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ImageElementManager.cs
185 lines (153 loc) · 5.7 KB
/
ImageElementManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System.ComponentModel;
using System.Threading.Tasks;
using Android.Widget;
using AScaleType = Android.Widget.ImageView.ScaleType;
using ARect = Android.Graphics.Rect;
using System;
using Xamarin.Forms.Internals;
#if __ANDROID_29__
using AViewCompat = AndroidX.Core.View.ViewCompat;
#else
using AViewCompat = Android.Support.V4.View.ViewCompat;
#endif
namespace Xamarin.Forms.Platform.Android.FastRenderers
{
public static class ImageElementManager
{
public static void Init(IVisualElementRenderer renderer)
{
renderer.ElementPropertyChanged += OnElementPropertyChanged;
renderer.ElementChanged += OnElementChanged;
if (renderer is ILayoutChanges layoutChanges)
layoutChanges.LayoutChange += OnLayoutChange;
}
static void OnLayoutChange(object sender, global::Android.Views.View.LayoutChangeEventArgs e)
{
if (sender is IVisualElementRenderer renderer && renderer.View is ImageView imageView)
AViewCompat.SetClipBounds(imageView, imageView.GetScaleType() == AScaleType.CenterCrop ? new ARect(0, 0, e.Right - e.Left, e.Bottom - e.Top) : null);
}
public static void Dispose(IVisualElementRenderer renderer)
{
renderer.ElementPropertyChanged -= OnElementPropertyChanged;
renderer.ElementChanged -= OnElementChanged;
if (renderer is ILayoutChanges layoutChanges)
layoutChanges.LayoutChange -= OnLayoutChange;
if (renderer is IImageRendererController imageRenderer)
imageRenderer.SetFormsAnimationDrawable(null);
if (renderer.View is ImageView imageView)
{
imageView.SetImageDrawable(null);
imageView.Reset();
}
}
async static void OnElementChanged(object sender, VisualElementChangedEventArgs e)
{
var renderer = (sender as IVisualElementRenderer);
var view = renderer.View as ImageView;
var newImageElementManager = e.NewElement as IImageElement;
var oldImageElementManager = e.OldElement as IImageElement;
var rendererController = renderer as IImageRendererController;
if (rendererController.IsDisposed)
return;
await TryUpdateBitmap(rendererController, view, newImageElementManager, oldImageElementManager);
if (rendererController.IsDisposed)
return;
UpdateAspect(rendererController, view, newImageElementManager, oldImageElementManager);
if (rendererController.IsDisposed)
return;
ElevationHelper.SetElevation(view, renderer.Element);
}
async static void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var renderer = (sender as IVisualElementRenderer);
var ImageElementManager = (IImageElement)renderer.Element;
var imageController = (IImageController)renderer.Element;
if (renderer?.View?.LayoutParameters == null &&(renderer is ILayoutChanges lc && lc.HasLayoutOccurred))
{
return;
}
if (e.IsOneOf(Image.SourceProperty, Button.ImageSourceProperty))
{
await TryUpdateBitmap(renderer as IImageRendererController, (ImageView)renderer.View, (IImageElement)renderer.Element).ConfigureAwait(false);
}
else if (e.Is(Image.AspectProperty))
{
UpdateAspect(renderer as IImageRendererController, (ImageView)renderer.View, (IImageElement)renderer.Element);
}
else if (e.Is(Image.IsAnimationPlayingProperty))
await StartStopAnimation(renderer, imageController, ImageElementManager).ConfigureAwait(false);
}
async static Task StartStopAnimation(
IVisualElementRenderer renderer,
IImageController imageController,
IImageElement imageElement)
{
IImageRendererController imageRendererController = renderer as IImageRendererController;
var view = renderer.View as ImageView;
if (imageRendererController.IsDisposed || imageElement == null || view == null || view.IsDisposed())
return;
if (imageElement.IsLoading)
return;
if (!(view.Drawable is FormsAnimationDrawable) && imageElement.IsAnimationPlaying)
await TryUpdateBitmap(imageRendererController, view, imageElement);
if (view.Drawable is FormsAnimationDrawable animation)
{
if (imageElement.IsAnimationPlaying && !animation.IsRunning)
animation.Start();
else if (!imageElement.IsAnimationPlaying && animation.IsRunning)
animation.Stop();
}
}
async static Task TryUpdateBitmap(IImageRendererController rendererController, ImageView Control, IImageElement newImage, IImageElement previous = null)
{
if (newImage == null || rendererController.IsDisposed)
{
return;
}
if (Control.Drawable is FormsAnimationDrawable currentAnimation)
{
rendererController.SetFormsAnimationDrawable(currentAnimation);
currentAnimation.Stop();
}
else
{
rendererController.SetFormsAnimationDrawable(null);
}
try
{
await Control.UpdateBitmap(newImage, previous).ConfigureAwait(false);
}
catch (Exception ex)
{
Log.Warning(nameof(ImageElementManager), "Error loading image: {0}", ex);
}
finally
{
if (newImage is IImageController imageController)
imageController.SetIsLoading(false);
}
if (rendererController.IsDisposed)
return;
if (Control.Drawable is FormsAnimationDrawable updatedAnimation)
{
rendererController.SetFormsAnimationDrawable(updatedAnimation);
if (newImage.IsAnimationPlaying)
updatedAnimation.Start();
}
}
internal static void OnAnimationStopped(IElementController image, FormsAnimationDrawableStateEventArgs e)
{
if (image != null && e.Finished)
image.SetValueFromRenderer(Image.IsAnimationPlayingProperty, false);
}
static void UpdateAspect(IImageRendererController rendererController, ImageView Control, IImageElement newImage, IImageElement previous = null)
{
if (newImage == null || rendererController.IsDisposed)
{
return;
}
ImageView.ScaleType type = newImage.Aspect.ToScaleType();
Control.SetScaleType(type);
}
}
}