翻译动画后如何移动按钮?

How to move button after translate animation?

我正在将居中的水平按钮向左移动(相对布局)。播放翻译动画 (onAnimationEnd) 后,我设置布局参数以删除 CENTER_HORIZONTAL 规则。现在有两种可能:

1) 如果我设置 fillAfter(true) 按钮播放动画,然后离开屏幕一半;

2) 如果我设置 fillAfter(false) 按钮会播放动画(因此它会向左移动),然后它会闪烁一毫秒。那是因为它会回到原来的位置,直到我调用 setLayoutParams 并将其正确设置到左侧。但是它闪烁,这不是很好。

如何避免闪光?

代码

TranslateAnimation translateAnimation = new TranslateAnimation(0, LEFT_TRANSLATION, 0, 0);

translateAnimation.setDuration(1000);
translateAnimation.setFillAfter(true);

button1.startAnimation(translateAnimation);

translateAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) button1.getLayoutParams();
        lp.removeRule(RelativeLayout.CENTER_HORIZONTAL);
        button1.setLayoutParams(lp);

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }
});

已找到解决方案!

使用

button.clearAnimation();

在 onAnimationEnd() 中像这样:

@Override
public void onAnimationEnd(Animation animation) {

    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) button1.getLayoutParams();
    lp.removeRule(RelativeLayout.CENTER_HORIZONTAL);
    button1.setLayoutParams(lp);

    button1.clearAnimation();
}

来源:http://www.helloandroid.com/tutorials/solving-advanced-animation-problems