【问题标题】:Why is my Lottie animation so slow to load?为什么我的 Lottie 动画加载这么慢?
【发布时间】:2017-10-04 13:22:15
【问题描述】:

为了清楚起见,加载速度很慢,而不是动画。

我在 AsyncTask 上使用它如下:

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
}
}

这里一切正常。

我对这段代码的问题是,一旦对话框出现,Lottie 动画需要一秒钟才能出现在屏幕上。如果它在 4G 网络上,我可以看到动画。如果它在 WIFI 上,我唯一能看到的就是文字。

如何让动画在对话框弹出后立即显示?

【问题讨论】:

    标签: android lottie


    【解决方案1】:

    其实很简单。为了避免在对话框出现时渲染合成,我们在 LottieComposition 中预先渲染,如下所示:

            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();
                }
            });
    

    这样,动画将弹出 对话框。

    【讨论】:

    • 这已被弃用 (3.2.2)
    猜你喜欢
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多