Android How do I add some delay to multiple animations?

I want every animation, starting with the code below, to start with a delay, like a sequence. So, I have this code:

public void setAnimation(){ View view; String animation = prefs.getString("animations", "Scale"); String interpolator = prefs.getString("interpolators", "Bounce"); Animation animate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale_in); for(int i=0; i<gridView.getChildCount(); i++){ view = gridView.getChildAt(i); view.startAnimation(animate); } } 

since there is a for loop, all child animations will run instantly. I already tried:

 Thread.sleep.... Handler... animate.setStartTime... animate.setStartOffset... 

but all child animations run instantly.

I tried this method inside the loop and the animation does not start:

 animate.setAnimationListener(new AnimationListener(){ public void onAnimationEnd(Animation arg0) { view.startAnimation(animate); } public void onAnimationRepeat(Animation arg0) { } public void onAnimationStart(Animation arg0) { } }); 

Thanx in advance.

+4
source share
2 answers

The solution is to create a GridLayoutAnimationController or LayoutAnimationController.

0
source

I used the LayoutAnimationController to show elements in a LinearLayout with an animation effect one by one using the following code.

  Animation fadeIn = AnimationUtils.loadAnimation(context, R.anim.anim_fade_in); //lnrContactContainer is LinearLayout. AnimationSet set = new AnimationSet(true); set.addAnimation(fadeIn); set.setDuration(500); controller = new LayoutAnimationController(set, 1f); lnrContactContainer.setLayoutAnimation(controller); lnrContactContainer.setVisibility(View.VISIBLE); 

But the same approach does not work when I use it to display fadeout animations, hiding LinearLayout lnrContactContainer.setVisibility(View.GONE) ;

0
source

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


All Articles