8

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?

ademar111190
  • 14,215
  • 14
  • 85
  • 114

4 Answers4

13

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.

0gravity
  • 2,682
  • 4
  • 24
  • 33
  • here have two interpolators but have too two animations, it's works, but if it was just one animation it could be a little better – ademar111190 Aug 13 '12 at 13:22
11

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);
}

}
ademar111190
  • 14,215
  • 14
  • 85
  • 114
3

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

Domi mtz
  • 91
  • 1
  • 2
0

This post is already old, but for the next one who lands here:

The Example from @ademar111190 isn't quite right.

In getInterpolation(float t), t needs to be between 0 and 1. This is the Timespan for the Animation. The used input 500 will completely ignore the 2nd Interpolator.

Here the link to the Documentation: Time Interpolator

omar jayed
  • 850
  • 5
  • 16