动画两个布局之间的交叉淡化

时间:2014-08-27 07:38:42

标签: java android android-layout android-animation

我试图实现一个在两个布局之间交叉淡入淡出的动画,并在它们之间进行中途停留。第一个布局是主要布局,第二个布局是带有textView和红色背景的简单线性布局(您的经典"错误!"屏幕,如果您将......)。我的目标是在mainLayout和wrongLayout之间设置一个快速转换动画,让程序在显示wrongLayout时等待一小段时间,然后自动返回mainLayout。事实证明这非常困难,我尝试过使用监视器,Thread.sleep()等,但我得到的是程序在开始动画之前等待,然后执行它而没有任何停留。

我的代码如下:

在主要方法中 -

LinearLayout wrongLayout = (LinearLayout) findViewById(R.id.wrong_layout);
            RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_layout);

            int animationDuration = getResources().getInteger(
                    android.R.integer.config_longAnimTime);

            crossfade(wrongLayout, mainLayout, animationDuration);
            /* This is where I want it to wait for 1 second */
            crossfade(mainLayout, wrongLayout, animationDuration);

和交叉淡化方法 -

private void crossfade(View fadeInLayout, final View fadeOutLayout,
            int animationDuration) {
          // Set the content view to 0% opacity but visible, so that it is visible
        // (but fully transparent) during the animation.
        fadeInLayout.setAlpha(0f);
        fadeInLayout.setVisibility(View.VISIBLE);

        // Animate the content view to 100% opacity, and clear any animation
        // listener set on the view.
        fadeInLayout.animate()
                .alpha(1f)
                .setDuration(animationDuration)
                .setListener(null);

        // Animate the loading view to 0% opacity. After the animation ends,
        // set its visibility to GONE as an optimization step (it won't
        // participate in layout passes, etc.)
        fadeOutLayout.animate()
                .alpha(0f)
                .setDuration(animationDuration)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        fadeOutLayout.setVisibility(View.GONE);
                    }
                });

    } 

非常感谢...

1 个答案:

答案 0 :(得分:0)

好的,所以我发现这篇文章只涉及同样的问题 -

Why the Sleep executes first and than the code above it in android?

我已经更改了相应的执行功能,现在它似乎工作正常,但我仍然感到困惑的是,我不能让代码在特定时间内停在指定位置。哦,好吧......