Ejemplo n.º 1
0
 public static Task UpdateBitmap(this AImageView imageView, IImageElement newView, IImageElement previousView) =>
 imageView.UpdateBitmap(newView, previousView, null, null);
Ejemplo n.º 2
0
 public void DrawImage(IImageElement element, RectangleF rect)
 {
     this.surface.DrawImage(element, rect);
 }
Ejemplo n.º 3
0
        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 (Control.Drawable is FormsAnimationDrawable updatedAnimation)
            {
                rendererController.SetFormsAnimationDrawable(updatedAnimation);

                if (newImage.IsAnimationPlaying)
                {
                    updatedAnimation.Start();
                }
            }
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(IImageElement obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
        async static Task TryUpdateBitmap(IImageRendererController rendererController, ImageView Control, IImageElement newImage, IImageElement previous = null)
        {
            if (newImage == null || rendererController.IsDisposed)
            {
                return;
            }

            await Control.UpdateBitmap(newImage, previous).ConfigureAwait(false);
        }
Ejemplo n.º 7
0
        static async Task UpdateBitmap(
            this AImageView imageView,
            IImageElement newView,
            IImageElement previousView,
            ImageSource newImageSource,
            ImageSource previousImageSource)
        {
            IImageController imageController = newView as IImageController;

            newImageSource      = newImageSource ?? newView?.Source;
            previousImageSource = previousImageSource ?? previousView?.Source;

            if (imageView.IsDisposed())
            {
                return;
            }

            if (newImageSource != null && Equals(previousImageSource, newImageSource))
            {
                return;
            }

            imageController?.SetIsLoading(true);

            (imageView as IImageRendererController)?.SkipInvalidate();
            imageView.Reset();
            imageView.SetImageResource(global::Android.Resource.Color.Transparent);

            try
            {
                if (newImageSource != null)
                {
                    // all this animation code will go away if/once we pull in GlideX
                    IFormsAnimationDrawable animation = null;

                    if (imageController != null && imageController.GetLoadAsAnimation())
                    {
                        var animationHandler = Registrar.Registered.GetHandlerForObject <IAnimationSourceHandler>(newImageSource);
                        if (animationHandler != null)
                        {
                            animation = await animationHandler.LoadImageAnimationAsync(newImageSource, imageView.Context);
                        }
                    }

                    if (animation == null)
                    {
                        var imageViewHandler = Registrar.Registered.GetHandlerForObject <IImageViewHandler>(newImageSource);
                        if (imageViewHandler != null)
                        {
                            await imageViewHandler.LoadImageAsync(newImageSource, imageView);
                        }
                        else
                        {
                            using (var drawable = await imageView.Context.GetFormsDrawableAsync(newImageSource))
                            {
                                // only set the image if we are still on the same one
                                if (!imageView.IsDisposed() && SourceIsNotChanged(newView, newImageSource))
                                {
                                    imageView.SetImageDrawable(drawable);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (!imageView.IsDisposed() && SourceIsNotChanged(newView, newImageSource))
                        {
                            imageView.SetImageDrawable(animation.ImageDrawable);
                        }
                        else
                        {
                            animation?.Reset();
                            animation?.Dispose();
                        }
                    }
                }
                else
                {
                    imageView.SetImageBitmap(null);
                }
            }
            finally
            {
                // only mark as finished if we are still working on the same image
                if (SourceIsNotChanged(newView, newImageSource))
                {
                    imageController?.SetIsLoading(false);
                    imageController?.NativeSizeChanged();
                }
            }


            bool SourceIsNotChanged(IImageElement imageElement, ImageSource imageSource)
            {
                return((imageElement != null) ? imageElement.Source == imageSource : true);
            }
        }
Ejemplo n.º 8
0
        public static async Task SetImage(IImageVisualElementRenderer renderer, IImageElement imageElement, Image oldElement = null)
        {
            _ = renderer ?? throw new ArgumentNullException(nameof(renderer), $"{nameof(ImageElementManager)}.{nameof(SetImage)} {nameof(renderer)} cannot be null");
            _ = imageElement ?? throw new ArgumentNullException(nameof(imageElement), $"{nameof(ImageElementManager)}.{nameof(SetImage)} {nameof(imageElement)} cannot be null");

            var Element = renderer.Element;
            var Control = renderer.GetImage();

            if (renderer.IsDisposed || Element == null || Control == null)
            {
                return;
            }

            var imageController = imageElement as IImageController;

            var source = imageElement.Source;

#if __MOBILE__
            if (Control.Image?.Images != null && Control.Image.Images.Length > 1)
            {
                renderer.SetImage(null);
            }
            else
#endif
            if (oldElement != null)
            {
                var oldSource = oldElement.Source;
                if (Equals(oldSource, source))
                {
                    return;
                }

                if (oldSource is FileImageSource oldFile && source is FileImageSource newFile && oldFile == newFile)
                {
                    return;
                }

                renderer.SetImage(null);
            }

            imageController?.SetIsLoading(true);
            try
            {
                var uiimage = await source.GetNativeImageAsync();

                if (renderer.IsDisposed)
                {
                    return;
                }

                // only set if we are still on the same image
                if (Control != null && imageElement.Source == source)
                {
                    renderer.SetImage(uiimage);
                }
                else
                {
                    uiimage?.Dispose();
                }
            }
            finally
            {
                // only mark as finished if we are still on the same image
                if (imageElement.Source == source)
                {
                    imageController?.SetIsLoading(false);
                }
            }

            (imageElement as IViewController)?.NativeSizeChanged();
        }
Ejemplo n.º 9
0
        public static async Task SetImage(IImageVisualElementRenderer renderer, IImageElement imageElement, Image oldElement = null)
        {
            _ = renderer ?? throw new ArgumentNullException(nameof(renderer), $"{nameof(ImageElementManager)}.{nameof(SetImage)} {nameof(renderer)} cannot be null");
            _ = imageElement ?? throw new ArgumentNullException(nameof(imageElement), $"{nameof(ImageElementManager)}.{nameof(SetImage)} {nameof(imageElement)} cannot be null");

            var Element = renderer.Element;
            var Control = renderer.GetImage();

            if (renderer.IsDisposed || Element == null || Control == null)
            {
                return;
            }

            var imageController = imageElement as IImageController;

            var source = imageElement.Source;

#if __MOBILE__
            if (Control.Image?.Images != null && Control.Image.Images.Length > 1)
            {
                renderer.SetImage(null);
            }
#else
            if (Control.Image != null && Control.Image.Representations().Length > 1)
            {
                renderer.SetImage(null);
            }
#endif
            else if (oldElement != null)
            {
                var oldSource = oldElement.Source;
                if (Equals(oldSource, source))
                {
                    return;
                }

                if (oldSource is FileImageSource oldFile && source is FileImageSource newFile && oldFile == newFile)
                {
                    return;
                }

#if __MOBILE__
                if (Control is FormsUIImageView imageView)
                {
                    if (imageView.Animation != null)
                    {
                        Control.StopAnimating();
                        imageView.AnimationStopped -= OnAnimationStopped;
                        imageView.Animation.Dispose();
                    }

                    renderer.SetImage(null);
                    imageView.Animation = null;
                }
#endif
            }

            imageController?.SetIsLoading(true);

            try
            {
#if __MOBILE__
                bool useAnimation = imageController.GetLoadAsAnimation();
                IAnimationSourceHandler handler = null;
                if (useAnimation && source != null)
                {
                    handler = Controls.Internals.Registrar.Registered.GetHandlerForObject <IAnimationSourceHandler>(source);
                }

                if (handler != null)
                {
                    FormsCAKeyFrameAnimation animation = await handler.LoadImageAnimationAsync(source, scale : (float)UIScreen.MainScreen.Scale).ConfigureAwait(false);

                    if (animation != null && Control is FormsUIImageView imageView && imageElement.Source == source)
                    {
                        if (imageView.Animation != null)
                        {
                            imageView.AnimationStopped -= OnAnimationStopped;
                        }

                        imageView.Animation         = animation;
                        imageView.AnimationStopped += OnAnimationStopped;

                        if ((bool)Element.GetValue(Image.IsAnimationPlayingProperty))
                        {
                            imageView.StartAnimating();
                        }
                    }
                    else
                    {
                        animation?.Dispose();
                    }
                }
                else
#endif
                {
                    var uiimage = await source.GetNativeImageAsync();

                    if (renderer.IsDisposed)
                    {
                        return;
                    }

                    // only set if we are still on the same image
                    if (Control != null && imageElement.Source == source)
                    {
                        renderer.SetImage(uiimage);
                    }
                    else
                    {
                        uiimage?.Dispose();
                    }
                }
            }
Ejemplo n.º 10
0
        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)
            {
                Application.Current?.FindMauiContext()?.CreateLogger <IImageRendererController>()?.LogWarning(ex, "Error loading image");
            }
            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();
                }
            }
        }
Ejemplo n.º 11
0
        // TODO hartez 2017/04/07 09:33:03 Review this again, not sure it's handling the transition from previousImage to 'null' newImage correctly
        static async Task UpdateBitmap(
            this AImageView imageView,
            IImageElement newView,
            IImageElement previousView,
            ImageSource newImageSource,
            ImageSource previousImageSource)
        {
            IImageController imageController = newView as IImageController;

            newImageSource      = newView?.Source;
            previousImageSource = previousView?.Source;

            if (newImageSource == null || imageView.IsDisposed())
            {
                return;
            }

            if (Equals(previousImageSource, newImageSource))
            {
                return;
            }

            imageController?.SetIsLoading(true);

            (imageView as IImageRendererController)?.SkipInvalidate();
            imageView.SetImageResource(global::Android.Resource.Color.Transparent);

            bool   setByImageViewHandler = false;
            Bitmap bitmap = null;

            try
            {
                if (newImageSource != null)
                {
                    var imageViewHandler = Internals.Registrar.Registered.GetHandlerForObject <IImageViewHandler>(newImageSource);
                    if (imageViewHandler != null)
                    {
                        await imageViewHandler.LoadImageAsync(newImageSource, imageView);

                        setByImageViewHandler = true;
                    }
                    else
                    {
                        var imageSourceHandler = Internals.Registrar.Registered.GetHandlerForObject <IImageSourceHandler>(newImageSource);
                        bitmap = await imageSourceHandler.LoadImageAsync(newImageSource, imageView.Context);
                    }
                }
            }
            catch (TaskCanceledException)
            {
                imageController?.SetIsLoading(false);
            }

            // Check if the source on the new image has changed since the image was loaded
            if (!Equals(newView?.Source, newImageSource))
            {
                bitmap?.Dispose();
                return;
            }

            if (!setByImageViewHandler && !imageView.IsDisposed())
            {
                if (bitmap == null && newImageSource is FileImageSource)
                {
                    imageView.SetImageResource(ResourceManager.GetDrawableByName(((FileImageSource)newImageSource).File));
                }
                else
                {
                    imageView.SetImageBitmap(bitmap);
                }
            }

            bitmap?.Dispose();
            imageController?.SetIsLoading(false);
        }