package android.animation;
import android.graphics.Path;
import android.graphics.PointF;
import java.util.ArrayList;
public class PathKeyframes implements Keyframes {
private static final int FRACTION_OFFSET = 0;
private static final int X_OFFSET = 1;
private static final int Y_OFFSET = 2;
private static final int NUM_COMPONENTS = 3;
private static final ArrayList<Keyframe> EMPTY_KEYFRAMES = new ArrayList<Keyframe>();
private PointF mTempPointF = new PointF();
private float[] mKeyframeData;
public PathKeyframes(Path path) {
this(path, 0.5f);
}
public PathKeyframes(Path path, float error) {
if (path == null || path.isEmpty()) {
throw new IllegalArgumentException("The path must not be null or empty");
}
mKeyframeData = path.approximate(error);
}
@Override
public ArrayList<Keyframe> getKeyframes() {
return EMPTY_KEYFRAMES;
}
@Override
public Object getValue(float fraction) {
int numPoints = mKeyframeData.length / 3;
if (fraction < 0) {
return interpolateInRange(fraction, 0, 1);
} else if (fraction > 1) {
return interpolateInRange(fraction, numPoints - 2, numPoints - 1);
} else if (fraction == 0) {
return pointForIndex(0);
} else if (fraction == 1) {
return pointForIndex(numPoints - 1);
} else {
int low = 0;
int high = numPoints - 1;
while (low <= high) {
int mid = (low + high) / 2;
float midFraction = mKeyframeData[(mid * NUM_COMPONENTS) + FRACTION_OFFSET];
if (fraction < midFraction) {
high = mid - 1;
} else if (fraction > midFraction) {
low = mid + 1;
} else {
return pointForIndex(mid);
}
}
return interpolateInRange(fraction, high, low);
}
}
private PointF interpolateInRange(float fraction, int startIndex, int endIndex) {
int startBase = (startIndex * NUM_COMPONENTS);
int endBase = (endIndex * NUM_COMPONENTS);
float startFraction = mKeyframeData[startBase + FRACTION_OFFSET];
float endFraction = mKeyframeData[endBase + FRACTION_OFFSET];
float intervalFraction = (fraction - startFraction)/(endFraction - startFraction);
float startX = mKeyframeData[startBase + X_OFFSET];
float endX = mKeyframeData[endBase + X_OFFSET];
float startY = mKeyframeData[startBase + Y_OFFSET];
float endY = mKeyframeData[endBase + Y_OFFSET];
float x = interpolate(intervalFraction, startX, endX);
float y = interpolate(intervalFraction, startY, endY);
mTempPointF.set(x, y);
return mTempPointF;
}
@Override
public void setEvaluator(TypeEvaluator evaluator) {
}
@Override
public Class getType() {
return PointF.class;
}
@Override
public Keyframes clone() {
Keyframes clone = null;
try {
clone = (Keyframes) super.clone();
} catch (CloneNotSupportedException e) {}
return clone;
}
private PointF pointForIndex(int index) {
int base = (index * NUM_COMPONENTS);
int xOffset = base + X_OFFSET;
int yOffset = base + Y_OFFSET;
mTempPointF.set(mKeyframeData[xOffset], mKeyframeData[yOffset]);
return mTempPointF;
}
private static float interpolate(float fraction, float startValue, float endValue) {
float diff = endValue - startValue;
return startValue + (diff * fraction);
}
public FloatKeyframes createXFloatKeyframes() {
return new FloatKeyframesBase() {
@Override
public float getFloatValue(float fraction) {
PointF pointF = (PointF) PathKeyframes.this.getValue(fraction);
return pointF.x;
}
};
}
public FloatKeyframes createYFloatKeyframes() {
return new FloatKeyframesBase() {
@Override
public float getFloatValue(float fraction) {
PointF pointF = (PointF) PathKeyframes.this.getValue(fraction);
return pointF.y;
}
};
}
public IntKeyframes createXIntKeyframes() {
return new IntKeyframesBase() {
@Override
public int getIntValue(float fraction) {
PointF pointF = (PointF) PathKeyframes.this.getValue(fraction);
return Math.round(pointF.x);
}
};
}
public IntKeyframes createYIntKeyframes() {
return new IntKeyframesBase() {
@Override
public int getIntValue(float fraction) {
PointF pointF = (PointF) PathKeyframes.this.getValue(fraction);
return Math.round(pointF.y);
}
};
}
private abstract static class SimpleKeyframes implements Keyframes {
@Override
public void setEvaluator(TypeEvaluator evaluator) {
}
@Override
public ArrayList<Keyframe> getKeyframes() {
return EMPTY_KEYFRAMES;
}
@Override
public Keyframes clone() {
Keyframes clone = null;
try {
clone = (Keyframes) super.clone();
} catch (CloneNotSupportedException e) {}
return clone;
}
}
abstract static class IntKeyframesBase extends SimpleKeyframes implements IntKeyframes {
@Override
public Class getType() {
return Integer.class;
}
@Override
public Object getValue(float fraction) {
return getIntValue(fraction);
}
}
abstract static class FloatKeyframesBase extends SimpleKeyframes
implements FloatKeyframes {
@Override
public Class getType() {
return Float.class;
}
@Override
public Object getValue(float fraction) {
return getFloatValue(fraction);
}
}
}