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); }
public AndroidGIFImageParser(Context context, int sourceDensity, int targetDensity) { _context = context; _sourceDensity = sourceDensity; _targetDensity = targetDensity; Animation = new FormsAnimationDrawable(); }
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); }
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); }
public static async Task <IFormsAnimationDrawable> LoadImageAnimationAsync(UriImageSource imagesource, Context context, CancellationToken cancelationToken = default(CancellationToken)) { FormsAnimationDrawable animation = null; if (imagesource is IStreamImageSource streamImageSource) { using var stream = await streamImageSource.GetStreamAsync(cancelationToken).ConfigureAwait(false); if (stream != null) { var options = new BitmapFactory.Options { InJustDecodeBounds = true }; 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) { Application.Current?.FindMauiContext()?.CreateLogger <FileImageSourceHandler>()?.LogWarning("Could not retrieve image or image data was invalid: {imagesource}", imagesource); } } } return(animation); }
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) { Application.Current?.FindMauiContext()?.CreateLogger <ImageLoaderSourceHandler>()?.LogWarning("Image data was invalid: {streamSource}", streamSource); } return(animation); }
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 = context.GetDrawableId(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) { Application.Current?.FindMauiContext()?.CreateLogger <FileImageSourceHandler>()?.LogWarning("Could not retrieve image or image data was invalid: {imagesource}", imagesource); } return(animation); }
public Task <IFormsAnimationDrawable> LoadImageAnimationAsync(ImageSource imagesource, Context context, CancellationToken cancelationToken = default, float scale = 1) { return(FormsAnimationDrawable.LoadImageAnimationAsync(imagesource, context, cancelationToken)); }