More native interpolators

Gotta collect 'em all

Change-Id: I3ccc2b5c842b27b906c8a0470fbedc2bf285bc38
This commit is contained in:
John Reck
2014-05-12 16:39:41 -07:00
parent 315c329544
commit c8ac775659
12 changed files with 261 additions and 12 deletions

View File

@@ -20,12 +20,17 @@ import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import com.android.internal.view.animation.HasNativeInterpolator;
import com.android.internal.view.animation.NativeInterpolatorFactory;
import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
/**
* An interpolator where the rate of change starts out slowly and
* and then accelerates.
*
*/
public class AccelerateInterpolator implements Interpolator {
@HasNativeInterpolator
public class AccelerateInterpolator implements Interpolator, NativeInterpolatorFactory {
private final float mFactor;
private final double mDoubleFactor;
@@ -64,4 +69,10 @@ public class AccelerateInterpolator implements Interpolator {
return (float)Math.pow(input, mDoubleFactor);
}
}
/** @hide */
@Override
public long createNativeInterpolator() {
return NativeInterpolatorFactoryHelper.createAccelerateInterpolator(mFactor);
}
}

View File

@@ -20,10 +20,15 @@ import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import com.android.internal.view.animation.HasNativeInterpolator;
import com.android.internal.view.animation.NativeInterpolatorFactory;
import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
/**
* An interpolator where the change starts backward then flings forward.
*/
public class AnticipateInterpolator implements Interpolator {
@HasNativeInterpolator
public class AnticipateInterpolator implements Interpolator, NativeInterpolatorFactory {
private final float mTension;
public AnticipateInterpolator() {
@@ -53,4 +58,10 @@ public class AnticipateInterpolator implements Interpolator {
// a(t) = t * t * ((tension + 1) * t - tension)
return t * t * ((mTension + 1) * t - mTension);
}
/** @hide */
@Override
public long createNativeInterpolator() {
return NativeInterpolatorFactoryHelper.createAnticipateInterpolator(mTension);
}
}

View File

@@ -19,6 +19,11 @@ package android.view.animation;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import com.android.internal.view.animation.HasNativeInterpolator;
import com.android.internal.view.animation.NativeInterpolatorFactory;
import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
import static com.android.internal.R.styleable.AnticipateOvershootInterpolator_extraTension;
import static com.android.internal.R.styleable.AnticipateOvershootInterpolator_tension;
import static com.android.internal.R.styleable.AnticipateOvershootInterpolator;
@@ -27,7 +32,8 @@ import static com.android.internal.R.styleable.AnticipateOvershootInterpolator;
* An interpolator where the change starts backward then flings forward and overshoots
* the target value and finally goes back to the final value.
*/
public class AnticipateOvershootInterpolator implements Interpolator {
@HasNativeInterpolator
public class AnticipateOvershootInterpolator implements Interpolator, NativeInterpolatorFactory {
private final float mTension;
public AnticipateOvershootInterpolator() {
@@ -80,4 +86,10 @@ public class AnticipateOvershootInterpolator implements Interpolator {
if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);
else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);
}
/** @hide */
@Override
public long createNativeInterpolator() {
return NativeInterpolatorFactoryHelper.createAnticipateOvershootInterpolator(mTension);
}
}

View File

@@ -19,10 +19,15 @@ package android.view.animation;
import android.content.Context;
import android.util.AttributeSet;
import com.android.internal.view.animation.HasNativeInterpolator;
import com.android.internal.view.animation.NativeInterpolatorFactory;
import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
/**
* An interpolator where the change bounces at the end.
*/
public class BounceInterpolator implements Interpolator {
@HasNativeInterpolator
public class BounceInterpolator implements Interpolator, NativeInterpolatorFactory {
public BounceInterpolator() {
}
@@ -47,4 +52,10 @@ public class BounceInterpolator implements Interpolator {
else if (t < 0.9644f) return bounce(t - 0.8526f) + 0.9f;
else return bounce(t - 1.0435f) + 0.95f;
}
/** @hide */
@Override
public long createNativeInterpolator() {
return NativeInterpolatorFactoryHelper.createBounceInterpolator();
}
}

View File

@@ -20,12 +20,17 @@ import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import com.android.internal.view.animation.HasNativeInterpolator;
import com.android.internal.view.animation.NativeInterpolatorFactory;
import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
/**
* Repeats the animation for a specified number of cycles. The
* rate of change follows a sinusoidal pattern.
*
*/
public class CycleInterpolator implements Interpolator {
@HasNativeInterpolator
public class CycleInterpolator implements Interpolator, NativeInterpolatorFactory {
public CycleInterpolator(float cycles) {
mCycles = cycles;
}
@@ -44,4 +49,10 @@ public class CycleInterpolator implements Interpolator {
}
private float mCycles;
/** @hide */
@Override
public long createNativeInterpolator() {
return NativeInterpolatorFactoryHelper.createCycleInterpolator(mCycles);
}
}

View File

@@ -20,12 +20,17 @@ import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import com.android.internal.view.animation.HasNativeInterpolator;
import com.android.internal.view.animation.NativeInterpolatorFactory;
import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
/**
* An interpolator where the rate of change starts out quickly and
* and then decelerates.
*
*/
public class DecelerateInterpolator implements Interpolator {
@HasNativeInterpolator
public class DecelerateInterpolator implements Interpolator, NativeInterpolatorFactory {
public DecelerateInterpolator() {
}
@@ -60,4 +65,10 @@ public class DecelerateInterpolator implements Interpolator {
}
private float mFactor = 1.0f;
/** @hide */
@Override
public long createNativeInterpolator() {
return NativeInterpolatorFactoryHelper.createDecelerateInterpolator(mFactor);
}
}

View File

@@ -19,11 +19,16 @@ package android.view.animation;
import android.content.Context;
import android.util.AttributeSet;
import com.android.internal.view.animation.HasNativeInterpolator;
import com.android.internal.view.animation.NativeInterpolatorFactory;
import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
/**
* An interpolator where the rate of change is constant
*
*/
public class LinearInterpolator implements Interpolator {
@HasNativeInterpolator
public class LinearInterpolator implements Interpolator, NativeInterpolatorFactory {
public LinearInterpolator() {
}
@@ -34,4 +39,10 @@ public class LinearInterpolator implements Interpolator {
public float getInterpolation(float input) {
return input;
}
/** @hide */
@Override
public long createNativeInterpolator() {
return NativeInterpolatorFactoryHelper.createLinearInterpolator();
}
}

View File

@@ -20,11 +20,16 @@ import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import com.android.internal.view.animation.HasNativeInterpolator;
import com.android.internal.view.animation.NativeInterpolatorFactory;
import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
/**
* An interpolator where the change flings forward and overshoots the last value
* then comes back.
*/
public class OvershootInterpolator implements Interpolator {
@HasNativeInterpolator
public class OvershootInterpolator implements Interpolator, NativeInterpolatorFactory {
private final float mTension;
public OvershootInterpolator() {
@@ -56,4 +61,10 @@ public class OvershootInterpolator implements Interpolator {
t -= 1.0f;
return t * t * ((mTension + 1) * t + mTension) + 1.0f;
}
/** @hide */
@Override
public long createNativeInterpolator() {
return NativeInterpolatorFactoryHelper.createOvershootInterpolator(mTension);
}
}

View File

@@ -24,5 +24,13 @@ public final class NativeInterpolatorFactoryHelper {
private NativeInterpolatorFactoryHelper() {}
public static native long createAccelerateDecelerateInterpolator();
public static native long createAccelerateInterpolator(float factor);
public static native long createAnticipateInterpolator(float tension);
public static native long createAnticipateOvershootInterpolator(float tension);
public static native long createBounceInterpolator();
public static native long createCycleInterpolator(float cycles);
public static native long createDecelerateInterpolator(float factor);
public static native long createLinearInterpolator();
public static native long createOvershootInterpolator(float tension);
public static native long createLutInterpolator(float[] values);
}

View File

@@ -32,6 +32,38 @@ static jlong createAccelerateDecelerateInterpolator(JNIEnv* env, jobject clazz)
return reinterpret_cast<jlong>(new AccelerateDecelerateInterpolator());
}
static jlong createAccelerateInterpolator(JNIEnv* env, jobject clazz, jfloat factor) {
return reinterpret_cast<jlong>(new AccelerateInterpolator(factor));
}
static jlong createAnticipateInterpolator(JNIEnv* env, jobject clazz, jfloat tension) {
return reinterpret_cast<jlong>(new AnticipateInterpolator(tension));
}
static jlong createAnticipateOvershootInterpolator(JNIEnv* env, jobject clazz, jfloat tension) {
return reinterpret_cast<jlong>(new AnticipateOvershootInterpolator(tension));
}
static jlong createBounceInterpolator(JNIEnv* env, jobject clazz) {
return reinterpret_cast<jlong>(new BounceInterpolator());
}
static jlong createCycleInterpolator(JNIEnv* env, jobject clazz, jfloat cycles) {
return reinterpret_cast<jlong>(new CycleInterpolator(cycles));
}
static jlong createDecelerateInterpolator(JNIEnv* env, jobject clazz, jfloat factor) {
return reinterpret_cast<jlong>(new DecelerateInterpolator(factor));
}
static jlong createLinearInterpolator(JNIEnv* env, jobject clazz) {
return reinterpret_cast<jlong>(new LinearInterpolator());
}
static jlong createOvershootInterpolator(JNIEnv* env, jobject clazz, jfloat tension) {
return reinterpret_cast<jlong>(new OvershootInterpolator(tension));
}
static jlong createLutInterpolator(JNIEnv* env, jobject clazz, jfloatArray jlut) {
jsize len = env->GetArrayLength(jlut);
if (len <= 0) {
@@ -53,6 +85,14 @@ const char* const kClassPathName = "com/android/internal/view/animation/NativeIn
static JNINativeMethod gMethods[] = {
#ifdef USE_OPENGL_RENDERER
{ "createAccelerateDecelerateInterpolator", "()J", (void*) createAccelerateDecelerateInterpolator },
{ "createAccelerateInterpolator", "(F)J", (void*) createAccelerateInterpolator },
{ "createAnticipateInterpolator", "(F)J", (void*) createAnticipateInterpolator },
{ "createAnticipateOvershootInterpolator", "(F)J", (void*) createAnticipateOvershootInterpolator },
{ "createBounceInterpolator", "()J", (void*) createBounceInterpolator },
{ "createCycleInterpolator", "(F)J", (void*) createCycleInterpolator },
{ "createDecelerateInterpolator", "(F)J", (void*) createDecelerateInterpolator },
{ "createLinearInterpolator", "()J", (void*) createLinearInterpolator },
{ "createOvershootInterpolator", "(F)J", (void*) createOvershootInterpolator },
{ "createLutInterpolator", "([F)J", (void*) createLutInterpolator },
#endif
};

View File

@@ -18,7 +18,7 @@
#include "Interpolator.h"
#include <math.h>
#include <cmath>
#include <cutils/log.h>
#include "utils/MathUtils.h"
@@ -34,6 +34,62 @@ float AccelerateDecelerateInterpolator::interpolate(float input) {
return (float)(cosf((input + 1) * M_PI) / 2.0f) + 0.5f;
}
float AccelerateInterpolator::interpolate(float input) {
if (mFactor == 1.0f) {
return input * input;
} else {
return pow(input, mDoubleFactor);
}
}
float AnticipateInterpolator::interpolate(float t) {
return t * t * ((mTension + 1) * t - mTension);
}
static float a(float t, float s) {
return t * t * ((s + 1) * t - s);
}
static float o(float t, float s) {
return t * t * ((s + 1) * t + s);
}
float AnticipateOvershootInterpolator::interpolate(float t) {
if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);
else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);
}
static float bounce(float t) {
return t * t * 8.0f;
}
float BounceInterpolator::interpolate(float t) {
t *= 1.1226f;
if (t < 0.3535f) return bounce(t);
else if (t < 0.7408f) return bounce(t - 0.54719f) + 0.7f;
else if (t < 0.9644f) return bounce(t - 0.8526f) + 0.9f;
else return bounce(t - 1.0435f) + 0.95f;
}
float CycleInterpolator::interpolate(float input) {
return sinf(2 * mCycles * M_PI * input);
}
float DecelerateInterpolator::interpolate(float input) {
float result;
if (mFactor == 1.0f) {
result = 1.0f - (1.0f - input) * (1.0f - input);
} else {
result = 1.0f - pow((1.0f - input), 2 * mFactor);
}
return result;
}
float OvershootInterpolator::interpolate(float t) {
t -= 1.0f;
return t * t * ((mTension + 1) * t + mTension) + 1.0f;
}
LUTInterpolator::LUTInterpolator(float* values, size_t size) {
mValues = values;
mSize = size;

View File

@@ -37,12 +37,68 @@ protected:
class ANDROID_API AccelerateDecelerateInterpolator : public Interpolator {
public:
AccelerateDecelerateInterpolator() {}
virtual ~AccelerateDecelerateInterpolator() {}
virtual float interpolate(float input);
};
class ANDROID_API AccelerateInterpolator : public Interpolator {
public:
AccelerateInterpolator(float factor) : mFactor(factor), mDoubleFactor(factor*2) {}
virtual float interpolate(float input);
private:
const float mFactor;
const float mDoubleFactor;
};
class ANDROID_API AnticipateInterpolator : public Interpolator {
public:
AnticipateInterpolator(float tension) : mTension(tension) {}
virtual float interpolate(float input);
private:
const float mTension;
};
class ANDROID_API AnticipateOvershootInterpolator : public Interpolator {
public:
AnticipateOvershootInterpolator(float tension) : mTension(tension) {}
virtual float interpolate(float input);
private:
const float mTension;
};
class ANDROID_API BounceInterpolator : public Interpolator {
public:
virtual float interpolate(float input);
};
class ANDROID_API CycleInterpolator : public Interpolator {
public:
CycleInterpolator(float cycles) : mCycles(cycles) {}
virtual float interpolate(float input);
private:
const float mCycles;
};
class ANDROID_API DecelerateInterpolator : public Interpolator {
public:
DecelerateInterpolator(float factor) : mFactor(factor) {}
virtual float interpolate(float input);
private:
const float mFactor;
};
class ANDROID_API LinearInterpolator : public Interpolator {
public:
virtual float interpolate(float input) { return input; }
};
class ANDROID_API OvershootInterpolator : public Interpolator {
public:
OvershootInterpolator(float tension) : mTension(tension) {}
virtual float interpolate(float input);
private:
const float mTension;
};
class ANDROID_API LUTInterpolator : public Interpolator {
public:
LUTInterpolator(float* values, size_t size);