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:
- RepeatCount – the number of steps this animation has.
- 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)); } });
Hey Avi,
Great post. Thank you for sharing!
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.
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
Admin ,I need someone to make me an app game or two. ANy suggestions
Excellent posts ! Keep up the good work !
Excellent Post
Although I actually like this post, I think there was an spelling error close towards the finish of your third paragraph.
This really answered my problem, thank you!
hi, thanks a lot.