-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a SKLottieView to play Lottie animations (#118)
- Loading branch information
1 parent
fe4c7bc
commit 9e2015e
Showing
54 changed files
with
1,400 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
samples/Forms/SkiaSharpDemo/Converters/TimeSpanToDoubleConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Globalization; | ||
using Xamarin.Forms; | ||
|
||
namespace SkiaSharpDemo.Converters | ||
{ | ||
public class TimeSpanToDoubleConverter : IValueConverter | ||
{ | ||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => | ||
value switch | ||
{ | ||
TimeSpan ts => ts.TotalMilliseconds == 0 && parameter is not null | ||
? double.Parse(parameter.ToString()) | ||
: ts.TotalMilliseconds, | ||
_ => throw new ArgumentException("Value was not a TimeSpan.", nameof(value)), | ||
}; | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => | ||
value switch | ||
{ | ||
double d => TimeSpan.FromMilliseconds(d), | ||
_ => throw new ArgumentException("Value was not a double.", nameof(value)), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:controls="clr-namespace:SkiaSharp.Extended.UI.Controls;assembly=SkiaSharp.Extended.UI" | ||
xmlns:views="clr-namespace:SkiaSharpDemo.Views" | ||
x:Class="SkiaSharpDemo.Demos.LottiePage" | ||
Title="Lottie"> | ||
|
||
<Grid RowDefinitions="*,Auto"> | ||
|
||
<controls:SKLottieView x:Name="lottieView" | ||
Source="Lottie/trophy.json" | ||
Duration="{Binding Duration}" | ||
Progress="{Binding Progress}" | ||
IsRunning="{Binding IsBusy}"/> | ||
|
||
<BoxView Color="Green" Opacity="0.5" CornerRadius="12" | ||
WidthRequest="25" HeightRequest="24" Margin="24" | ||
HorizontalOptions="End" VerticalOptions="Start" | ||
IsVisible="{Binding IsComplete, Source={Reference lottieView}}" /> | ||
|
||
<StackLayout Spacing="12" Padding="12" Grid.Row="1"> | ||
|
||
<Slider Minimum="0" | ||
Maximum="{Binding Duration, Converter={StaticResource TimeSpanToDouble}, ConverterParameter=1}" | ||
Value="{Binding Progress, Converter={StaticResource TimeSpanToDouble}}" /> | ||
|
||
<Grid ColumnDefinitions="*,*" Margin="0,-12,0,0"> | ||
<Label Grid.Column="0" | ||
Text="{Binding Progress, StringFormat='{}{0:mm\\:ss\\.fff}'}" /> | ||
<Label Grid.Column="1" HorizontalOptions="End" | ||
Text="{Binding Duration, StringFormat='{}{0:mm\\:ss\\.fff}'}" /> | ||
</Grid> | ||
|
||
<StackLayout Orientation="Horizontal" HorizontalOptions="Center" Spacing="6"> | ||
<Button Text="Reset" Command="{Binding ResetCommand}" /> | ||
<Button Text="-100ms" Command="{Binding StepCommand}" CommandParameter="-100" /> | ||
<Button Text="Play/Pause" Command="{Binding PlayPauseCommand}" /> | ||
<Button Text="+100ms" Command="{Binding StepCommand}" CommandParameter="100" /> | ||
<Button Text="End" Command="{Binding EndCommand}" /> | ||
</StackLayout> | ||
|
||
</StackLayout> | ||
</Grid> | ||
|
||
</ContentPage> |
68 changes: 68 additions & 0 deletions
68
samples/Forms/SkiaSharpDemo/Demos/Lottie/LottiePage.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Windows.Input; | ||
using System.Collections.Generic; | ||
using SkiaSharp.Extended.UI.Controls; | ||
using Xamarin.Forms; | ||
|
||
namespace SkiaSharpDemo.Demos | ||
{ | ||
public partial class LottiePage : ContentPage | ||
{ | ||
private TimeSpan duration; | ||
private TimeSpan progress; | ||
|
||
public LottiePage() | ||
{ | ||
InitializeComponent(); | ||
|
||
ResetCommand = new Command(OnReset); | ||
StepCommand = new Command<string>(OnStep); | ||
EndCommand = new Command(OnEnd); | ||
PlayPauseCommand = new Command(OnPlayPause); | ||
|
||
IsBusy = true; | ||
|
||
BindingContext = this; | ||
} | ||
|
||
public TimeSpan Duration | ||
{ | ||
get => duration; | ||
set | ||
{ | ||
duration = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
public TimeSpan Progress | ||
{ | ||
get => progress; | ||
set | ||
{ | ||
progress = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
public ICommand ResetCommand { get; } | ||
|
||
public ICommand StepCommand { get; } | ||
|
||
public ICommand PlayPauseCommand { get; } | ||
|
||
public ICommand EndCommand { get; } | ||
|
||
private void OnReset() => | ||
Progress = TimeSpan.Zero; | ||
|
||
private void OnStep(string step) => | ||
Progress += TimeSpan.FromMilliseconds(int.Parse(step)); | ||
|
||
private void OnEnd() => | ||
Progress = Duration; | ||
|
||
private void OnPlayPause() => | ||
IsBusy = !IsBusy; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
samples/Maui/SkiaSharpDemo/Converters/TimeSpanToDoubleConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Globalization; | ||
|
||
namespace SkiaSharpDemo.Converters; | ||
|
||
public class TimeSpanToDoubleConverter : IValueConverter | ||
{ | ||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => | ||
value switch | ||
{ | ||
TimeSpan ts => ts.TotalMilliseconds == 0 && parameter is not null | ||
? double.Parse(parameter.ToString()) | ||
: ts.TotalMilliseconds, | ||
_ => throw new ArgumentException("Value was not a TimeSpan.", nameof(value)), | ||
}; | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => | ||
value switch | ||
{ | ||
double d => TimeSpan.FromMilliseconds(d), | ||
_ => throw new ArgumentException("Value was not a double.", nameof(value)), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:controls="clr-namespace:SkiaSharp.Extended.UI.Controls;assembly=SkiaSharp.Extended.UI" | ||
xmlns:views="clr-namespace:SkiaSharpDemo.Views" | ||
x:Class="SkiaSharpDemo.Demos.LottiePage" | ||
Title="Lottie"> | ||
|
||
<Grid RowDefinitions="*,Auto"> | ||
|
||
<controls:SKLottieView x:Name="lottieView" | ||
Source="Lottie/trophy.json" | ||
Duration="{Binding Duration}" | ||
Progress="{Binding Progress}" | ||
IsRunning="{Binding IsBusy}"/> | ||
|
||
<BoxView Color="Green" Opacity="0.5" CornerRadius="12" | ||
WidthRequest="25" HeightRequest="24" Margin="24" | ||
HorizontalOptions="End" VerticalOptions="Start" | ||
IsVisible="{Binding IsComplete, Source={Reference lottieView}}" /> | ||
|
||
<VerticalStackLayout Spacing="12" Padding="12" Grid.Row="1"> | ||
|
||
<Slider Minimum="0" | ||
Maximum="{Binding Duration, Converter={StaticResource TimeSpanToDouble}, ConverterParameter=1}" | ||
Value="{Binding Progress, Converter={StaticResource TimeSpanToDouble}}" /> | ||
|
||
<Grid ColumnDefinitions="*,*" Margin="0,-12,0,0"> | ||
<Label Grid.Column="0" | ||
Text="{Binding Progress, StringFormat='{}{0:mm\\:ss\\.fff}'}" /> | ||
<Label Grid.Column="1" HorizontalOptions="End" | ||
Text="{Binding Duration, StringFormat='{}{0:mm\\:ss\\.fff}'}" /> | ||
</Grid> | ||
|
||
<HorizontalStackLayout HorizontalOptions="Center" Spacing="6"> | ||
<Button Text="Reset" Command="{Binding ResetCommand}" /> | ||
<Button Text="-100ms" Command="{Binding StepCommand}" CommandParameter="-100" /> | ||
<Button Text="Play/Pause" Command="{Binding PlayPauseCommand}" /> | ||
<Button Text="+100ms" Command="{Binding StepCommand}" CommandParameter="100" /> | ||
<Button Text="End" Command="{Binding EndCommand}" /> | ||
</HorizontalStackLayout> | ||
|
||
</VerticalStackLayout> | ||
</Grid> | ||
|
||
</ContentPage> |
Oops, something went wrong.