Home » Android » java – animation with two interpolators

java – animation with two interpolators

Posted by: admin June 15, 2020 Leave a comment

Questions:

I need to do a animation with two interpolators, for example the animation have 1 seconde of duration for 0 sec to 0.5 sec uses accelerate interpolaor ans for 0.5 to 1 sec use bounce interpolator.

have a way for to do this?

How to&Answers:

You can try something like this:

<?xml version="1.0" encoding="utf-8"?>
<set 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">

<translate
    android:interpolator="@android:anim/bounce_interpolator"
    android:fromYDelta="0%p"
    android:toYDelta="100"
    android:duration="500"/>

<translate
    android:interpolator = "@android:anim/accelerate_interpolator"
    android:fromYDelta="100"
    android:toYDelta="100"
    android:fromXDelta="0"
    android:toXDelta="100"
    android:startOffset="500"
    android:duration="1000"/>

</set>

This uses two interpolators, the first one is a bounce that moves a view for halve a second. And the second interpolator is an accelerate interpolator that moves a view to the right after halve a second has passed, for a duration of one second. Therefore with a total animation time of 1 second. Hope that helps.

Answer:

I do with one only animation:

Animation animation = new TranslateAnimation(0,100,0,0);
animation.setDuration(1000);
pointerAnimation.setInterpolator(new CustomBounceInterpolator(500));
view.startAnimation(animation);

and the CustomInterpolator Class:

public class CustomBounceInterpolator implements Interpolator {

private float timeDivider;
private AccelerateInterpolator a;
private BounceInterpolator b;

public CustomBounceInterpolator(float timeDivider) {
    a = new AccelerateInterpolator();
    b = new BounceInterpolator();
    this.timeDivider = timeDivider;
}

public float getInterpolation(float t) {
    if (t < timeDivider)
        return a.getInterpolation(t);
    else
        return b.getInterpolation(t);
}

}

Answer:

Hello in the example there is a failure for the anonymous class.

its not this:pointerAnimation.setInterpolator(new CustomInterpolator(500));

it is this:pointerAnimation.setInterpolator(new CustomBounceInterpolator(500));

many thanks anyway helped me a lot