2017-05-06 61 views
2

編輯:爲了說清楚,加載速度很慢,而不是動畫。爲什麼我的洛蒂動畫這樣慢?

我如下使用它上的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上,我能看到的只是文本。

我的問題是,一旦對話框彈出,我該如何讓動畫顯示出來?

回答

1

終於解決了它,這要歸功於Gabriel Peal

其實很簡單。爲了避免使該組合物時,對話框顯示出來,我們在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(); 
      } 
     }); 

這樣,動畫會彈出對話框。