编辑:要说清楚,加载速度慢,而不是动画。
我在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上,我唯一能看到的是文本。
我的问题是,如何在对话框弹出后立即显示动画?
答案 0 :(得分:2)
实际上非常简单。为了避免在对话框出现时渲染合成,我们先在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();
}
});
这样,动画将弹出 对话框。