From 7d141490379facf13f2dfa587036816e200a4518 Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Tue, 16 Apr 2024 23:06:10 +0200 Subject: [PATCH] Use the new Skottie animation builder (#277) The new builder supports loading embedded images as well as caching any images or files if they are re-used. --- samples/SkiaSharpDemo/Demos/Lottie/LottiePage.xaml | 2 +- samples/SkiaSharpDemo/Resources/Raw/Lottie/dotnetbot.json | 1 + .../Controls/Lottie/SKFileLottieImageSource.shared.cs | 2 +- .../Controls/Lottie/SKLottieImageSource.shared.cs | 8 +++++++- .../Controls/Lottie/SKStreamLottieImageSource.shared.cs | 2 +- .../Controls/Lottie/SKUriLottieImageSource.shared.cs | 2 +- 6 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 samples/SkiaSharpDemo/Resources/Raw/Lottie/dotnetbot.json diff --git a/samples/SkiaSharpDemo/Demos/Lottie/LottiePage.xaml b/samples/SkiaSharpDemo/Demos/Lottie/LottiePage.xaml index 76bc247d..05905b12 100644 --- a/samples/SkiaSharpDemo/Demos/Lottie/LottiePage.xaml +++ b/samples/SkiaSharpDemo/Demos/Lottie/LottiePage.xaml @@ -8,7 +8,7 @@ LoadAnimationAsync(CancellationTok if (stream is null) throw new FileLoadException($"Unable to load Lottie animation file \"{File}\"."); - var animation = Skottie.Animation.Create(stream); + var animation = CreateAnimationBuilder().Build(stream); if (animation is null) throw new FileLoadException($"Unable to parse Lottie animation \"{File}\"."); diff --git a/source/SkiaSharp.Extended.UI.Maui/Controls/Lottie/SKLottieImageSource.shared.cs b/source/SkiaSharp.Extended.UI.Maui/Controls/Lottie/SKLottieImageSource.shared.cs index ae8ec4d2..8a62e368 100644 --- a/source/SkiaSharp.Extended.UI.Maui/Controls/Lottie/SKLottieImageSource.shared.cs +++ b/source/SkiaSharp.Extended.UI.Maui/Controls/Lottie/SKLottieImageSource.shared.cs @@ -1,4 +1,5 @@ - +using SkiaSharp.Resources; + namespace SkiaSharp.Extended.UI.Controls; [TypeConverter(typeof(Converters.SKLottieImageSourceConverter))] @@ -10,6 +11,11 @@ public abstract class SKLottieImageSource : Element public abstract Task LoadAnimationAsync(CancellationToken cancellationToken = default); + internal Skottie.AnimationBuilder CreateAnimationBuilder() => + Skottie.Animation.CreateBuilder() + .SetResourceProvider(new CachingResourceProvider(new DataUriResourceProvider())) + .SetFontManager(SKFontManager.Default); + public static object FromUri(Uri uri) => new SKUriLottieImageSource { Uri = uri }; diff --git a/source/SkiaSharp.Extended.UI.Maui/Controls/Lottie/SKStreamLottieImageSource.shared.cs b/source/SkiaSharp.Extended.UI.Maui/Controls/Lottie/SKStreamLottieImageSource.shared.cs index e7068b97..0332b563 100644 --- a/source/SkiaSharp.Extended.UI.Maui/Controls/Lottie/SKStreamLottieImageSource.shared.cs +++ b/source/SkiaSharp.Extended.UI.Maui/Controls/Lottie/SKStreamLottieImageSource.shared.cs @@ -23,7 +23,7 @@ public override async Task LoadAnimationAsync(CancellationTok if (stream is null) throw new FileLoadException($"Unable to load Lottie animation stream."); - var animation = Skottie.Animation.Create(stream); + var animation = CreateAnimationBuilder().Build(stream); if (animation is null) throw new FileLoadException($"Unable to parse Lottie animation."); diff --git a/source/SkiaSharp.Extended.UI.Maui/Controls/Lottie/SKUriLottieImageSource.shared.cs b/source/SkiaSharp.Extended.UI.Maui/Controls/Lottie/SKUriLottieImageSource.shared.cs index 7ec62bea..efd84adc 100644 --- a/source/SkiaSharp.Extended.UI.Maui/Controls/Lottie/SKUriLottieImageSource.shared.cs +++ b/source/SkiaSharp.Extended.UI.Maui/Controls/Lottie/SKUriLottieImageSource.shared.cs @@ -30,7 +30,7 @@ public override async Task LoadAnimationAsync(CancellationTok if (stream is null) throw new FileLoadException($"Unable to load Lottie animation uri \"{Uri}\"."); - var animation = Skottie.Animation.Create(stream); + var animation = CreateAnimationBuilder().Build(stream); if (animation is null) throw new FileLoadException($"Unable to parse Lottie animation \"{Uri}\".");