如何在android中动画后刷新布局?

时间:2014-08-06 12:36:41

标签: android android-layout layout

我有一个移动特定用户操作的布局。布局稍微向上移动,但按钮可以在动画之前点击它们的位置。如何在onAnimationEnd上更新布局的子项?

Here I could not find the answer, but I understood the problems is in how the Android Layout system is build.

我试过了:

popupLayout.requestLayout();

popupLayout.invalidate();

都没有奏效。关于如何更新包含按钮的任何其他想法?


更新

以下是我制作动画的方法:

 Animation slide = null;
    slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
            0.0f, Animation.RELATIVE_TO_SELF, -0.3f);

    slide.setDuration(300);
    slide.setFillAfter(true);
    slide.setFillEnabled(true);
    popupLayout.startAnimation(slide);
    // and then the AnimationListener

更新

我尝试设置LayoutParams,但它也不起作用。

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    dialogLayout.getWidth(), dialogLayout.getHeight());
            // lp.setMargins(0, 0, 0, 0);
            dialogLayout.setLayoutParams(lp);

2 个答案:

答案 0 :(得分:1)

onAnimationEnd()内,您必须更新您正在设置动画的视图的布局参数,在您的情况下,它是LinearLayout。您必须更新有关父级的视图。如果您的动画视图父级是例如RelativeLayout你可以这样称呼:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) animatedView.getLayoutParams();
//TODO modify params
animatedView.setLayoutParams(params);
animatedView.requestLayout();

我认为这应该有效。您也可以尝试在父母上调用requestLayout()

关于View动画还有tutorial on YT。 Chet Haase讨论了处理动画,包括TranslateAnimation。我没有时间观看,但也许你会找到答案。

<强>更新

如果您在动画后成功设置了布局参数,请不要忘记删除行:

slide.setFillAfter(true);

请记住,动画仅修改相对于视图位置的绘图坐标。换句话说,它会创建一个原始位置的偏移量,在动画结束后必须重置。方法setFillAfter(true)跳过重置此偏移量。

答案 1 :(得分:0)

setContentView(rootLayout)将完成工作......无法正常工作