Introduction
Right from the start, the animation is something that has interested me. In this article, we will create a simple animation.
Animation can add subtle visual clues that notify the users about what is going on in your app and improve their mental model of your app's interface. Animation can be of many types:
- Crossfading two views
- Using view pager for screen slides
- Displaying card flip animations
- Zooming a view
- Animating layout changes
In this article, we will look at "Crossfading two views". In this type of animation, an object will appear and then will fade away according to the fading time specified by you. This type of animation can be used to occupy the user while something is being loaded.
Step 1
I am using images for this purpose. You can use any other element (like a Progress Bar, text etcetera).
Copy all the images you want to use to the clipboard and paste them on "drawable".
I am using 3 images, namely scene1, scene2, and scene3.
Step 2
Open the layout file, in other words, "activity_main" and make the following changes in it:
- <ImageView
- android:layout_width="700dp"
- android:layout_height="600dp"
- android:id="@+id/im"
- />
The layout looks like:

Step 3
Open "MainActivity.java" and add the following code to it:
- package com.animation2;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.app.Activity;
- import android.os.Handler;
- import android.view.Menu;
- import android.view.View;
- import android.view.animation.*;
- import android.widget.ImageView;
- public class MainActivity extends Activity {
- ImageView im;
- Animation fadeIn;
- Animation fadeOut;
- int fadeOutTime=5000;
- int timeBetween=3000;
- int images[]={R.drawable.scene1,R.drawable.scene2,R.drawable.scene3};
- static int index=0;
- private AnimationSet animation;
- private Handler mHandler;
- final Context context=this;
- private Runnable mCountUpdater = new Runnable() {
- private int mCount = 0;
- @Override
- public void run() {
- if(mCount>2)
- return;
- else
- {
- func();
- mCount++;
- mHandler.postDelayed(this, 9000);
- }
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mHandler = new Handler();
- mHandler.post(mCountUpdater);
- }
- public void func()
- {
- if(index<=2)
- {
- im=(ImageView)findViewById(R.id.im);
- im.setImageResource(images[index]);
- fadeIn = new AlphaAnimation(0.00f, 1);
- fadeIn.setInterpolator(new LinearInterpolator()); // add this
- fadeIn.setDuration(500);
- fadeOut = new AlphaAnimation(1, 0f);
- fadeOut.setInterpolator(new AccelerateInterpolator()); // and this
- fadeOut.setStartOffset(timeBetween);
- fadeOut.setDuration(fadeOutTime);
- animation = new AnimationSet(false); // change to false
- animation.addAnimation(fadeIn);
- animation.addAnimation(fadeOut);
- animation.setRepeatCount(1);
- animation.setFillAfter(true);
- im.setAnimation(animation);
- index=index+1;
- animation.setAnimationListener(new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {
- }
- @Override
- public void onAnimationEnd(Animation animation) {
- if(index==3)
- {
- Intent i=new Intent(context,Second.class);
- startActivity(i);
- }
- }
- @Override
- public void onAnimationRepeat(Animation animation) {
- }
- });
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }




Join the conversation! Your thoughts help the community grow.