Android How can i add some delay on multiple animations?

放肆的年华 提交于 2019-12-22 08:46:23

问题


What i want is that each animation, from 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);
        }
    }

because there is a for loop, all child animations will start instantly. I already tried with:

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

but all child animations starts instantly.

I tried this method inside of loop, and the animations won't 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.


回答1:


The solution is to create a GridLayoutAnimationController or LayoutAnimationController.




回答2:


I used LayoutAnimationController to show elements in LinearLayout with animation effect one by one using 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 same approach does not work when I use it to show fadeout animation while hiding LinearLayout lnrContactContainer.setVisibility(View.GONE);



来源:https://stackoverflow.com/questions/9341447/android-how-can-i-add-some-delay-on-multiple-animations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!