Android : Change ObjectAnimator interpolator midway through animation

on Friday, April 17, 2015


currently I have created an animation using Android's ObjectAnimator. This is what I use to do my animations:



public class AnimatableFloat implements Animator.AnimatorListener {

private ObjectAnimator animator;

public ObjectAnimator animator() {
return animator;
}

private AnimatableFloat(){
}

private float value;
public float getValue(){
return value;
}

public void setValue(float value){
this.value = value;
}

public static AnimatableFloat createStartedAnimation(TimeInterpolator i, float start, float end, int duration){
AnimatableFloat af = new AnimatableFloat();
ObjectAnimator oa = ObjectAnimator.ofFloat(af, "value", start, end);
if(i != null)
oa.setInterpolator(i);
oa.setDuration(duration);
oa.addListener(af);
af.animator = oa;
oa.start();
return af;
}

private boolean isStarted, isEnded;

public boolean isStarted() {
return isStarted;
}

public boolean isEnded() {
return isEnded;
}

@Override
public void onAnimationStart(Animator animation) {
//care about start
isStarted = true;
}

@Override
public void onAnimationEnd(Animator animation) {
//care about end
isEnded = true;
}

@Override
public void onAnimationCancel(Animator animation) {

}

@Override
public void onAnimationRepeat(Animator animation) {

}


}


And I start it using the following code:



scaleAnim = AnimatableFloat.createStartedAnimation(new AccelerateInterpolator(1f), 0, radi, (int)(CanvasView.FPS * 15f));


Now this works alright for one interpolator, but is it possible to change the interpolator mid animation? I have tried creating a new animation from the previous point, but this completely resets the animations. I have also tried the following:



long ppt = scaleAnim.animator().getCurrentPlayTime();
float radi = (float)Math.sqrt(Math.pow(getEndX() - getX(), 2) + Math.pow(getEndY() - getY(), 2));
scaleAnim = AnimatableFloat.createStartedAnimation(new AccelerateInterpolator(1f), scaleAnim.getValue(), radi, (int)(CanvasView.FPS * 25f));
scaleAnim.animator().setCurrentPlayTime(ppt);


But this doesn't seem to work, as it doesn't seem to continue where it left out. Does anybody know a way where I can change the interpolator of the animation mid way without completely resetting the animation? Thanks.


0 comments:

Post a Comment