コード例 #1
0
ファイル: ImageViewExtensions.cs プロジェクト: sung-su/maui
        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?.PlatformSizeChanged();
                }
            }


            bool SourceIsNotChanged(IImageElement imageElement, ImageSource imageSource)
            {
                return((imageElement != null) ? imageElement.Source == imageSource : true);
            }
        }