问:我现在正面临一个问题,我正在开发类似于Uber或lyft的Android应用程序,但是我面临的问题是汽车在地图上的行驶平稳,但有时它会在一个位置以360度旋转,我需要停下来请帮忙,这是我的动画代码。
private void animateMarkr(double laat, double lnng,Location prevLocation) {
final LatLng toPosition=new LatLng(laat,lnng);
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = mGoogleMap.getProjection();
Point startPoint = proj.toScreenLocation(carMarker.getPosition());
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
final long duration = 3000;
final boolean hideMarker=false;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
LatLng pre=carMarker.getPosition();
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;
// carMarker.setRotation(getBearing(pre,new LatLng(lat,lng)));
carMarker.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
handler.postDelayed(this, 20);
} else {
if (hideMarker) {
carMarker.setVisible(false);
} else {
carMarker.setVisible(true);
}
}
}
});
}
//Here is my Rotation Code
private void rotateMarker(Location location,Location preLocation){
Location prevLocation = new Location(LocationManager.GPS_PROVIDER);
prevLocation.setLatitude(carMarker.getPosition().latitude);
prevLocation.setLongitude(carMarker.getPosition().longitude);
final float toRotation = prevLocation.bearingTo(location);
performRotaion(toRotation);
}
private void performRotaion(final float toRotation){
if(!isMarkerRotating) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final float startRotation = carMarker.getRotation();
final float totalDegree=0;
final long duration = 1000;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
isMarkerRotating = true;
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed / duration);
float rot = t toRotation + (1 - t) startRotation;
if(carMarker != null){
carMarker.setAnchor(0.5f, 0.5f);
// if(rot<0.0f && rot>-355.0f) {
carMarker.setRotation(-rot > 180 ? rot / 2 : rot);
carMarker.setFlat(true);
}
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
isMarkerRotating = false;
}
}
});
}
}
// method call
rotateMarker(location,previcsLocation);
animateMarkr(location.getLatitude(), location.getLongitude(),previcsLocation);
答:我已经实现了同一段代码,它的工作就像一个魅力。
私有无效animateMarker(LatLng latlng,最终标记标记){
最终的LatLng目标= latlng;
最后的持续时间= 800;
最终处理程序处理程序= new Handler();
最后的长时间开始= SystemClock.uptimeMillis();
投影项目= mMap.getProjection();
点startPoint = proj.toScreenLocation(marker.getPosition());
最终LatLng startLatLng = proj.fromScreenLocation(startPoint);
最终插值器interpolator = new LinearInterpolator();
handler.post(new Runnable(){
@Override
公共无效run(){
长时间已过= SystemClock.uptimeMillis()-开始;
float t = interpolator.getInterpolation((float)经过/持续时间);
double lng = t * target.longitude +(1- t)* startLatLng.longitude;
double lat = t * target.latitude +(1- t)* startLatLng.latitude;
marker.setPosition(new LatLng(lat,lng));
如果(t <1.0){
// 10毫秒后再次发布。
handler.postDelayed(this,10);
}其他{
//动画结束
}
}
});
}