Example usage for android.animation PropertyValuesHolder setEvaluator

List of usage examples for android.animation PropertyValuesHolder setEvaluator

Introduction

In this page you can find the example usage for android.animation PropertyValuesHolder setEvaluator.

Prototype

public void setEvaluator(TypeEvaluator evaluator) 

Source Link

Document

The TypeEvaluator will be automatically determined based on the type of values supplied to PropertyValuesHolder.

Usage

From source file:android.support.graphics.drawable.AnimatorInflaterCompat.java

private static PropertyValuesHolder loadPvh(Context context, Resources res, Theme theme, XmlPullParser parser,
        String propertyName, int valueType) throws XmlPullParserException, IOException {

    PropertyValuesHolder value = null;
    ArrayList<Keyframe> keyframes = null;

    int type;//from ww  w .ja v a2s . c om
    while ((type = parser.next()) != XmlPullParser.END_TAG && type != XmlPullParser.END_DOCUMENT) {
        String name = parser.getName();
        if (name.equals("keyframe")) {
            if (valueType == VALUE_TYPE_UNDEFINED) {
                valueType = inferValueTypeOfKeyframe(res, theme, Xml.asAttributeSet(parser), parser);
            }
            Keyframe keyframe = loadKeyframe(context, res, theme, Xml.asAttributeSet(parser), valueType,
                    parser);
            if (keyframe != null) {
                if (keyframes == null) {
                    keyframes = new ArrayList<Keyframe>();
                }
                keyframes.add(keyframe);
            }
            parser.next();
        }
    }

    int count;
    if (keyframes != null && (count = keyframes.size()) > 0) {
        // make sure we have keyframes at 0 and 1
        // If we have keyframes with set fractions, add keyframes at start/end
        // appropriately. If start/end have no set fractions:
        // if there's only one keyframe, set its fraction to 1 and add one at 0
        // if >1 keyframe, set the last fraction to 1, the first fraction to 0
        Keyframe firstKeyframe = keyframes.get(0);
        Keyframe lastKeyframe = keyframes.get(count - 1);
        float endFraction = lastKeyframe.getFraction();
        if (endFraction < 1) {
            if (endFraction < 0) {
                lastKeyframe.setFraction(1);
            } else {
                keyframes.add(keyframes.size(), createNewKeyframe(lastKeyframe, 1));
                ++count;
            }
        }
        float startFraction = firstKeyframe.getFraction();
        if (startFraction != 0) {
            if (startFraction < 0) {
                firstKeyframe.setFraction(0);
            } else {
                keyframes.add(0, createNewKeyframe(firstKeyframe, 0));
                ++count;
            }
        }
        Keyframe[] keyframeArray = new Keyframe[count];
        keyframes.toArray(keyframeArray);
        for (int i = 0; i < count; ++i) {
            Keyframe keyframe = keyframeArray[i];
            if (keyframe.getFraction() < 0) {
                if (i == 0) {
                    keyframe.setFraction(0);
                } else if (i == count - 1) {
                    keyframe.setFraction(1);
                } else {
                    // figure out the start/end parameters of the current gap
                    // in fractions and distribute the gap among those keyframes
                    int startIndex = i;
                    int endIndex = i;
                    for (int j = startIndex + 1; j < count - 1; ++j) {
                        if (keyframeArray[j].getFraction() >= 0) {
                            break;
                        }
                        endIndex = j;
                    }
                    float gap = keyframeArray[endIndex + 1].getFraction()
                            - keyframeArray[startIndex - 1].getFraction();
                    distributeKeyframes(keyframeArray, gap, startIndex, endIndex);
                }
            }
        }
        value = PropertyValuesHolder.ofKeyframe(propertyName, keyframeArray);
        if (valueType == VALUE_TYPE_COLOR) {
            value.setEvaluator(ArgbEvaluator.getInstance());
        }
    }

    return value;
}

From source file:android.support.graphics.drawable.AnimatorInflaterCompat.java

