0

我正在爲谷歌地圖v2中的汽車組(標記)同時旋轉的動畫組實現動畫。在google地圖v2中同時旋轉的標記組的動畫android

這樣我需要在handler.post()方法內編寫我的動畫部分。

通過這樣做動畫部分(handler.post()方法)只運行一個標記。

我在for循環中編寫了handler.post()方法。它只在第一次運行,這意味着只有一個標記正在旋轉。之後,它不工作。我的代碼如下。

private void animateCarsInMarkers(final MarkerOptions mark, final long bearing, final LatLng startPosition, final int position){ 

    final Handler handler = new Handler(); 
    final long start = SystemClock.uptimeMillis(); 
    final long duration = 3000; 
    final Interpolator interpolator = new LinearInterpolator(); 
    final Marker marker = mGoogleMap.addMarker(mark); 

    final float rotationValue = Float.parseFloat(String.valueOf(bearing)); 

    try { 
     if(tempCarsArray != null && !tempCarsArray.isEmpty()){ 
      sLongitude = tempCarsArray.get(position).getDouble(LONGITUDE); 
      sLatitude = tempCarsArray.get(position).getDouble(LATITUDE); 
      sBearing = tempCarsArray.get(position).getLong("Bearing"); 

      final double dLongitude = startPosition.longitude; 
      final double dLatitude = startPosition.latitude; 
      handler.post(new Runnable() { 
       @Override 
       public void run() { 
        long elapsed = SystemClock.uptimeMillis() - start; 
        float time = interpolator.getInterpolation((float) elapsed/duration); 
        double lng = time * dLongitude + (1 - time) * sLongitude; 
        double lat = time * dLatitude + (1 - time) * sLatitude; 
        float rotationValue = time * dbearing + (1-time) * sBearing; 
        marker.setRotation((-rotationValue > 180) ? (rotationValue/2) : rotationValue); 
        marker.setPosition(new LatLng(lat, lng)); 
        if (time < 1.0) { 
         handler.postDelayed(this, 16); 
        } 
       } 
      }); 
     tempCarsArray.clear(); 
    } else { 

      marker.setPosition(startPosition); 
      marker.setRotation(-rotationValue > 180 ? rotationValue/2 : rotationValue); 
     } 
    }catch (JSONException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

這是我在裏面調用循環的方法。但它只在循環中第一次運行。後來它不起作用。所以在這組標記中只有一個標記是動畫的。我的for循環如下:

for(int i=0; i<size; i++){ 
animateCarsInMarkers(mark, bearing, latLng, i); 
} 

此循環只在i = 0的值時運行,它不會再運行。 在此先感謝。

回答

0

如果您的'for loop'只運行一次,則表示您的「for循環條件部分」i<size已針對此實例滿足條件。我會建議你實際登錄「大小」並檢查其值。

如果您正在尋找the size of an array,使用tempCarsArray.length像:

for(int i=0; i < tempCarsArray.length; i++){ 
    animateCarsInMarkers(mark, bearing, latLng, i); 
} 

要檢查是否size原因造成的問題,試試這個。

如果你知道你期待的旋轉標誌物的實際數量,嘗試用一個整數來取代現在,像:

//if you're expecting 5 markers 
for(int i=0; i < 5 ; i++){ 
    animateCarsInMarkers(mark, bearing, latLng, i); 
} 

如果所有標記做旋轉,這意味着你的尺寸可變的只有值爲1.

+0

感謝您的解決方案。這個對我有用。我糾正了我的錯誤,這是因爲只有大小。但仍然只有一個標記是動畫的。你能否建議我採用任何可能的方法**在同一時間製作一組標記**。 @newguy –

+0

不要忘記接受或upvote答案表明它爲你工作。現在,對於後續問題,這可能會有所幫助。[SO線程](http://stackoverflow.com/questions/14864664/animating-markers-on-google-maps-v2)。視頻演示和代碼包括在內。 – noogui