3

简单的任务:通过按下按钮,将 X 值缩放为 0,当动画完成后,在第二个视图上开始另一个动画,将 X 从 0 缩放到 1。1 秒后应该播放反向动画,仅此而已。下面的运行代码我有动画第一部分的无限动画循环。

使用了 Nineoldandroids lib,但我认为这与原生动画框架并没有什么不同,至少在果冻豆设备上。

public class MainActivity extends Activity
{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final View mybutton = findViewById(R.id.mybutton);
    final View myprogress = findViewById(R.id.myprogress);

    mybutton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            animate(mybutton).scaleX(0).setListener(new AnimatorListenerAdapter()
            {
                @Override
                public void onAnimationEnd(Animator animation)
                {
                    mybutton.setVisibility(View.INVISIBLE);
                    myprogress.setVisibility(View.VISIBLE);
                    ViewHelper.setScaleX(myprogress, 0f);

                    animate(myprogress).scaleX(1).setListener(new AnimatorListenerAdapter()
                    {
                        @SuppressWarnings("ConstantConditions")
                        @Override
                        public void onAnimationEnd(Animator animation)
                        {
                            mybutton.getHandler().postDelayed(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    animate(myprogress).scaleX(0).setListener(new AnimatorListenerAdapter()
                                    {
                                        @Override
                                        public void onAnimationEnd(Animator animation)
                                        {
                                            myprogress.setVisibility(View.INVISIBLE);
                                            mybutton.setVisibility(View.VISIBLE);

                                            ViewHelper.setScaleX(mybutton, 0);
                                            animate(mybutton).scaleX(1);
                                        }
                                    });
                                }
                            }, 1000);
                        }
                    });
                }
            });
        }
    });
}

}

布局很简单:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
>

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/mycontainer">


            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="string text"
                    android:id="@+id/mybutton"
                    />

            <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@id/mybutton"
                    android:layout_alignTop="@id/mybutton"
                    android:layout_alignRight="@id/mybutton"
                    android:layout_alignBottom="@id/mybutton"
                    android:visibility="invisible"
                    android:id="@+id/myprogress"
                    />


            </RelativeLayout>
</RelativeLayout>

我哪里错了?

4

1 回答 1

21

疯狂!查明其来源: animate() 方法调用时,会创建 ViewPropertyAnimator 并将其保存到地图中。当您尝试使用 animate() 再次为该视图设置动画时,它会从地图中获取已创建的 viewpropertyanimator 并立即调用 onAnimationStart() ,甚至在您设置新的动画参数之前,并且因为在第一个动画上设置了animationlistener,所以它被触发了!并推出了第一个动画(它的第二部分)。这是创建无限循环。

要阻止它,当您第二次尝试为视图设置动画时,您必须清除旧的侦听器,所以

animate(mybutton).setListener(null).scaleX(1);

停止无限循环。文档应该警告它,绝对!

于 2014-04-08T10:42:55.527 回答