Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

Android development – Custom Animation

Posted on 01/07/2011

android
Android developers web site provide you with 2 predefined animation techniques which you can use in your applications – Tween animation and frame animation. They are super easy to implement and work quite nice.
If you are using Android 3.0 or above, you should check out Property Animation technicqe. But what if you need to create your own customized animations and you are using older versions? Don’t worry, that is easy to do as well, here is how you do it.

To create a customized animation you need to follow these 3 steps.
For the explanation, we will create a simple animation that changes the background of a button gradually from black to red.

Step 1 – Extend the Animation class and set the properties
Create a class which extends Android Animation class.
This class will hold the logic of your animation.
See my example:

public class BGColorAnimation extends Animation {
	
	private View view;
	private int currentRedColor;
	
	//The steps to skip between colors
	private static int STEP_SIZE=30;  
	private static int ANIMATION_DURATION=50;

	public BGColorAnimation(View view) {
		this.view=view;
		setDuration(ANIMATION_DURATION);
		setRepeatCount(255/STEP_SIZE);
		setFillAfter(true);
		setInterpolator(new AccelerateInterpolator());
		
		setAnimationListener(new MyAnimationListener());
	}
}
  • As you see, there is not much in this class since my animation is not that complicated.
  • Notice that I have made all the necessary animation parameters initialization from inside the constructor, but you can defiantly initialize them from outside the class.
  • There are 2 important parameters which determines the behavior of the animation:
    1. RepeatCount – the number of steps this animation has.
    2. Duration – the sleep time between 2 steps.
  • On each step, the animation listener will be triggered.

Step 2 – Implement AnimationListener interface
Create a class which implements Animation.AnimationListener.
As mentioned, it is triggered on each animation step.

class MyNumbersAnimationListener implements AnimationListener{
	    	private int index;

	    	@Override
		public void onAnimationEnd(Animation animation) {
			
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			// Change color of the view
			view.setBackgroundColor(
				Color.rgb(currentRedColor+=STEP_SIZE, 0, 0));
		}

		@Override
		public void onAnimationStart(Animation animation) {
			view.setBackgroundColor(Color.BLACK);
			currentRedColor=0;
		}
		
	}
	  

In fact, I see no reason why not doing both steps in the same class:

public class BGColorAnimation extends Animation implements
		Animation.AnimationListener {

	private View view;
	private int currentRedColor;

	// The steps to skip between colors
	private static int STEP_SIZE= 30;
	private static int ANIMATION_DURATION = 50;

	public BGColorAnimation(View view) {
		this.view = view;
		setDuration(ANIMATION_DURATION);
		setRepeatCount(255 / STEP_SIZE);
		setFillAfter(true);
		setInterpolator(new AccelerateInterpolator());

		setAnimationListener(this);
	}

	@Override
	public void onAnimationEnd(Animation animation) {

	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		view.setBackgroundColor(
			Color.rgb(currentRedColor += STEP_SIZE, 0, 0));
	}

	@Override
	public void onAnimationStart(Animation animation) {
		view.setBackgroundColor(Color.BLACK);
		currentRedColor = 0;
	}

}

Step 3 – Start the animation from a view
Animations are triggered from a view.
Example:

Button button = (Button)findViewById(R.id.b_colors);
button.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					button.startAnimation(new BGColorAnimation(button));
				}
			});

zip Download demo project

9 thoughts on “Android development – Custom Animation”

  1. Juhani says:
    05/07/2011 at 22:07

    Hey Avi,
    Great post. Thank you for sharing!

    Reply
  2. taf says:
    11/07/2011 at 23:56

    Hi,

    I don’t think the interpolator is going to be used when you extend the animation class like this. Maybe have a look at overriding the applyTransformation method in the Animation class. I think you’ll be able to lose the Animation listener as well.

    Reply
    1. admin says:
      12/07/2011 at 06:58

      Hi Raf.
      This is a beginners post, that’s why I didnt want to refer to the interpolator quite yet. But thanks a lot for your csuggestion, i’ll be sure to look into this soon.
      Avi

      Reply
      1. joshua says:
        08/09/2011 at 07:51

        Admin ,I need someone to make me an app game or two. ANy suggestions

        Reply
  3. Subhas says:
    01/08/2011 at 11:11

    Excellent posts ! Keep up the good work !

    Reply
  4. Roy says:
    15/08/2011 at 14:29

    Excellent Post

    Reply
  5. click here says:
    29/05/2012 at 07:51

    Although I actually like this post, I think there was an spelling error close towards the finish of your third paragraph.

    Reply
  6. http://4csdiamonds.com/ critique says:
    01/10/2013 at 10:28

    This really answered my problem, thank you!

    Reply
  7. elmira says:
    22/01/2016 at 21:43

    hi, thanks a lot.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
1d96f52e7159fe09c7a3dd2a9816d166-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme