I am trying to move two markers at the same time with ObjectAnimator and AnimatorSet but I don't get it to work, here I share the code I am using.
handler =new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (list.size() > 1 ){
if (index < list.size() - 1) {
index++;
next = index + 1;
}
if (index < list.size() - 1) {
starposition = list.get(index);
endposition = list.get(next);
}
if (index2 < list2.size() - 1) {
index2++;
next2 = index2 + 1;
}
if (index2 < list2.size() - 1) {
starposition2 = list2.get(index2);
endposition2 = list2.get(next2);
}
final ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(marker,"rotation",0,1);
objectAnimator.setInterpolator(new LinearInterpolator());
objectAnimator.setDuration(2000);
objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
v = objectAnimator.getAnimatedFraction();
logg = v * endposition.longitude + (1 - v)
* starposition.longitude;
latt = v * endposition.latitude + (1 - v)
* starposition.latitude;
LatLng newpos = new LatLng(latt, logg);
marker.setPosition(newpos);
marker.setAnchor(0.5f, 0.5f);
marker.setRotation(getBearing(starposition, newpos));
}
});
final ObjectAnimator objectAnimator2= ObjectAnimator.ofFloat(marker,"rotation",0,1);
objectAnimator2.setInterpolator(new LinearInterpolator());
objectAnimator2.setDuration(2000);
objectAnimator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
h = objectAnimator2.getAnimatedFraction();
logg2 = h * endposition2.longitude + (1 - h)
* starposition2.longitude;
latt2 = h * endposition2.latitude + (1 - h)
* starposition2.latitude;
LatLng newpos2 = new LatLng(latt2,logg2);
marker2.setPosition(newpos2);
marker2.setAnchor(0.5f, 0.5f);
marker2.setRotation(getBearing(starposition2, newpos2));
}
});
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(objectAnimator,objectAnimator2);
animatorSet.start();
handler.postDelayed(this, 3000);
}
}
},3000);
I have two lists where I store the LatLngs for each marker, then I create the Objectanimator to give the animation to each marker, but ... when I run it, the two do not move at the same time but in a sequential when the first one ends, the other begins to move