Wednesday, July 06, 2011

Android development – Custom Animation

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.


Read more: Mobile Zone
QR: android-development-%E2%80%93-custom

Posted via email from Jasper-Net