Creates a random animation set for a view, moving upwards, rotating back and forth, and fading. - Android Animation

Android examples for Animation:Rotate Animation

Description

Creates a random animation set for a view, moving upwards, rotating back and forth, and fading.

Demo Code


//package com.java2s;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;

public class Main {
    /**// w  w  w  .  j  a  v  a  2s  .co m
     * Creates a random animation set for a view, moving upwards, rotating back and forth, and
     * fading.
     *
     * @param heart view for animation
     * @param left starting left position of view
     * @param top starting top position of view
     * @return animation for adorable hearts
     */
    public static AnimationSet getRandomHeartAnimation(final View heart,
            int left, int top) {
        final int duration = (int) (Math.random() * 1000) + 1000;
        final int translationHeight = (int) (Math.random() * (top / 6))
                + (top / 4);

        TranslateAnimation translate = new TranslateAnimation(
                Animation.ABSOLUTE, left, Animation.ABSOLUTE, left,
                Animation.ABSOLUTE, top, Animation.ABSOLUTE, top
                        - translationHeight);
        AlphaAnimation alpha = new AlphaAnimation(1f, 0f);

        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(translate);
        animationSet.addAnimation(alpha);
        animationSet.setDuration(duration);
        animationSet
                .setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        heart.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        heart.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        // does nothing
                    }
                });
        return animationSet;
    }
}

Related Tutorials