Skip to content Skip to sidebar Skip to footer

Animating Actionbar's Icon

I have an ActionBar icon (the main one on the left, not an action item) that I would like to animate. I am setting my ActionBar's icon in my Activity like this: getSupportActionBar

Solution 1:

A proper approach for it would be to forget the XML layout and create a custom Drawable.

An instance of this custom drawable will be set to the icon on the ActionBar and call invalidateSelf() whenever necessary to redraw (due to animation, for example).

The drawable can hold reference to other drawables (e.g. BitmapDrawable to have something from the /res/ folder or a Color or Gradient drawable for a background shade) and call (for example) bgDraw.draw(canvas) during the onDraw callback.

It can also draw stuff directly on the canvas that is given to it during onDraw callback. With the canvas you can draw circle, lines, areas, path and text directly on it.

edit:

very simple animation example (didn't check the code, likely typos):

privatelong animationTime;
publicvoiddoAnimation(){
    animationTime = System.currentTimeMilis() + 3000; // 3 seconds
    invalidateSelf();
}

publicvoidonDraw(Canvas canvas){
    // do your drawing.// You can use difference between// currentTimeMilis and animationTime for status/position
   ...

    // at the endif(animationTime > System.currentTimeMilis())
         invalidateSelf();
}

Post a Comment for "Animating Actionbar's Icon"