How to Set Animation Using View in Android

Procedure
  1. Start Eclipse IDE.
  2. Create a new project.
  3. Create two Java files, one is AnimationUsingView.java and the second is MyView.java.
  4. Create an activity_animation_using_view.xml file for layout design.
  5. In MyView.java file add an image using BitmapFactory and then in the onDraw function use a canvas with drawLine,drawBitmap, and drawColor.
  6. In the AnimationUsingView.java file extends with Activity and declares a variable of MyView type then add animation_using_view in onCreateOptionsMenu.
The code is given below.
 
MyView.java
  1. package a.v;  
  2. import android.content.Context;  
  3. import android.graphics.Bitmap;  
  4. import android.graphics.BitmapFactory;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.Paint;  
  8. import android.view.View;  
  9. public class MyView extends View {  
  10.  Bitmap ball;  
  11.  float change = 0;  
  12.  public MyView(Context context) {  
  13.   super(context);  
  14.   ball = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);  
  15.  }  
  16.  @Override  
  17.  protected void onDraw(Canvas canvas) {  
  18.   super.onDraw(canvas);  
  19.   Paint pi = new Paint();  
  20.   pi.setColor(Color.YELLOW);  
  21.   pi.setStrokeWidth(45);  
  22.   canvas.drawColor(Color.RED);  
  23.   canvas.drawBitmap(ball, canvas.getWidth() / 2, change, null);  
  24.   canvas.drawLine((canvas.getWidth() / 2) + 200, (canvas.getWidth() / 2) + 20, change, pi);  
  25.   if (change < canvas.getHeight()) {  
  26.    change += 5;  
  27.   } else {  
  28.    change = 0;  
  29.   }  
  30.   invalidate();  
  31.  }  
AnimationUsingView.java
  1. package a.v;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. public class AnimationUsingView extends Activity {  
  6.  MyView a1;  
  7.  @Override  
  8.  protected void onCreate(Bundle savedInstanceState) {  
  9.   super.onCreate(savedInstanceState);  
  10.   a1 = new MyView(getApplicationContext());  
  11.   setContentView(a1);  
  12.  }  
  13.  @Override  
  14.  public boolean onCreateOptionsMenu(Menu menu) {  
  15.   getMenuInflater().inflate(R.menu.animation_using_view, menu);  
  16.   return true;  
  17.  }  
activity_animation_using_view.xml
  1. <RelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.   
  5.       android:layout_width="match_parent"  
  6.       android:layout_height="match_parent"  
  7.       android:paddingBottom="@dimen/activity_vertical_margin"  
  8.       android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.       android:paddingRight="@dimen/activity_horizontal_margin"  
  10.      android:paddingTop="@dimen/activity_vertical_margin"  
  11.      tools:context=".AnimationUsingView" >  
  12.       
  13. </RelativeLayout> 
Output
 
Output
 
Animation using View in Android