Saturday, December 10, 2011

A Basic Android Animation Example

In this Post we shall develop a very simple Android Animation Example.
The classes involved are:-

 For an explanation of the Graphics classes , please refer the previous post at:-
http://champakonline.blogspot.com/2011/12/basic-android-graphics-example.html
The classes you would find there are View,Canvas,Paint,Path,Bitmap, etc.
 Drawing was done by putting the code in the onDraw method of the View class.
You might want a quick review of that post before continuing.


Basic classes for Animation
1) android.view.animation.Animation
This class represents an Android Animation. There are many sub classes of this animation namely:-
    a) android.view.animation.ScaleAnimation
    b) android.view.animationAlphaAnimation
    c) android.view.animation.RotateAnimation
    d) android.view.animation.TranslateAnimation;
In this example we implement an animation using AlphaAnimation and RotateAnimation.

Main functionality of the Animation class
The Animation class provides methods for  setting the RepeatMode , RepeatCount, Interpolator, setDuration.
RepeatMode: This is the RepeatMode of the Animation. Possible values are Animation.REVERSE,Animation.RESTART.
RepeatCount: Possible values are Animation.INFINITE,Animation.ABSOLUTE.
setDuration(); The value gives the duration of one round of the Animation.

setInterpolator

This method is used to set an Interpolator. We have used an android.view.animation.AccelerateDecelerateInterpolator.
other interpolators are :-
 android.view.animation.AccelerateInterpolator,
android.view.animation.AnticipateOvershootInterpolator,
android.view.animation.BounceInterpolator,
android.view.animation.DecelerateInterpolator,
android.view.animation.AnticipateInterpolator,
android.view.animation.CycleInterpolator,
android.view.animation.LinearInterpolator,
android.view.animation.OvershootInterpolator .


We have used the android.view.animation.AccelerateDecelerateInterpolator.
The interpolator decides the progress and restart of the Animation.




 The Code:-
 HypatiaAnimationView.java 

package hypatia.animation.basic;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;


public class HypatiaAnimationView extends View implements AnimationListener {
String themessage="Reserve your right to think, for even to think wrongly is better than not to think at all.";
    public HypatiaAnimationView(Context context) {
        super(context);
        paint = new Paint();
        paint.setColor(HypatiaBasicAnimationActivity.colors[HypatiaBasicAnimationActivity.colorno]);
        HypatiaBasicAnimationActivity.colorno=(HypatiaBasicAnimationActivity.colorno + 1) % HypatiaBasicAnimationActivity.colors.length;
       
        // TODO Auto-generated constructor stub
    }
    Paint paint;//The paint object for the drawing
    @Override
public void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
            if (hypatiaanimationset == null) {//If the animation set is null, create it
                //***********************Create the Animation Set****************************
                {
       
                hypatiaanimationset=new AnimationSet(true);
                animation = new AlphaAnimation(1F, 0F);
                animation.setRepeatMode(Animation.REVERSE);
                animation.setRepeatCount(Animation.INFINITE);
              
                animation.setDuration(5000);
                animation.setAnimationListener(this);
                hypatiaanimationset.addAnimation(animation);
                animation = new RotateAnimation(0, 360,canvas.getWidth()/2,canvas.getHeight()/2);
                animation.setRepeatMode(Animation.REVERSE);
                animation.setRepeatCount(Animation.INFINITE);
                animation.setDuration(1000);
                hypatiaanimationset.addAnimation(animation);
               
                hypatiaanimationset.setInterpolator(new android.view.animation.AccelerateDecelerateInterpolator());
                startAnimation(hypatiaanimationset);
                }
                //***************************************************************************
           
        }

        Path circle = new Path();

        int cx = canvas.getWidth() / 2;
        int cy = canvas.getHeight() / 2;
        int r = Math.min(cx, cy);
        paint.setTextSize(48);
        if(HypatiaBasicAnimationActivity.colorno % 2==0)
       circle.addCircle(cx, cy, r-5, Direction.CW);
        else
        circle.addCircle(cx, cy, r-35, Direction.CCW);
        canvas.drawTextOnPath(themessage, circle, 0, 30, paint);
        Bitmap hypatiaportrait = BitmapFactory.decodeResource(getResources(),R.drawable.hypatia);
        canvas.drawBitmap(hypatiaportrait,cx-hypatiaportrait.getWidth()/2,cy-hypatiaportrait.getHeight()/2,null);

    }
   
   
   
    private Animation animation;
private AnimationSet hypatiaanimationset;
 
    @Override
    public void onAnimationEnd(Animation animation) {
        // TODO Auto-generated method stub
       
    }
    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub
       
        paint.setColor(HypatiaBasicAnimationActivity.colors[HypatiaBasicAnimationActivity.colorno]);
        HypatiaBasicAnimationActivity.colorno=(HypatiaBasicAnimationActivity.colorno + 1) % HypatiaBasicAnimationActivity.colors.length;
        HypatiaBasicAnimationActivity.v.invalidate();
    }
    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub
       
    }
}
 

HypatiaBasicAnimationActivity.java

package hypatia.animation.basic;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

public class HypatiaBasicAnimationActivity extends Activity {
    /** Called when the activity is first created. */
static    HypatiaAnimationView v;
static int[] colors={Color.WHITE,Color.YELLOW,Color.MAGENTA,Color.GREEN,Color.RED};
static int colorno=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //This is the first method to be executed
       
        super.onCreate(savedInstanceState);
         v=new HypatiaAnimationView(this);//Create the view and store it in a static variable for further use
        setContentView(v);//Make this view the current view. This means that whenever the view is invalidated it's onDraw method will be called
    }
  
   
   
}


In the next Post of the series we shall create an Animation using an XML File.

No comments:

Post a Comment