【问题标题】:A view after an scale animation, have not resize to new position缩放动画后的视图,尚未调整到新位置
【发布时间】:2012-07-11 22:12:15
【问题描述】:

缩放动画后的视图,尚未调整到新位置。

我使用 android 动画 (ScaleAnimation) 将布局(启动时的屏幕大小)缩放到自身大小的 0.95 倍,并且比例参考屏幕中心点。

final AnimationListener al2 = new AnimationListener() {

    @Override
    public void onAnimationStart(Animation arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationRepeat(Animation arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationEnd(Animation arg0) {

    }
};
public void animScale(){
    ScaleAnimation sa = new ScaleAnimation(1,0.95f,1,0.95f, topLayout.getMeasuredWidth()/2, topLayout.getMeasuredHeight()/2);
    sa.setDuration(500);
    sa.setFillEnabled(true);
    sa.setFillAfter(true);
    sa.setAnimationListener(al2);
    topLayout.startAnimation(sa);

}

public void onCreate(Bundle savedInstanceState) {   

    scaleButton = (Button) findViewById(R.id.scaleButton);
    scaleButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            animScale();
        }
    });
}

在此动画之后,我的布局已更改为新位置,但是当我再次单击按钮时

布局总是从屏幕尺寸缩放到 0.95 倍。

向我展示布局永远不会通过动画改变实际大小。

我需要在动画监听器动画结尾添加什么代码?

我希望在我点击按钮时实现它, 屏幕尺寸 -> 屏幕尺寸 *0.95 -> 屏幕尺寸 *0.95^2 ->........

非常感谢。

【问题讨论】:

    标签: android animation


    【解决方案1】:

    出现问题是因为您使用的是Animation 类。使用动画,您只能更改可见外观。 在您的情况下,您需要使用Animator,它还会更新位置和尺寸。

    Animation 和 Animator 的区别在 this question 中解释。

    【讨论】:

      【解决方案2】:

      ScaleAnimation 不会改变布局的参数。它在不更改参数的情况下进行动画处理,以获得更流畅的动画效果。

      你可以创建一个布尔实例变量

      Boolean animationDone = false;
      

      并在onAnimationEnd中设置为true表示动画是否完成

      public void onAnimationEnd(Animation arg0) {
         animationDone=true;
      }
      

      并在动画之前在 animScale 中检查它以防止多个动画....

      public void animScale(){
       if ( animationDone ) return;
        //..... animation code here...
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-26
        • 1970-01-01
        • 1970-01-01
        • 2016-02-10
        相关资源
        最近更新 更多