Create linear Animation - Android Animation

Android examples for Animation:Animation Creation

Description

Create linear Animation

Demo Code


//package com.java2s;

import android.view.View;

import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;

public class Main {
    public static void linearAnimation(final View view, final int deltaX,
            final int deltaY, long duration, final Runnable callback) {
        linearAnimation(view, 0, 0, deltaX, deltaY, duration, callback);
    }//from   w w  w  .ja v a 2 s.  c om

    public static void linearAnimation(final View view, final int startX,
            final int startY, final int endX, final int endY,
            long duration, final Runnable callback) {
        Animation anim = new TranslateAnimation(startX, endX, startY, endY);
        anim.setDuration(duration);
        anim.setInterpolator(new DecelerateInterpolator());
        view.startAnimation(anim);

        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.clearAnimation();
                view.setX(view.getX() + endX);
                view.setY(view.getY() + endY);

                if (callback != null)
                    callback.run();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }
}

Related Tutorials