Android Animation

Android Animation first step
Android prepares animation features under android.view.animation

Animation

Animation class is basic class for Animation
This supports old Android version. You can use any versions

  • Animation
  • AlphaAnimation
  • RotateAnimation
  • ScaleAnimation
  • TranslateAnimation
  • AnimationSet

The target of this class is View class. View class is basic UI parts of Android.

Basic Sample

TextView textView = (TextView)findViewById(R.id.textview);
AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.1f); 
alpha.setDuration(3000);
alpha.setFillAfter(true); // Keep state	
textView.startAnimation(alpha);	

setFillAfter
Basically, after animation, View came back original state.
If you want to keep animation operation, setFillAfter true.

RotateAnimation

RotateAnimation ranim = new RotateAnimation(0.0f, 180f);
ranim.setDuration(3000);
ranim.setInterpolator(new LinearInterpolator());
textView.startAnimation(ranim);

Add one more animation

We can detect animation start, repeat, end using listener

AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.1f); 
alpha.setDuration(3000);
alpha.setFillAfter(true); // Keep state
alpha.setAnimationListener(new AnimationListener() {
			
    @Override
    public void onAnimationStart(Animation anim) {
    }
			
    @Override
    public void onAnimationRepeat(Animation anim) {
    }
			
    @Override
    public void onAnimationEnd(Animation anim) {
        // One more
    }
});
textView.startAnimation(alpha);

We can add additional animation in onAnimationEnd.

Combination

We can combine multiple animation at the same time.
AnimationSet is a container of animations.

AnimationSet animset = new AnimationSet(true);  // sharedInterpolar
AlphaAnimation alpha = new AlphaAnimation(0.9f, 0.2f);
RotateAnimation rotate = new RotateAnimation(0, 360, 50, 25);
ScaleAnimation scale = new ScaleAnimation(0.1f, 1, 0.1f, 1);
TranslateAnimation translate = new TranslateAnimation(50, 0, 200, 0);
	    
animset.addAnimation(alpha); 
animset.addAnimation(rotate);
animset.addAnimation(scale);
animset.addAnimation(translate);
animset.setDuration(5000);
textView.startAnimation(animset);

Interpolator

Interpolator is animation process, how it goes.

animation.setInterpolator(new LinearInterpolator());

This animation goes linear(how it changes).
Interpolator list is following

  • AccelerateDecelerateInterpolator
  • AccelerateInterpolator
  • AnticipateInterpolator
  • AnticipateOvershootInterpolator
  • BounceInterpolator
  • CycleInterpolator
  • DecelerateInterpolator
  • LinearInterpolator
  • OvershootInterpolator

To set them, use setInterpolator.

Cancel

We can cancel animation while doing

animation.cancel();

Ref

@IT