Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for float and int types as well as color for animator's valueFrom/valueTo. #17

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Xml;
Expand All @@ -46,7 +48,9 @@ public class PathAnimatorInflater {
private static final int TOGETHER = 0;
private static final int SEQUENTIALLY = 1;

private static final int VALUE_TYPE_PATH = 2;
private static final int VALUE_TYPE_FLOAT = 0;
private static final int VALUE_TYPE_INT = 1;
private static final int VALUE_TYPE_PATH = 2;

private static final boolean DBG_ANIMATOR_INFLATER = false;

Expand Down Expand Up @@ -220,15 +224,62 @@ private static void parseAnimatorFromTypeArray(ValueAnimator anim,

long startDelay = arrayAnimator.getInt(R.styleable.Animator_android_startOffset, 0);

int valueType = arrayAnimator.getInt(R.styleable.Animator_vc_valueType, 0);
int valueType = arrayAnimator.getInt(R.styleable.Animator_vc_valueType, VALUE_TYPE_FLOAT);

TypeEvaluator evaluator = null;

// Must be a path animator by the time I reach here
if (valueType == VALUE_TYPE_PATH) {
boolean hasValueFrom = arrayAnimator.hasValue(R.styleable.Animator_android_valueFrom);
boolean hasValueTo = arrayAnimator.hasValue(R.styleable.Animator_android_valueTo);

/*
* Check if animating color by checking if valueFrom or valueTo has a
* leading '#' character.
*/
boolean isValueFromColor = false;
boolean isValueToColor = false;
if (hasValueFrom) {
CharSequence text = arrayAnimator.getText(R.styleable.Animator_android_valueFrom);
isValueFromColor = text.length() != 0 && text.charAt(0) == '#';
}
if (hasValueTo) {
CharSequence text = arrayAnimator.getText(R.styleable.Animator_android_valueTo);
isValueToColor = text.length() != 0 && text.charAt(0) == '#';
}
boolean isColor = isValueFromColor || isValueToColor;
if (isColor) {
valueType = VALUE_TYPE_INT;
}

if (valueType == VALUE_TYPE_FLOAT) {
float valueFrom = arrayAnimator.getFloat(R.styleable.Animator_android_valueFrom, 0);
float valueTo = arrayAnimator.getFloat(R.styleable.Animator_android_valueTo, 0);

if (hasValueFrom && hasValueTo) {
anim.setFloatValues(valueFrom, valueTo);
} else if (hasValueTo) {
anim.setFloatValues(valueTo);
}
} else if (valueType == VALUE_TYPE_INT) {
int valueFrom;
int valueTo;
if (isColor) {
valueFrom = arrayAnimator.getColor(R.styleable.Animator_android_valueFrom, Color.BLACK);
valueTo = arrayAnimator.getColor(R.styleable.Animator_android_valueTo, Color.BLACK);
evaluator = new ArgbEvaluator();
} else {
valueFrom = arrayAnimator.getInt(R.styleable.Animator_android_valueFrom, 0);
valueTo = arrayAnimator.getInt(R.styleable.Animator_android_valueTo, 0);
}

if (hasValueFrom && hasValueTo) {
anim.setIntValues(valueFrom, valueTo);
} else if (arrayAnimator.hasValue(R.styleable.Animator_android_valueTo)) {
anim.setIntValues(valueTo);
}
} else if (valueType == VALUE_TYPE_PATH) {
evaluator = setupAnimatorForPath(anim, arrayAnimator);
} else {
throw new IllegalArgumentException("target is not a pathType target");
throw new IllegalArgumentException("Unsupported value type: " + valueType);
}

anim.setDuration(duration);
Expand Down