Example usage for android.animation Keyframe getFraction

List of usage examples for android.animation Keyframe getFraction

Introduction

In this page you can find the example usage for android.animation Keyframe getFraction.

Prototype

public float getFraction() 

Source Link

Document

Gets the time for this keyframe, as a fraction of the overall animation duration.

Usage

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

private static void dumpKeyframes(Object[] keyframes, String header) {
    if (keyframes == null || keyframes.length == 0) {
        return;//  w  ww. j ava 2s.c  o m
    }
    Log.d(TAG, header);
    int count = keyframes.length;
    for (int i = 0; i < count; ++i) {
        Keyframe keyframe = (Keyframe) keyframes[i];
        Log.d(TAG,
                "Keyframe " + i + ": fraction " + (keyframe.getFraction() < 0 ? "null" : keyframe.getFraction())
                        + ", " + ", value : " + ((keyframe.hasValue()) ? keyframe.getValue() : "null"));
    }
}

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;/*from   ww  w.java2  s.  c  o m*/
    ArrayList<Keyframe> keyframes = null;

    int type;
    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;
}