【问题标题】:Android Position Button after animation (view.layout())动画后的Android位置按钮(view.layout())
【发布时间】:2015-04-10 08:39:24
【问题描述】:

我知道,我必须在动画之后重新定位实际视图,以使其之后可点击。我找到的解决方案是使用视图的 .layout(l,t,r,b) 方法。

但是现在发生的情况是,视图正确地向上移动了 150 像素,但随后(当调用上述方法时)又向上跳跃了 150 像素。所以我的视图的“hitbox”现在是我期望的位置,但我的视图的渲染图像在它上面。

点击 hitbox,视图开始向下 150px 动画,然后再次跳跃 150px。

这里有两张图片来说明我的问题。第二张图片显示了它在“向上动画”之后的样子。红色矩形显示我希望图像的位置。

之前:

之后:

以下是重要的代码行:

animSlideUpButton = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -150);
animSlideDownButton = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -150, Animation.ABSOLUTE, 0);

animSlideUpButton.setDuration(1000);
animSlideDownButton.setDuration(1000);

animSlideUpButton.setFillAfter(true);
animSlideDownButton.setFillAfter(true);

animSlideUpButton.setAnimationListener(this);
animSlideDownButton.setAnimationListener(this);

听众:

@Override
public void onAnimationEnd(Animation animation) {
    if(animation == animSlideUpButton){
        int left = containerBttToggleFooter.getLeft();
        int top = containerBttToggleFooter.getTop();
        int right = containerBttToggleFooter.getRight();
        int bottom = containerBttToggleFooter.getBottom();

        containerBttToggleFooter.layout(left, top-150, right, bottom-150);

    }else if(animation == animSlideDownButton){
        int left = containerBttToggleFooter.getLeft();
        int top = containerBttToggleFooter.getTop();
        int right = containerBttToggleFooter.getRight();
        int bottom = containerBttToggleFooter.getBottom();

        containerBttToggleFooter.layout(left, top+150, right, bottom+150);

    }
}

有什么想法吗?

【问题讨论】:

    标签: android animation layout


    【解决方案1】:

    您的问题是您将fillAfter 与动画一起使用,因此动画结束后翻译保持 150,除此之外,您还通过 onAnimationEnd 方法将它们额外移动了 150。

    要解决此问题,请更改以下代码:

    animSlideUpButton.setFillAfter(true);
    animSlideDownButton.setFillAfter(true);
    

    到这里:

    animSlideUpButton.setFillAfter(false);
    animSlideDownButton.setFillAfter(false);
    

    【讨论】:

    • 谢谢,就是这样!但是现在在重置视图的动画和我在 onEndAnimation-Listener 中设置新布局之间有一点闪烁。
    • 尝试在 onAnimationEnd 方法中的视图上添加对 clearAnimation() 的调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    相关资源
    最近更新 更多