Android button animations synchronize with timing

2019-05-24 13:25发布

问题:

I need an advice how to create some animations I want to add in my buttons. Actually I have the animation code, the thing which I need is how to set properly the timing of each one. Here is what I tried already :

    fest.setVisibility(View.INVISIBLE);
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            fest.setVisibility(View.VISIBLE);
            fest.startAnimation(anim);
            handler.removeCallbacks(this);
        }
    }, 500);

This is the things which I did for 7 buttons. First I set the visibility to invisible because I want to achieve the effect that they are appearing after 5 miliseconds after onCreate and for every next button I am increasing the delay time with 5 miliseconds so every of them to appear after the previos one. But the problem in this code is that when the next handler starts for the second button for example, the previos button is getting invisible for a part of the seconds and shows again(I hope someone understand what I mean).

Any suggestions for a bette implementation of something like that?

Thanks in advance!

回答1:

So here is the thing which fixed that problem. I used this for every button and it's working as I want :

    final Handler festHandler = new Handler();
    festHandler.postDelayed(new Runnable() {

        @Override
        public void run() {
             Animation anim = AnimationUtils.loadAnimation(Menu.this, R.anim.fadein);
             fest.setVisibility(View.VISIBLE);
             fest.startAnimation(anim);
             festHandler.removeCallbacks(this);
        }
    }, 400);