Skip to content

Instantly share code, notes, and snippets.

@rotorgames
Created September 15, 2017 15:05
Show Gist options
  • Select an option

  • Save rotorgames/dc9f86b02c70f9edd645bc22864f7c8c to your computer and use it in GitHub Desktop.

Select an option

Save rotorgames/dc9f86b02c70f9edd645bc22864f7c8c to your computer and use it in GitHub Desktop.
using Lottie.Forms;
using Xamarin.Forms;
namespace Shping.Controls.Lottie
{
public class LottieAnimationView : AnimationView
{
public static readonly BindableProperty AssetFolderProperty = BindableProperty.Create(nameof(AssetFolder), typeof(string), typeof(LottieAnimationView));
public string AssetFolder
{
get { return (string)GetValue(AssetFolderProperty); }
set { SetValue(AssetFolderProperty, value); }
}
public static readonly BindableProperty SourceHeightProperty = BindableProperty.Create(nameof(SourceHeight), typeof(double), typeof(LottieAnimationView), 0d);
public double SourceHeight
{
get { return (double)GetValue(SourceHeightProperty); }
set { SetValue(SourceHeightProperty, value); }
}
public static readonly BindableProperty SourceWidthProperty = BindableProperty.Create(nameof(SourceWidth), typeof(double), typeof(LottieAnimationView), 0d);
public double SourceWidth
{
get { return (double)GetValue(SourceWidthProperty); }
set { SetValue(SourceWidthProperty, value); }
}
protected override void OnPropertyChanged(string propertyName = null)
{
base.OnPropertyChanged(propertyName);
switch (propertyName)
{
case nameof(SourceWidth):
case nameof(SourceHeight):
InvalidateMeasure();
break;
}
}
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
if (SourceWidth > 0 && SourceHeight > 0)
{
double widht, height;
var aspect = SourceWidth/SourceHeight;
//var height = widthConstraint/aspect;
if (widthConstraint > heightConstraint)
{
height = heightConstraint;
widht = heightConstraint*aspect;
}
else
{
height = widthConstraint / aspect;
widht = widthConstraint;
}
var size = new Size(widht, height);
return new SizeRequest(size, size);
}
return base.OnMeasure(widthConstraint, heightConstraint);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment