第一次尝试后动画不会重现

时间:2014-12-03 10:11:33

标签: java android animation

首先它按预期顺利运行并完成,但之后它在设备和genymotion模拟器的中途停止。有趣的是,它使用相同的动画完成,如果我只将3-5串在一起,它是可重现的。 我使用的是Android Studio 1.0 RC2。

MainActivity的相关部分

 private static final String ANIM = "Animation";

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

        Button btn = (Button) findViewById(R.id.button);

        final ImageView iv = (ImageView) findViewById(R.id.imageV);
        iv.setVisibility(View.INVISIBLE);

        final Animation slideUpFromBottom = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_bottom);
        final Animation slideLeft = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_out_left);
        final Animation slideInFromLeft = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_left);
        final Animation slideOutAtBottom = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_out_bottom);
        final Animation slideOutAtTop = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_out_top);
        final Animation slideInFromTop = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_top);
        final Animation slideInFromRight = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_right);
        final Animation slideOutToRight = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_out_right);

        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                slideUpFromBottom.setAnimationListener(new Animation.AnimationListener()
                {
                    @Override
                    public void onAnimationStart(Animation animation)
                    {
                        Log.d(ANIM,"Startes slideUpFromBottom");
                        iv.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation)
                    {
                        Log.d(ANIM, "Finished slideUpFromBottom");
                        slideLeft.setStartOffset(100);
                        iv.setVisibility(View.INVISIBLE);
                        iv.startAnimation(slideLeft);
                        slideUpFromBottom.reset();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation)
                    {

                    }
                });

                slideLeft.setAnimationListener(new Animation.AnimationListener()
                {
                    @Override
                    public void onAnimationStart(Animation animation)
                    {
                        Log.d(ANIM, "Started slideLeft");
                        iv.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation)
                    {
                        Log.d(ANIM, "Finished slideLeft");
                        slideInFromLeft.setStartOffset(100);
                        iv.setVisibility(View.INVISIBLE);
                        iv.startAnimation(slideInFromLeft);
                        slideLeft.reset();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation)
                    {

                    }
                });

                slideInFromLeft.setAnimationListener(new Animation.AnimationListener()
                {
                    @Override
                    public void onAnimationStart(Animation animation)
                    {
                        Log.d(ANIM, "Started slideInFromLeft");
                        iv.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation)
                    {
                        Log.d(ANIM, "Finished slideInFromLeft");
                        slideOutAtTop.setStartOffset(100);
                        iv.setVisibility(View.INVISIBLE);
                        iv.startAnimation(slideOutAtTop);
                        slideInFromLeft.reset();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation)
                    {

                    }
                });

                slideOutAtTop.setAnimationListener(new Animation.AnimationListener()
                {
                    @Override
                    public void onAnimationStart(Animation animation)
                    {
                        Log.d(ANIM, "Started slideOutAtTop");
                        iv.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation)
                    {
                        Log.d(ANIM, "Finished slideOutAtTop");
                        slideInFromTop.setStartOffset(100);
                        iv.setVisibility(View.INVISIBLE);
                        iv.setAnimation(slideInFromTop);
                        slideOutAtTop.reset();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation)
                    {

                    }
                });

                slideInFromTop.setAnimationListener(new Animation.AnimationListener()
                {
                    @Override
                    public void onAnimationStart(Animation animation)
                    {
                        Log.d(ANIM, "Started slideInFromTop");
                        iv.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation)
                    {
                        Log.d(ANIM, "Finished slideInFromTop");
                        slideOutToRight.setStartOffset(100);
                        iv.setVisibility(View.INVISIBLE);
                        iv.setAnimation(slideOutToRight);
                        slideInFromTop.reset();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation)
                    {

                    }
                });

                slideOutToRight.setAnimationListener(new Animation.AnimationListener()
                {
                    @Override
                    public void onAnimationStart(Animation animation)
                    {
                        Log.d(ANIM, "Started slideOutToRight");
                        iv.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation)
                    {
                        Log.d(ANIM, "Finished slideOutToRight");
                        slideInFromRight.setStartOffset(100);
                        iv.setVisibility(View.INVISIBLE);
                        iv.setAnimation(slideInFromRight);
                        slideOutToRight.reset();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation)
                    {

                    }
                });

                slideInFromRight.setAnimationListener(new Animation.AnimationListener()
                {
                    @Override
                    public void onAnimationStart(Animation animation)
                    {
                        Log.d(ANIM, "Started slideInFromRight");
                        iv.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation)
                    {
                        Log.d(ANIM, "Finished slideInFromRight");
                        slideOutAtBottom.setStartOffset(100);
                        iv.setVisibility(View.INVISIBLE);
                        iv.setAnimation(slideOutAtBottom);
                        slideInFromRight.reset();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation)
                    {

                    }
                });

                slideOutAtBottom.setAnimationListener(new Animation.AnimationListener()
                {
                    @Override
                    public void onAnimationStart(Animation animation)
                    {
                        Log.d(ANIM, "Started slideOutAtBottom");
                        iv.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation)
                    {
                        Log.d(ANIM, "Finished slideOutAtBottom");
                        iv.setVisibility(View.INVISIBLE);
                        slideOutAtBottom.reset();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation)
                    {

                    }
                });

                iv.startAnimation(slideUpFromBottom);
            }
        });

    }

slide_in_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/accelerate_interpolator"
     android:shareInterpolator="true">

    <translate
        android:duration="1000"
        android:fromXDelta="0"
        android:fromYDelta="150%"
        android:toXDelta="0"
        android:toYDelta="0" />

</set>

slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/accelerate_interpolator"
     android:shareInterpolator="true">

    <translate
        android:fromXDelta="-100%"
        android:toXDelta="0%"
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:duration="1000" />
</set>

slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/accelerate_interpolator"
     android:shareInterpolator="true">


    <translate
        android:fromXDelta="100%"
        android:toXDelta="0%"
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:duration="1000" />
</set>

slide_in_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/accelerate_interpolator"
     android:shareInterpolator="true">

    <translate
        android:fromXDelta="0%"
        android:toXDelta="0%"
        android:fromYDelta="-150%"
        android:toYDelta="0%"
        android:duration="1000"/>
</set>

slide_out_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/accelerate_interpolator"
     android:shareInterpolator="true">

    <translate
        android:fromXDelta="0%"
        android:toXDelta="0%"
        android:fromYDelta="0%"
        android:toYDelta="150%"
        android:duration="1000"/>
</set>

slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/accelerate_interpolator"
     android:shareInterpolator="true">

    <translate
        android:fromXDelta="0%"
        android:toXDelta="-100%"
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:duration="1000" />
</set>

slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/accelerate_interpolator"
     android:shareInterpolator="true">


    <translate
        android:fromXDelta="0%"
        android:toXDelta="100%"
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:duration="1000" />
</set>

slide_out_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/accelerate_interpolator"
     android:shareInterpolator="true">

    <translate
    android:fromXDelta="0%"
    android:toXDelta="0%"
    android:fromYDelta="0%"
    android:toYDelta="-150%"
    android:duration="1000"/>
</set>

1 个答案:

答案 0 :(得分:1)

我猜是因为你在.setAnimation的某些内部使用了onAnimationEnd

如果您想使用.setAnimation,则可能需要先致电.setStartTime

或仅使用.startAnimation表示所有动画。