private static PropertyValuesHolder getPVH(TypedArray styledAttributes, int valueType, int valueFromId,
        int valueToId, String propertyName) {

    TypedValue tvFrom = styledAttributes.peekValue(valueFromId);
    boolean hasFrom = (tvFrom != null);
    int fromType = hasFrom ? tvFrom.type : 0;
    TypedValue tvTo = styledAttributes.peekValue(valueToId);
    boolean hasTo = (tvTo != null);
    int toType = hasTo ? tvTo.type : 0;

    if (valueType == VALUE_TYPE_UNDEFINED) {
        // Check whether it's color type. If not, fall back to default type (i.e. float type)
        if ((hasFrom && isColorType(fromType)) || (hasTo && isColorType(toType))) {
            valueType = VALUE_TYPE_COLOR;
        } else {//ww  w  .java2 s .co  m
            valueType = VALUE_TYPE_FLOAT;
        }
    }

    boolean getFloats = (valueType == VALUE_TYPE_FLOAT);

    PropertyValuesHolder returnValue = null;

    if (valueType == VALUE_TYPE_PATH) {
        String fromString = styledAttributes.getString(valueFromId);
        String toString = styledAttributes.getString(valueToId);

        PathParser.PathDataNode[] nodesFrom = PathParser.createNodesFromPathData(fromString);
        PathParser.PathDataNode[] nodesTo = PathParser.createNodesFromPathData(toString);
        if (nodesFrom != null || nodesTo != null) {
            if (nodesFrom != null) {
                TypeEvaluator evaluator = new PathDataEvaluator();
                if (nodesTo != null) {
                    if (!PathParser.canMorph(nodesFrom, nodesTo)) {
                        throw new InflateException(" Can't morph from " + fromString + " to " + toString);
                    }
                    returnValue = PropertyValuesHolder.ofObject(propertyName, evaluator, nodesFrom, nodesTo);
                } else {
                    returnValue = PropertyValuesHolder.ofObject(propertyName, evaluator, (Object) nodesFrom);
                }
            } else if (nodesTo != null) {
                TypeEvaluator evaluator = new PathDataEvaluator();
                returnValue = PropertyValuesHolder.ofObject(propertyName, evaluator, (Object) nodesTo);
            }
        }
    } else {
        TypeEvaluator evaluator = null;
        // Integer and float value types are handled here.
        if (valueType == VALUE_TYPE_COLOR) {
            // special case for colors: ignore valueType and get ints
            evaluator = ArgbEvaluator.getInstance();
        }
        if (getFloats) {
            float valueFrom;
            float valueTo;
            if (hasFrom) {
                if (fromType == TypedValue.TYPE_DIMENSION) {
                    valueFrom = styledAttributes.getDimension(valueFromId, 0f);
                } else {
                    valueFrom = styledAttributes.getFloat(valueFromId, 0f);
                }
                if (hasTo) {
                    if (toType == TypedValue.TYPE_DIMENSION) {
                        valueTo = styledAttributes.getDimension(valueToId, 0f);
                    } else {
                        valueTo = styledAttributes.getFloat(valueToId, 0f);
                    }
                    returnValue = PropertyValuesHolder.ofFloat(propertyName, valueFrom, valueTo);
                } else {
                    returnValue = PropertyValuesHolder.ofFloat(propertyName, valueFrom);
                }
            } else {
                if (toType == TypedValue.TYPE_DIMENSION) {
                    valueTo = styledAttributes.getDimension(valueToId, 0f);
                } else {
                    valueTo = styledAttributes.getFloat(valueToId, 0f);
                }
                returnValue = PropertyValuesHolder.ofFloat(propertyName, valueTo);
            }
        } else {
            int valueFrom;
            int valueTo;
            if (hasFrom) {
                if (fromType == TypedValue.TYPE_DIMENSION) {
                    valueFrom = (int) styledAttributes.getDimension(valueFromId, 0f);
                } else if (isColorType(fromType)) {
                    valueFrom = styledAttributes.getColor(valueFromId, 0);
                } else {
                    valueFrom = styledAttributes.getInt(valueFromId, 0);
                }
                if (hasTo) {
                    if (toType == TypedValue.TYPE_DIMENSION) {
                        valueTo = (int) styledAttributes.getDimension(valueToId, 0f);
                    } else if (isColorType(toType)) {
                        valueTo = styledAttributes.getColor(valueToId, 0);
                    } else {
                        valueTo = styledAttributes.getInt(valueToId, 0);
                    }
                    returnValue = PropertyValuesHolder.ofInt(propertyName, valueFrom, valueTo);
                } else {
                    returnValue = PropertyValuesHolder.ofInt(propertyName, valueFrom);
                }
            } else {
                if (hasTo) {
                    if (toType == TypedValue.TYPE_DIMENSION) {
                        valueTo = (int) styledAttributes.getDimension(valueToId, 0f);
                    } else if (isColorType(toType)) {
                        valueTo = styledAttributes.getColor(valueToId, 0);
                    } else {
                        valueTo = styledAttributes.getInt(valueToId, 0);
                    }
                    returnValue = PropertyValuesHolder.ofInt(propertyName, valueTo);
                }
            }
        }
        if (returnValue != null && evaluator != null) {
            returnValue.setEvaluator(evaluator);
        }
    }

    return returnValue;
}