Skip to content

Instantly share code, notes, and snippets.

@westonal
Last active March 31, 2019 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonal/d1e196d437682ae88e00a4914d4c597a to your computer and use it in GitHub Desktop.
Save westonal/d1e196d437682ae88e00a4914d4c597a to your computer and use it in GitHub Desktop.
Interpolators, pulse and repeat
package com.example.animation;
import android.view.animation.Interpolator;
public final class PulseInterpolator implements Interpolator {
private final int duration;
private final int offFor;
/**
* Creates a single pulse signal that has a linear climb from 0..1 over 1 unit of time.
* Then it stays at 1 for {@param onFor} units of time.
* Then it has a linear drop from 1..0 over 1 unit of time.
* Then it stays at 0 for {@param offFor} units of time.
* <p>
* Total duration of animation {@param onFor} + {@param offFor} + 2 units.
*
* @param onFor Units of time to stay on for, relative to the duration of the climb/drop.
* @param offFor Units of time to stay off for, relative to the duration of the climb/drop.
*/
public PulseInterpolator(int onFor, int offFor) {
this.duration = onFor + offFor + 2;
this.offFor = offFor;
}
@Override
public float getInterpolation(float v) {
v *= duration;
if (v > 1) {
v = duration - v - offFor;
}
return Math.max(0, Math.min(1, v));
}
}
package com.example.animation;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
public final class RepeatInterpolator implements Interpolator {
private final double repeats;
private final Interpolator interpolator;
public RepeatInterpolator(double repeats) {
this(new LinearInterpolator(), repeats);
}
public RepeatInterpolator(Interpolator interpolator, double repeats) {
this.interpolator = interpolator;
this.repeats = repeats;
}
@Override
public float getInterpolation(float v) {
final double v1 = v % (1.0 / repeats);
return interpolator.getInterpolation((float) (v1 * repeats));
}
}
@westonal
Copy link
Author

westonal commented Mar 31, 2019

Example: new PulseInterpolator(3, 1)

image

@westonal
Copy link
Author

westonal commented Mar 31, 2019

new RepeatInterpolator(new PulseInterpolator(1, 1), 2)

image

new RepeatInterpolator(2.5)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment