예제 #1
0
 public AndroidGIFImageParser(Context context, int sourceDensity, int targetDensity)
 {
     _context       = context;
     _sourceDensity = sourceDensity;
     _targetDensity = targetDensity;
     Animation      = new FormsAnimationDrawable();
 }
예제 #2
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);
        }
예제 #3
0
        internal static async Task <FormsAnimationDrawable> GetFormsAnimationDrawableFromFile(string file, Context context, BitmapFactory.Options options)
        {
            FormsAnimationDrawable animation = null;

            using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, true))
                animation = await GetFormsAnimationDrawableFromStream(stream, context, options);

            return(animation);
        }
예제 #4
0
        internal static async Task <FormsAnimationDrawable> GetFormsAnimationDrawableFromResource(int resourceId, Context context, BitmapFactory.Options options)
        {
            FormsAnimationDrawable animation = null;

            using (var stream = context.Resources.OpenRawResource(resourceId))
                animation = await GetFormsAnimationDrawableFromStream(stream, context, options);

            return(animation);
        }
예제 #5
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);
        }
예제 #6
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);
        }
예제 #7
0
        public static async Task <IFormsAnimationDrawable> LoadImageAnimationAsync(FileImageSource imagesource, Context context, CancellationToken cancelationToken = default(CancellationToken))
        {
            string file = ((FileImageSource)imagesource).File;
            FormsAnimationDrawable animation = null;

            BitmapFactory.Options options = new BitmapFactory.Options
            {
                InJustDecodeBounds = true
            };

            int drawableIdentifier = ResourceManager.GetDrawableByName(file);

            if (drawableIdentifier != 0)
            {
                if (!FileImageSourceHandler.DecodeSynchronously)
                {
                    await BitmapFactory.DecodeResourceAsync(context.Resources, drawableIdentifier, options);
                }
                else
                {
                    BitmapFactory.DecodeResource(context.Resources, drawableIdentifier, options);
                }

                animation = await GetFormsAnimationDrawableFromResource(drawableIdentifier, context, options);
            }
            else
            {
                animation = await GetFormsAnimationDrawableFromFile(file, context, options);
            }

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

            return(animation);
        }
예제 #8
0
 public Task <IFormsAnimationDrawable> LoadImageAnimationAsync(ImageSource imagesource, Context context, CancellationToken cancelationToken = default, float scale = 1)
 {
     return(FormsAnimationDrawable.LoadImageAnimationAsync(imagesource, context, cancelationToken));
 }