2016-10-05 8 views
2

Я использую этот код, чтобы анимировать маркер, чтобы воспроизвести движение транспортного средства на картах Google, но маркер энергично встряхивает, когда маркер .setPosition вызывается в функции обработчика. Код ниже:Маркер шаткий при добавлении анимации google maps android

public static void animateMarker(final GoogleMap map, final Marker marker, final LatLng toPosition) { 
    final Handler handler = new Handler(); 
    final long start = SystemClock.uptimeMillis(); 
    Projection proj = map.getProjection(); 
    Point startPoint = proj.toScreenLocation(marker.getPosition()); 
    final LatLng startLatLng = proj.fromScreenLocation(startPoint); 
    final long duration = 5000; 
    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) { 
       handler.postDelayed(this, 16); 
      } 
     } 
    }); 
} 
+0

Вы смогли решить эту проблему? –

ответ

0

Try используя AccelerateDecelerateInterpolator, интерполятор, скорость которого пусков изменения и заканчивается медленно, но ускоряется по центру, вместо LinearInterpolator, используемый в этом Google video tutorial. Маркерная анимация была бесшовной. Вот фрагмент:

public class MarkerAnimation { 
    static void animateMarkerToGB(final Marker marker, final LatLng finalPosition, final LatLngInterpolator latLngInterpolator) { 
     final LatLng startPosition = marker.getPosition(); 
     final Handler handler = new Handler(); 
     final long start = SystemClock.uptimeMillis(); 
     final Interpolator interpolator = new AccelerateDecelerateInterpolator(); 
     final float durationInMs = 3000; 

    handler.post(new Runnable() { 
     long elapsed; 
     float t; 
     float v; 

     @Override 
     public void run() { 
      // Calculate progress using interpolator 
      elapsed = SystemClock.uptimeMillis() - start; 
      t = elapsed/durationInMs; 
      v = interpolator.getInterpolation(t); 

      marker.setPosition(latLngInterpolator.interpolate(v, startPosition, finalPosition)); 

      // Repeat till progress is complete. 
      if (t < 1) { 
       // Post again 16ms later. 
       handler.postDelayed(this, 16); 
      } 
     } 
    }); 
} 

Full code [here](https://gist.github.com/broady/6314689).