android视图动画和没有动画的行为不同

时间:2014-01-30 12:01:50

标签: android animation view

当我使用案例1时,它按预期工作,但使用animate(case2)视图永远不会变得可见!

案例1>

viewstub.setVisibility(View.GONE);
mContentLoaded = false;
showContentOrLoadingIndicator(mContentLoaded);  
此视图中的

将再次变为隐形和可见!

案例2>

viewstub.setVisibility(View.GONE); 

viewstub.animate().alpha(0f).setDuration(mShortAnimationDuration)
            .setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    viewstub.setVisibility(View.GONE);
                }
            });
此视图中的

将变为不可见,永远不会再次显示!任何解决方案或原因?

private void showContentOrLoadingIndicator(boolean contentLoaded) {
        // Decide which view to hide and which to show.
        final View showView = contentLoaded ? listView : mLoadingView;
        final View hideView = contentLoaded ? mLoadingView : listView;

        // Set the "show" view to 0% opacity but visible, so that it is visible
        // (but fully transparent) during the animation.
        showView.setAlpha(0f);
        showView.setVisibility(View.VISIBLE);

        // Animate the "show" view to 100% opacity, and clear any animation
        // listener set on
        // the view. Remember that listeners are not limited to the specific
        // animation
        // describes in the chained method calls. Listeners are set on the
        // ViewPropertyAnimator object for the view, which persists across
        // several
        // animations.
        showView.animate().alpha(1f).setDuration(mShortAnimationDuration)
                .setListener(null);

        // Animate the "hide" 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.)
        hideView.animate().alpha(0f).setDuration(mShortAnimationDuration)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        hideView.setVisibility(View.GONE);
                    }
                });
    }

2 个答案:

答案 0 :(得分:1)

我并不完全明白确切的理由,而且我们并没有深入研究过这些文档,但我认为我可以指出你的方向正确。

似乎问题来自代码的这一部分:

showView.animate().alpha(1f).setDuration(mShortAnimationDuration)
            .setListener(null);

好像动画可以运行并完成,然后才将听众设置为null(这与the auto-start property相矛盾。所以如果您的列表设置为AnimatorListenerAdapter它,当调用showContentOrLoadingIndicator(true);然后调用

@Override
public void onAnimationEnd(Animator animation) {
    hideView.setVisibility(View.GONE);
}

方法将在侦听器设置为null之前隐藏您的列表。 我通过在GONE

中记录设置为onAnimationEnd的视图找到了此信息

因此,解决方案是在方法链的早期将侦听器设置为null:

showView.animate()
    .setListener(null)
    .alpha(1f)
    .setDuration(mShortAnimationDuration)

如果有人可以提供有关如何发生这种情况的详细解释,我有兴趣接受进一步的教育。

希望这有帮助

答案 1 :(得分:0)

View.Gonesource)将视图设置为从UI消失。它仍然存在,但在其上显示任何其他UI元素。消失的视图不会占用任何空间。

要再次显示,您应该将可见性设置回View.Visiblesource)。