Last active
March 31, 2019 14:49
-
-
Save westonal/d1e196d437682ae88e00a4914d4c597a to your computer and use it in GitHub Desktop.
Interpolators, pulse and repeat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
new PulseInterpolator(3, 1)