From 93612514d9b9adc18091e0af58bd611f43a9a835 Mon Sep 17 00:00:00 2001 From: Daniel Luberda Date: Fri, 26 Apr 2019 19:06:08 +0200 Subject: [PATCH] XF: Placeholders don't always show or show with incorrect aspect ratio Fixes #353 #1239 --- .../CachedImageFastRenderer.cs | 12 +++++++----- .../CachedImageRenderer.cs | 18 ++++++++++-------- .../CachedImageRenderer.cs | 10 ++++++---- .../CachedImageRenderer.cs | 14 ++++++++------ 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/source/FFImageLoading.Forms.Droid/CachedImageFastRenderer.cs b/source/FFImageLoading.Forms.Droid/CachedImageFastRenderer.cs index d5090eab1..47a40f322 100644 --- a/source/FFImageLoading.Forms.Droid/CachedImageFastRenderer.cs +++ b/source/FFImageLoading.Forms.Droid/CachedImageFastRenderer.cs @@ -32,7 +32,7 @@ public class CachedImageFastRenderer : CachedImageView, IVisualElementRenderer private static readonly MethodInfo _viewExtensionsMethod = typeof(ImageRenderer).Assembly.GetType("Xamarin.Forms.Platform.Android.ViewExtensions")?.GetRuntimeMethod("EnsureId", new[] { typeof(Android.Views.View) }); private static readonly MethodInfo _elementRendererTypeOnTouchEvent = ElementRendererType?.GetRuntimeMethod("OnTouchEvent", new[] { typeof(MotionEvent) }); private bool _isDisposed; - private int? _defaultLabelFor; + private int? _defaultLabelFor; private VisualElementTracker _visualElementTracker; private IDisposable _visualElementRenderer; private IScheduledWork _currentTask; @@ -110,7 +110,7 @@ private void OnElementChanged(ElementChangedEventArgs e) if (e.NewElement != null) { - e.NewElement.InternalReloadImage = new Action(ReloadImage); + e.NewElement.InternalReloadImage = new Action(ReloadImage); e.NewElement.InternalCancel = new Action(CancelIfNeeded); e.NewElement.InternalGetImageAsJPG = new Func>(GetImageAsJpgAsync); e.NewElement.InternalGetImageAsPNG = new Func>(GetImageAsPngAsync); @@ -316,16 +316,18 @@ private void UpdateBitmap(CachedImageView imageView, CachedImage image, CachedIm private async void ImageLoadingSizeChanged(CachedImage element, bool isLoading) { - if (element == null || _isDisposed || isLoading) + if (element == null || _isDisposed) return; - + await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => { if (element == null || _isDisposed) return; ((IVisualElementController)element).NativeSizeChanged(); - element.SetIsLoading(isLoading); + + if (!isLoading) + element.SetIsLoading(isLoading); }); } diff --git a/source/FFImageLoading.Forms.Droid/CachedImageRenderer.cs b/source/FFImageLoading.Forms.Droid/CachedImageRenderer.cs index cc0e93321..6ecf5ef99 100644 --- a/source/FFImageLoading.Forms.Droid/CachedImageRenderer.cs +++ b/source/FFImageLoading.Forms.Droid/CachedImageRenderer.cs @@ -68,7 +68,7 @@ private static void RegisterRenderer(Type type, Type renderer) } private bool _isDisposed; - private IScheduledWork _currentTask; + private IScheduledWork _currentTask; private ImageSourceBinding _lastImageSource; private readonly MotionEventHelper _motionEventHelper = CachedImage.FixedAndroidMotionEventHandler ? new MotionEventHelper() : null; private readonly static Type _platformDefaultRendererType = typeof(ImageRenderer).Assembly.GetType("Xamarin.Forms.Platform.Android.Platform+DefaultRenderer"); @@ -131,7 +131,7 @@ protected override void OnElementChanged(ElementChangedEventArgs e) if (e.NewElement != null) { - e.NewElement.InternalReloadImage = new Action(ReloadImage); + e.NewElement.InternalReloadImage = new Action(ReloadImage); e.NewElement.InternalCancel = new Action(CancelIfNeeded); e.NewElement.InternalGetImageAsJPG = new Func>(GetImageAsJpgAsync); e.NewElement.InternalGetImageAsPNG = new Func>(GetImageAsPngAsync); @@ -234,9 +234,9 @@ private void UpdateBitmap(CachedImageView imageView, CachedImage image, CachedIm } } - private async void ImageLoadingSizeChanged(CachedImage element, bool isLoading) - { - if (element == null || _isDisposed || isLoading) + private async void ImageLoadingSizeChanged(CachedImage element, bool isLoading) + { + if (element == null || _isDisposed) return; await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => @@ -245,11 +245,13 @@ await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => return; ((IVisualElementController)element).NativeSizeChanged(); - element.SetIsLoading(isLoading); + + if (!isLoading) + element.SetIsLoading(isLoading); }); - } + } - private void ReloadImage() + private void ReloadImage() { UpdateBitmap(Control, Element, null); } diff --git a/source/FFImageLoading.Forms.Touch/CachedImageRenderer.cs b/source/FFImageLoading.Forms.Touch/CachedImageRenderer.cs index d1baf15ef..d1d5059c0 100644 --- a/source/FFImageLoading.Forms.Touch/CachedImageRenderer.cs +++ b/source/FFImageLoading.Forms.Touch/CachedImageRenderer.cs @@ -40,7 +40,7 @@ internal class _CachedImageRenderer } private bool _isDisposed; - private IScheduledWork _currentTask; + private IScheduledWork _currentTask; private ImageSourceBinding _lastImageSource; private readonly object _updateBitmapLock = new object(); @@ -99,7 +99,7 @@ protected override void OnElementChanged(ElementChangedEventArgs e) if (e.NewElement != null) { - e.NewElement.InternalReloadImage = new Action(ReloadImage); + e.NewElement.InternalReloadImage = new Action(ReloadImage); e.NewElement.InternalCancel = new Action(CancelIfNeeded); e.NewElement.InternalGetImageAsJPG = new Func>(GetImageAsJpgAsync); e.NewElement.InternalGetImageAsPNG = new Func>(GetImageAsPngAsync); @@ -221,7 +221,7 @@ private void UpdateImage(PImageView imageView, CachedImage image, CachedImage pr private async void ImageLoadingSizeChanged(CachedImage element, bool isLoading) { - if (element == null || _isDisposed || isLoading) + if (element == null || _isDisposed) return; await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => @@ -230,7 +230,9 @@ await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => return; ((IVisualElementController)element).NativeSizeChanged(); - element.SetIsLoading(isLoading); + + if (!isLoading) + element.SetIsLoading(isLoading); }); } diff --git a/source/FFImageLoading.Forms.WinUWP/CachedImageRenderer.cs b/source/FFImageLoading.Forms.WinUWP/CachedImageRenderer.cs index 22309ec90..35cc89f21 100644 --- a/source/FFImageLoading.Forms.WinUWP/CachedImageRenderer.cs +++ b/source/FFImageLoading.Forms.WinUWP/CachedImageRenderer.cs @@ -225,20 +225,22 @@ static Stretch GetStretch(Aspect aspect) } } - private async void ImageLoadingSizeChanged(CachedImage element, bool isLoading) - { - if (element == null || _isDisposed || isLoading) + private async void ImageLoadingSizeChanged(CachedImage element, bool isLoading) + { + if (element == null || _isDisposed) return; - + await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => { if (element == null || _isDisposed) return; ((IVisualElementController)element).InvalidateMeasure(Xamarin.Forms.Internals.InvalidationTrigger.RendererReady); - element.SetIsLoading(isLoading); + + if (!isLoading) + element.SetIsLoading(isLoading); }); - } + } void ReloadImage() {