pjz pjz - 2 months ago 7
Android Question

How to animate text over another View in Android?

I'm trying to animate some text 'zooming out' on top of another view. My code looks something like:

class BoardView extends View {

private TextView animText;

...

private void animText(String text, int color, int xBlocks, int yBlocks) {
animText.setText(text);
animText.setTextColor(color);
animText.setVisibility(View.VISIBLE);
final int x = BOARD_X_OFFSET + xBlocks * xBlockSize;
final int y = BOARD_Y_OFFSET + yBlocks * yBlockSize;
final float SCALE_FROM = (float) 0.25;
final float SCALE_TO = (float) 5.0;
ScaleAnimation anim = new ScaleAnimation(SCALE_FROM, SCALE_TO, SCALE_FROM, SCALE_TO, x, y);
anim.setDuration(500);
animText.setAnimation(anim);
this.setAnimation(null);
startAnimation(anim);
}

}


with animText being invoked in the
onDraw()
routine of the
BoardView
. What I'm seeing, however, is the board zooming out, not the text, despite the above calls to
setAnimation()
.

I've looked in the main android docs and at one other example. Even pointers in the right direction would be helpful.

pjz pjz
Answer

Well, I'm glad I'm not the only one who had a difficult time finding the very subtle bug in the above. The bug is:

startAnimation(anim);

which isn't too obvious until you expand it into:

this.startAnimation(anim);

at which point it's clear why the outer view (ie. 'this') is animating as well as the text.