Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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

import android.view.animation.Animation;

import android.view.animation.AnimationSet;

import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;

public class Main {
    public static void rotate(final View view, float fromDegrees, float toDegrees, long duration) {
        if (Build.VERSION.SDK_INT >= 12) {
            if (duration == 0)
                view.setRotation(toDegrees);
            else
                view.animate().rotation(toDegrees).setDuration(duration).start();
        } else {
            RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            rotate.setDuration(duration);
            rotate.setFillEnabled(true);
            rotate.setFillBefore(true);
            rotate.setFillAfter(true);
            addAnimation(view, rotate);
        }
    }

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

    public 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);
    }
}