如何使用处理程序在Android中一个接一个地正确设置堆叠视图动画?

时间:2016-11-28 08:00:56

标签: android android-layout animation

我有一个带有RelativeLayout子项的LinearLayout。每个RelativeLayout都有一个像文件柜一样的背景图像。我试图动画抽屉一个接一个地顺利延迟观看。我尝试的所有东西都会立刻播放所有动画。我将此作为我为每个抽屉设置动画的方法:

    private void dropAndPause(final RelativeLayout drawer){
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_down);
            animation.setStartOffset(750L);
            drawer.startAnimation(animation);
        }
    }, 1200);
}

我也试过这个:

    View[] views = new View[] { ... };

// 100ms delay between Animations
long delayBetweenAnimations = 100l; 

for(int i = 0; i < views.length; i++) {
    final View view = views[i];

    // We calculate the delay for this Animation, each animation starts 100ms 
    // after the previous one
    int delay = i * delayBetweenAnimations;

    view.postDelayed(new Runnable() {
        @Override
        public void run() {
            Animation animation = AnimationUtils.loadAnimation(context, R.anim.your_animation);    
            view.startAnimation(animation);
        }
    }, delay);
}

而不是View[] views,我使用了这个:

    View[] drawers = new View[] {
    drawerOne, drawerTwo, drawerThree, drawerFour, drawerFive
};

再次播放所有动画。如何让每个&#34;抽屉&#34; /视图一次滑入一个?另外,我最初应该将每个视图视为可见性GONE吗?

1 个答案:

答案 0 :(得分:4)

如果要为布局内的视图设置动画,则使用layout animations会更容易。基本上,您需要加载动画,创建LayoutAnimationController,设置延迟,然后启动动画。这是一些示例代码。

Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_down);
animation.setStartOffset(750L);

LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setDelay(0.1F);

viewGroup.setLayoutAnimation(controller);
viewGroup.startLayoutAnimation();

此处的延迟设置为动画持续时间的一小部分。