2015-12-01 49 views
0

我有這個下面的for循環等待,直到裏面的方法完成了循環的Android

陣列部分具有循環我打電話animateMarker內部的各個部分,其在動畫標記的位置「經緯度」 列表我的地圖像2秒

我的問題是,這個循環變得如此之快,我甚至無法看到每個部分的動畫。它只是表明該數組中的最後一節,也有時FREEZ應用

的animatemarker內部具有hundler與後延遲

我希望看到每個animateMarker每個部分在我的循環 我與線程nooby所以我真的需要幫助,

這裏問題

 ArrayList<LatLng> sections = gd.getSection(mDoc); 
      for (int j = 0; j < sections.size(); j++) { 
       animateMarker(busMarker, new LatLng(sections.get(j).latitude, sections.get(j).longitude), false); 
      } 

我animateMarker(已經沒有任何問題工作正常!)

public void animateMarker(final Marker marker, final LatLng toPosition, 
           final boolean hideMarker) { 
     final Handler handler = new Handler(); 
     final long start = SystemClock.uptimeMillis(); 
     Projection proj = mMap.getProjection(); 
     Point startPoint = proj.toScreenLocation(marker.getPosition()); 
     final LatLng startLatLng = proj.fromScreenLocation(startPoint); 
     final long duration = 2000; 

     final Interpolator interpolator = new LinearInterpolator(); 

     handler.post(new Runnable() { 
      @Override 
      public void run() { 
       long elapsed = SystemClock.uptimeMillis() - start; 
       float t = interpolator.getInterpolation((float) elapsed 
         /duration); 
       double lng = t * toPosition.longitude + (1 - t) 
         * startLatLng.longitude; 
       double lat = t * toPosition.latitude + (1 - t) 
         * startLatLng.latitude; 
       marker.setPosition(new LatLng(lat, lng)); 

       if (t < 1.0) { 
        // Post again 16ms later. 
        handler.postDelayed(this, 16); 
       } else { 
        if (hideMarker) { 
         marker.setVisible(false); 
        } else { 
         marker.setVisible(true); 
        } 
       } 
      } 
     }); 
    } 
+0

您是否厭倦了在循環內張貼延遲? –

+0

想要這樣嗎? http://stackoverflow.com/questions/20896245/how-to-delay-a-loop-in-android-without-using-thread-sleep –

+0

http://stackoverflow.com/questions/17392046/loop-with-延遲期 –

回答

0

我不知道是否有正確的方法,但是這是方法我應該做的,

您可以打開一個新的線程,並呼籲在該線程的for循環。 您還需要使用地圖API玩動畫2秒,並讓可運行對象爲單個實例。所以它會更加記憶友好。

for (int j = 0; j < sections.size(); j++) { 
    synchronized (this) { 
     runOnUiThread(new Runnable() 
     { 
     public void run() 
     { 
      animateMarker(busMarker, new LatLng(sections.get(j).latitude, sections.get(j).longitude), false); 
     } 
     });    
    } 
    sleep(2000); 
}