start ValueAnimator - Android android.animation

Android examples for android.animation:ValueAnimator

Description

start ValueAnimator

Demo Code

/*//ww w.ja  v a 2  s  .c o m
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.util.Property;
import android.view.View;
import android.view.animation.Interpolator;
import android.widget.ImageView;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main{
    private static Method sAnimateValue;
    private static boolean sTryAnimateValue = true;
    public static void start(ValueAnimator... animators) {
        for (ValueAnimator animator : animators) {
            final float fraction = animator.getAnimatedFraction();
            if (fraction < 1.0f) {
                animator.start();
                setAnimatedFraction(animator, fraction);
            }
        }
    }
    public static void setAnimatedFraction(ValueAnimator animator,
            float fraction) {
        if (sTryAnimateValue) {
            // try to set the animated fraction directly so that it isn't affected by the
            // internal animator scale or time (b/17938711)
            try {
                if (sAnimateValue == null) {
                    sAnimateValue = ValueAnimator.class.getDeclaredMethod(
                            "animateValue", float.class);
                    sAnimateValue.setAccessible(true);
                }

                sAnimateValue.invoke(animator, fraction);
                return;
            } catch (NoSuchMethodException | InvocationTargetException
                    | IllegalAccessException e) {
                // something went wrong, don't try that again
                sTryAnimateValue = false;
            }
        }

        // if that doesn't work then just fall back to setting the current play time
        animator.setCurrentPlayTime(Math.round(fraction
                * animator.getDuration()));
    }
}

Related Tutorials