Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add Folder image in Xamarin Forms (PCL) #48

Closed
golo98 opened this issue Mar 27, 2017 · 8 comments
Closed

How to add Folder image in Xamarin Forms (PCL) #48

golo98 opened this issue Mar 27, 2017 · 8 comments

Comments

@golo98
Copy link

golo98 commented Mar 27, 2017

Hi, in a Xamarin Forms App (PCL), how to add the images folder in android and ios projects to lottie animation?

Thanks in advanced.

@golo98 golo98 changed the title How to add Folder image in Xamarin Forms How to add Folder image in Xamarin Forms (PCL) Mar 30, 2017
@paradisehuman
Copy link

Hi. I have same problem. Please Help.

@golo98
Copy link
Author

golo98 commented Apr 23, 2017

Hi @paradisehuman, I solved temporarily as follows:

  • Install the Lottie platform nugget package on Android and iOS projects.
  • In the PCL project, from this repository (Lottie.Forms) copy the file AnimationView.cs and aggregate this code:
public static readonly BindableProperty IsFolderImagesAddedProperty = BindableProperty.Create(nameof(IsFolderImagesAdded),
            typeof(bool), typeof(AnimationView), default(bool));

public bool IsFolderImagesAdded
        {
            get { return (bool)GetValue(IsFolderImagesAddedProperty); }

            set { SetValue(IsFolderImagesAddedProperty, value); }
        }
  • In Android and iOS projects add the correspondants files AnimationViewRenderer.cs, from Lotttie.Forms.Droid and Lottie.Forms.iOS.
  • On Android add this code to the AnimationViewRenderer.cs file
    OnElementChanged method
            if (e.NewElement != null)
            {
                e.NewElement.OnPlay += OnPlay;
                e.NewElement.OnPause += OnPause;
                _animationView.Loop(e.NewElement.Loop);
                if (!string.IsNullOrEmpty(e.NewElement.Animation))
                {
                    _animationView.SetAnimation(e.NewElement.Animation);
                    Element.Duration = TimeSpan.FromMilliseconds(_animationView.Duration);
                }
                if (e.NewElement.IsFolderImagesAdded)
                {
                    _animationView.SetImageAssetsFolder("images");
                }
                if (e.NewElement.AutoPlay)
                {
                    _animationView.PlayAnimation();
                }
            }

OnElementPropertyChanged method

            if (e.PropertyName == AnimationView.AnimationProperty.PropertyName)
            {
                _animationView.SetAnimation(Element.Animation);
                Element.Duration = TimeSpan.FromMilliseconds(_animationView.Duration);
                if (Element.IsFolderImagesAdded)
                {
                    _animationView.SetImageAssetsFolder("images");
                }
                if (Element.AutoPlay)
                {
                    _animationView.PlayAnimation();
                }
            }

  • On iOS, I only add the images directly on the root of the project and is working.
  • On the XAML file
    <forms:AnimationView x:Name="AnimationView" Animation="animation.json" Loop="True" AutoPlay="True" IsFolderImagesAdded="True" VerticalOptions="Center" HorizontalOptions="Start" HeightRequest="150" WidthRequest="150"/>

I hope this can help you.

@paradisehuman
Copy link

@golo98 Thank you for your help. But unfortunately a gray circle is showed me instead of the images. Where must be image folder in Android project ? In the root ? and what is the Images build action?

@golo98
Copy link
Author

golo98 commented Apr 23, 2017

@paradisehuman, on android project, the lottie animation file and the images subfolder resides in the Assets folder, on the iOS project the lottie animation and the respectives images I put in the root project folder.

@paradisehuman
Copy link

paradisehuman commented Apr 23, 2017

@golo98 I put Images in Assets folder and images build action is AndroidAssets and nothing has changed !

@golo98
Copy link
Author

golo98 commented Apr 23, 2017

@paradisehuman

image

myanimation.json, is the lottie animation and the "images" subfolder in Assets contains the respectives images.

<forms:AnimationView 
    x:Name="AnimationView"
    Animation="myanimation.json"
    Loop="True"
    AutoPlay="True"
    IsFolderImagesAdded="True"
    VerticalOptions="Center"
    HorizontalOptions="Center" 
    HeightRequest="300" WidthRequest="300"/>

On iOS
image

@paradisehuman
Copy link

@golo98 My problem was solved. my after effect project had problem. I replace another project and it works like a charm. Thank you .

@mhmd-azeez
Copy link
Contributor

mhmd-azeez commented Feb 25, 2018

@martijn00 is there any reason we can't have an "ImagesFolderPath" property on the animation view itself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants