translate Y in Animation - Android Animation

Android examples for Animation:Translate Animation

Description

translate Y in Animation

Demo Code


//package com.java2s;

import android.os.Build;
import android.view.View;

import android.view.animation.Animation;

import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

public class Main {
    public static void translateY(final View view, float fromY, float toY,
            long duration) {
        if (Build.VERSION.SDK_INT >= 12) {
            if (duration == 0)
                view.setTranslationY(toY);
            else/*from w w w . j  a va2 s  .c o m*/
                view.animate().translationY(toY).setDuration(duration)
                        .start();
        } else {
            TranslateAnimation translate = new TranslateAnimation(0, 0,
                    fromY, toY);
            translate.setDuration(duration);
            translate.setFillEnabled(true);
            translate.setFillBefore(true);
            translate.setFillAfter(true);
            addAnimation(view, translate);
        }
    }

    static void addAnimation(View view, Animation animation) {
        addAnimation(view, animation, false);
    }

    static void addAnimation(View view, Animation animation, boolean first) {
        Animation previousAnimation = view.getAnimation();
        if (previousAnimation == null
                || previousAnimation.getClass() == animation.getClass()) {
            if (animation.getStartTime() == Animation.START_ON_FIRST_FRAME)
                view.startAnimation(animation);
            else
                view.setAnimation(animation);
            return;
        }

        if (!(previousAnimation instanceof AnimationSet)) {
            AnimationSet newSet = new AnimationSet(false);
            newSet.addAnimation(previousAnimation);
            previousAnimation = newSet;
        }

        // Remove old of same type
        //
        AnimationSet set = (AnimationSet) previousAnimation;
        for (int i = 0; i < set.getAnimations().size(); i++) {
            Animation anim = set.getAnimations().get(i);
            if (anim.getClass() == animation.getClass()) {
                set.getAnimations().remove(i);
                break;
            }
        }

        // Add this (first if it is a scale animation ,else at end)
        if (animation instanceof ScaleAnimation || first) {
            set.getAnimations().add(0, animation);
        } else {
            set.getAnimations().add(animation);
        }

        animation.startNow();
        view.setAnimation(set);
    }
}

Related Tutorials