如何在第一个动画结束后开始第二个动画

时间:2013-01-16 19:56:56

标签: java android xml animation

我已经为两个图像创建了两个动画并且它工作正常但我希望在第一个动画完成时启动第二个动画。

代码:

package com.example.animatest;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class animation001 extends Activity {

    private ImageView image1;
    private ImageView image2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image1 = (ImageView) findViewById(R.id.imageView1);
        image2 = (ImageView) findViewById(R.id.imageView2);

        final Animation animTranslate1 = AnimationUtils.loadAnimation(this,
                R.anim.translate);
        final Animation animTranslate2 = AnimationUtils.loadAnimation(this,
                R.anim.translate2);

        image1.startAnimation(animTranslate1);
        image2.startAnimation(animTranslate2);

    }

}

这是第一个动画片xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <translate
        android:fromXDelta="-100%p"
        android:toXDelta="0"
        android:duration="1500"
        android:repeatCount="0"
        android:repeatMode="reverse" />
</set>

这是第二个动画xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <translate
        android:fromYDelta="-100%p"
        android:toYDelta="0"
        android:duration="25000"
        android:repeatCount="0"
        android:repeatMode="reverse" />
</set>

2 个答案:

答案 0 :(得分:5)

你可以这样做&amp;这对我有用。

只需在活动

中实施AnimationListener即可
public class animation001 extends Activity implements AnimationListener {
    .........
}

按照此tutorial,这有助于您更好地实施动画

答案 1 :(得分:1)

  1. 创建AnimationListener并实施public void onAnimationEnd(Animation animation)
  2. 使用AnimationListener
  3. 将此Animation.setAnimationListener()设置为您的第一个动画
  4. AnimationListener.onAnimationEnd()
  5. 中开始第二个动画