【问题标题】:How to move button after translate animation?翻译动画后如何移动按钮?
【发布时间】:2016-04-14 19:34:54
【问题描述】:

我将一个居中的水平按钮向左移动(相对布局)。播放平移动画 (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) {
    }
});

【问题讨论】:

    标签: android animation button layout translate-animation


    【解决方案1】:

    找到解决方案!

    使用

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      相关资源
      最近更新 更多