예제 #1
0
        internal static async Task <FormsAnimationDrawable> GetFormsAnimationDrawableFromStream(Stream stream, Context context, BitmapFactory.Options options)
        {
            FormsAnimationDrawable animation = null;

            using (var decoder = new AndroidGIFImageParser(context, options.InDensity, options.InTargetDensity))
            {
                try
                {
                    if (!FileImageSourceHandler.DecodeSynchronously)
                    {
                        await decoder.ParseAsync(stream).ConfigureAwait(false);
                    }
                    else
                    {
                        decoder.ParseAsync(stream).Wait();
                    }

                    animation = decoder.Animation;
                }
                catch (GIFDecoderFormatException)
                {
                    animation = null;
                }
            }

            return(animation);
        }
예제 #2
0
        public static async Task <IFormsAnimationDrawable> LoadImageAnimationAsync(UriImageSource imagesource, Context context, CancellationToken cancelationToken = default(CancellationToken))
        {
            Uri uri = imagesource?.Uri;
            FormsAnimationDrawable animation = null;

            if (uri != null)
            {
                var options = new BitmapFactory.Options
                {
                    InJustDecodeBounds = true
                };

                using (Stream stream = await imagesource.GetStreamAsync(cancelationToken).ConfigureAwait(false))
                {
                    using (var decoder = new AndroidGIFImageParser(context, options.InDensity, options.InTargetDensity))
                    {
                        try
                        {
                            if (!FileImageSourceHandler.DecodeSynchronously)
                            {
                                await decoder.ParseAsync(stream).ConfigureAwait(false);
                            }
                            else
                            {
                                decoder.ParseAsync(stream).Wait();
                            }

                            animation = decoder.Animation;
                        }
                        catch (GIFDecoderFormatException ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message);
                            animation = null;
                        }
                    }
                }

                if (animation == null)
                {
                    Log.Warning(nameof(FileImageSourceHandler), "Could not retrieve image or image data was invalid: {0}", imagesource);
                }
            }

            return(animation);
        }
예제 #3
0
        public async Task <IFormsAnimationDrawable> LoadImageAnimationAsync(StreamImageSource imagesource, Context context, CancellationToken cancelationToken = default(CancellationToken))
        {
            var streamSource = imagesource as StreamImageSource;
            FormsAnimationDrawable animation = null;

            if (streamSource?.Stream != null)
            {
                using (Stream stream = await((IStreamImageSource)streamSource).GetStreamAsync(cancelationToken).ConfigureAwait(false))
                {
                    int sourceDensity = 1;
                    int targetDensity = 1;

                    if (stream.CanSeek)
                    {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.InJustDecodeBounds = true;
                        await BitmapFactory.DecodeStreamAsync(stream, null, options);

                        sourceDensity = options.InDensity;
                        targetDensity = options.InTargetDensity;
                        stream.Seek(0, SeekOrigin.Begin);
                    }

                    using (var decoder = new AndroidGIFImageParser(context, sourceDensity, targetDensity))
                    {
                        try
                        {
                            await decoder.ParseAsync(stream).ConfigureAwait(false);

                            animation = decoder.Animation;
                        }
                        catch (GIFDecoderFormatException)
                        {
                            animation = null;
                        }
                    }
                }
            }

            if (animation == null)
            {
                Internals.Log.Warning(nameof(ImageLoaderSourceHandler), "Image data was invalid: {0}", streamSource);
            }

            return(animation);
        }