Rosenberg
Rosenberg

Reputation: 2444

Why is my Lottie animation so slow to load?

To make it clear, it's slow to load, not to animate.

I'm using it on an AsyncTask as follows:

public class GetCurrentLocation extends AsyncTask<String, Void, String>{

private Context mContext;
private View view;

public GetCurrentLocation (View view, Context mContext) {
this.view = view;
this.mContext = mContext;
}


protected void onPreExecute() {
super.onPreExecute();
    //Custom Dialog
    customDialog = new Dialog(mContext);
    customDialog.setContentView(R.layout.dialog_custom);
    customDialog.setTitle("Looking for address");

    //Custom Dialog Animation
    LottieAnimationView animationView = (LottieAnimationView) customDialog.findViewById(R.id.animation_view);
    animationView.setAnimation("PinJump.json"); //Lottie's premade animation
    animationView.loop(true);
    animationView.playAnimation();

    //Custom Dialog Text
    TextView text = (TextView) customDialog.findViewById(R.id.textView);
    text.setText(R.string.dialog_looking_for_address);
    customDialog.show();
}

protected String doInBackground(String... params) {
//Code removed to shorten codeblock, it calls gps location here
return null;
}

protected void onPostExecute(String result) {
super.onPostExecute(result);
customDialog.dismiss();
mPostSolicitationFragment.setLocalText(); //This is the method it transfer the GPS address to the view
}
}

Everything works well here.

My problem with this code is that once the dialog shows up, the Lottie animation takes a second before showing up on screen. If it's on 4G network I can see the animation. If it's on WIFI the only thing I can see is the text.

How can I make the animation shos up as soon as the dialog pops?

Upvotes: 4

Views: 5929

Answers (1)

Rosenberg
Rosenberg

Reputation: 2444

Pretty simple actually. In order to avoid rendering the composition when the dialog shows up, we render it before hand in LottieComposition, as shown below:

        LottieComposition.Factory.fromAssetFileName(mContext, "PinJump.json", new OnCompositionLoadedListener() {
            @Override
            public void onCompositionLoaded(LottieComposition composition) {
                mAnimationView.loop(true);
                mAnimationView.playAnimation();
                TextView text = (TextView) customDialog.findViewById(R.id.textView);
                text.setText(R.string.dialog_looking_for_address);
                customDialog.show();
            }
        });

That way, the animation will popup with the dialog.

Upvotes: 5

Related Questions