diff --git a/library/src/main/java/com/wnafee/vector/compat/PathAnimatorInflater.java b/library/src/main/java/com/wnafee/vector/compat/PathAnimatorInflater.java index dd9e6f6..8354b82 100644 --- a/library/src/main/java/com/wnafee/vector/compat/PathAnimatorInflater.java +++ b/library/src/main/java/com/wnafee/vector/compat/PathAnimatorInflater.java @@ -15,6 +15,7 @@ */ import android.animation.Animator; import android.animation.AnimatorSet; +import android.animation.ArgbEvaluator; import android.animation.ObjectAnimator; import android.animation.TypeEvaluator; import android.animation.ValueAnimator; @@ -22,6 +23,7 @@ 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; @@ -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; @@ -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);