How to animate text over another view in Android?

I am trying to animate some kind of “scaling” text on top of another view. My code looks something like this:

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);
}

}

calling animText in a routine onDraw() BoardView. However, I see that the board is downscaling, not text, despite the calls for above setAnimation().

I looked through the basic android docs and another example . Even pointers in the right direction would be helpful.

+3
source share
1 answer

Well, I'm glad that I am not the only one who had a difficult time to find a very subtle mistake in the above. Mistake:

startAnimation(anim);

, :

this.startAnimation(anim);

, (.. 'this') , .

+4

Source: https://habr.com/ru/post/1706750/


All Articles