Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5bed88e1 authored by Chet Haase's avatar Chet Haase
Browse files

Animators can now have dimension and color values.

You can now use floats, ints, dimensions, or colors as input values
in XML for Animator objects. There is still a 'valueType' attribute
that lets you specify the number values to create the animator with,
though it defaults to floats (or in the case of color inputs, to ints).

Change-Id: I65f1df802db602c33f2a0308a663b6f808148e25
parent 7ae15f6c
Loading
Loading
Loading
Loading
+75 −51
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.content.res.Resources.NotFoundException;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.util.Xml;
import android.view.animation.AnimationUtils;
import org.xmlpull.v1.XmlPullParser;
@@ -195,72 +196,95 @@ public class AnimatorInflater {
            anim = new ValueAnimator();
        }
        TypeEvaluator evaluator = null;
        boolean hasFrom = a.hasValue(com.android.internal.R.styleable.Animator_valueFrom);
        boolean hasTo = a.hasValue(com.android.internal.R.styleable.Animator_valueTo);

        switch (valueType) {
        int valueFromIndex = com.android.internal.R.styleable.Animator_valueFrom;
        int valueToIndex = com.android.internal.R.styleable.Animator_valueTo;

        boolean getFloats = (valueType == VALUE_TYPE_FLOAT);

        TypedValue tvFrom = a.peekValue(valueFromIndex);
        boolean hasFrom = (tvFrom != null);
        int fromType = hasFrom ? tvFrom.type : 0;
        TypedValue tvTo = a.peekValue(valueToIndex);
        boolean hasTo = (tvTo != null);
        int toType = hasTo ? tvTo.type : 0;

        if ((hasFrom && (fromType >= TypedValue.TYPE_FIRST_COLOR_INT) &&
                (fromType <= TypedValue.TYPE_LAST_COLOR_INT)) ||
            (hasTo && (toType >= TypedValue.TYPE_FIRST_COLOR_INT) &&
                (toType <= TypedValue.TYPE_LAST_COLOR_INT))) {
            // special case for colors: ignore valueType and get ints
            getFloats = false;
            anim.setEvaluator(new RGBEvaluator());
        }

            case VALUE_TYPE_FLOAT: {
        if (getFloats) {
            float valueFrom;
            float valueTo;
            if (hasFrom) {
                    valueFrom = a.getFloat(com.android.internal.R.styleable.Animator_valueFrom, 0f);
                if (fromType == TypedValue.TYPE_DIMENSION) {
                    valueFrom = a.getDimension(valueFromIndex, 0f);
                } else {
                    valueFrom = a.getFloat(valueFromIndex, 0f);
                }
                if (hasTo) {
                        valueTo = a.getFloat(com.android.internal.R.styleable.Animator_valueTo, 0f);
                    if (toType == TypedValue.TYPE_DIMENSION) {
                        valueTo = a.getDimension(valueToIndex, 0f);
                    } else {
                        valueTo = a.getFloat(valueToIndex, 0f);
                    }
                    anim.setFloatValues(valueFrom, valueTo);
                } else {
                    anim.setFloatValues(valueFrom);
                }
            } else {
                    valueTo = a.getFloat(com.android.internal.R.styleable.Animator_valueTo, 0f);
                    anim.setFloatValues(valueTo);
                if (toType == TypedValue.TYPE_DIMENSION) {
                    valueTo = a.getDimension(valueToIndex, 0f);
                } else {
                    valueTo = a.getFloat(valueToIndex, 0f);
                }
                anim.setFloatValues(valueTo);
            }
            break;

            case VALUE_TYPE_COLOR:
                evaluator = new RGBEvaluator();
                anim.setEvaluator(evaluator);
                // fall through to pick up values
            case VALUE_TYPE_INT: {
        } else {
            int valueFrom;
            int valueTo;
            if (hasFrom) {
                    valueFrom = a.getInteger(com.android.internal.R.styleable.Animator_valueFrom, 0);
                if (fromType == TypedValue.TYPE_DIMENSION) {
                    valueFrom = (int) a.getDimension(valueFromIndex, 0f);
                } else if ((fromType >= TypedValue.TYPE_FIRST_COLOR_INT) &&
                        (fromType <= TypedValue.TYPE_LAST_COLOR_INT)) {
                    valueFrom = a.getColor(valueFromIndex, 0);
                } else {
                    valueFrom = a.getInt(valueFromIndex, 0);
                }
                if (hasTo) {
                        valueTo = a.getInteger(com.android.internal.R.styleable.Animator_valueTo, 0);
                    if (toType == TypedValue.TYPE_DIMENSION) {
                        valueTo = (int) a.getDimension(valueToIndex, 0f);
                    } else if ((toType >= TypedValue.TYPE_FIRST_COLOR_INT) &&
                            (toType <= TypedValue.TYPE_LAST_COLOR_INT)) {
                        valueTo = a.getColor(valueToIndex, 0);
                    } else {
                        valueTo = a.getInt(valueToIndex, 0);
                    }
                    anim.setIntValues(valueFrom, valueTo);
                } else {
                    anim.setIntValues(valueFrom);
                }
            } else {
                    valueTo = a.getInteger(com.android.internal.R.styleable.Animator_valueTo, 0);
                    anim.setIntValues(valueTo);
                }
            }
            break;

            case VALUE_TYPE_CUSTOM: {
                // TODO: How to get an 'Object' value?
                float valueFrom;
                float valueTo;
                if (hasFrom) {
                    valueFrom = a.getFloat(com.android.internal.R.styleable.Animator_valueFrom, 0f);
                if (hasTo) {
                        valueTo = a.getFloat(com.android.internal.R.styleable.Animator_valueTo, 0f);
                        anim.setFloatValues(valueFrom, valueTo);
                    if (toType == TypedValue.TYPE_DIMENSION) {
                        valueTo = (int) a.getDimension(valueToIndex, 0f);
                    } else if ((toType >= TypedValue.TYPE_FIRST_COLOR_INT) &&
                        (toType <= TypedValue.TYPE_LAST_COLOR_INT)) {
                        valueTo = a.getColor(valueToIndex, 0);
                    } else {
                        anim.setFloatValues(valueFrom);
                        valueTo = a.getInt(valueToIndex, 0);
                    }
                } else {
                    valueTo = a.getFloat(com.android.internal.R.styleable.Animator_valueTo, 0f);
                    anim.setFloatValues(valueTo);
                    anim.setIntValues(valueTo);
                }
            }
            break;
        }


        anim.setDuration(duration);
        anim.setStartDelay(startDelay);

+6 −9
Original line number Diff line number Diff line
@@ -3418,21 +3418,18 @@
             greater than 0 or infinite. The default value is restart. -->
        <attr name="repeatMode"/>
        <!-- Value the animation starts from. -->
        <attr name="valueFrom" format="float|integer"/>
        <attr name="valueFrom" format="float|integer|color|dimension"/>
        <!-- Value the animation animates to. -->
        <attr name="valueTo" format="float|integer"/>
        <attr name="valueTo" format="float|integer|color|dimension"/>
        <!-- The type of valueFrom and valueTo. -->
        <attr name="valueType">
            <!-- valueFrom and valueTo are floats. -->
            <!-- valueFrom and valueTo are floats. This is the default value is valueType is
                 unspecified. Note that if either valueFrom or valueTo represent colors
                 (beginning with "#"), then this attribute is ignored and the color values are
                 interpreted as integers. -->
            <enum name="floatType" value="0" />
            <!-- valueFrom and valueTo are integers. -->
            <enum name="intType"   value="1" />
            <!-- valueFrom and valueTo are doubles. -->
            <enum name="doubleType" value="2" />
            <!-- valueFrom and valueTo are colors. -->
            <enum name="colorType" value="3" />
            <!-- valueFrom and valueTo are a custom type. -->
            <enum name="customType" value="4" />
        </attr>
    </declare-styleable>