1

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().setIcon(icon)

where icon is a Drawable produced by a library that converts a custom XML view into a bitmap. This XML view is a RelativeLayout with a background image and a TextView on top.

Today, when I have to update the TextView I simply re-generate the icon and call setIcon again. Instead, I would like to get a hold of my TextView and apply some animation effect on it, like fade-out and then fade-in after updating it (maybe never having to call setIcon, just re-use the same one).

Not sure how to go about this. Can someone recommend an approach?

EDIT: trying this approach:

In MyActivity:

Drawable myDrawable = new MyDrawable();
supportActionBar.setIcon(myDrawable);

and:

public class MyDrawable extends Drawable {
    private Paint paint;
    private RectF rect;

    public MyDrawable() {
        this.paint = new Paint();
        this.rect = new RectF();
    }

    @Override
    public void draw(Canvas canvas) {
        paint.setARGB(255, 0, 255, 0);
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.FILL);

        rect.right = 20f;
        rect.bottom = 20f;

        canvas.drawRoundRect(rect, 0.5f, 0.5f, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        paint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.OPAQUE;
    }
}

Nothing shows up. I verified that onDraw gets called. Something suspicious to me is that canvas has both height & width set to 1.

siger
  • 3,112
  • 1
  • 27
  • 42

1 Answers1

3

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):

private long animationTime;
public void doAnimation(){
    animationTime = System.currentTimeMilis() + 3000; // 3 seconds
    invalidateSelf();
}

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

    // at the end
    if(animationTime > System.currentTimeMilis())
         invalidateSelf();
}
Budius
  • 39,391
  • 16
  • 102
  • 144
  • Thanks. I am trying this approach but can't get anything to draw so far. My custom drawable's `onDraw` method receives a canvas of height and width '1', on which I call drawRoundRect for a (0,0)-(20,20) RectF. Any pointer? – siger Jun 23 '14 at 22:41
  • override `getIntrinsicWidth` and `getIntrinsicHeight` and return some values. Also override `setBounds` and initialise any sizing you need there. – Budius Jun 23 '14 at 22:51
  • Thanks, I have a basic bitmap + text working now. I am providing arbitrary intrinsic & bound values, would like to know more. Also for animating, I wonder if I should call invalidateSelf from onDraw and maintain a state machine. Any pointers would be helpful if you have. – siger Jun 24 '14 at 00:30
  • You can call `invalidateSelf()` from anywhere that suits you, including `onDraw` to keep the animation flowing. How you organize is very much up to you, but I just put a very simple way on the answer (check the edit). You can also use the `onLevelChange` callback that is built-in on Drawble to trigger different stuff (example, you set the `drawable.setLevel(1000)` to animate 1 second) – Budius Jun 24 '14 at 08:31
  • regarding the arbitrary intrinsic sizes: you comment you're using it for the actionbar icon, so I reckon the size you should return are the actionbar icon size. http://stackoverflow.com/questions/7165830/what-is-the-size-of-actionbar-in-pixels – Budius Jun 24 '14 at 19:21