不调用AnimationListener方法onAnimationRepeat

时间:2019-02-27 09:49:44

标签: android

实现AnimationListener时遇到问题

在这里,我实现了AnimationListener,当我在按钮上启动动画时,单击,然后调用onAnimationStart()和onAnimationEnd(),但是不调用onAnimationRepeat()方法。

我在这里做错了,请告诉我。

这是我的代码:

    public class AnimationDemo extends AppCompatActivity {
    TextView textView;
    int i = 0;
    String [] messages;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation_demo);
        messages = new String[]{
                "India", "India is", "India is Great", "India is Great Country!"
        };
    }

    public void startAnimation(View view){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.sample_animation);

        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.d("MY_TAG", "Animation Started!!!");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.d("MY_TAG", "Animation End!!!");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.d("MY_TAG", "Repeating...");
            }
        });

        textView = (TextView) findViewById(R.id.tvMessage);
        textView.startAnimation(animation);
    }
}

这是我的XML文件:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <scale
            android:interpolator="@android:anim/accelerate_decelerate_interpolator"
            android:duration="1000"
            android:fillAfter="true"
            android:fromXScale="0.0"
            android:fromYScale="0.0"
            android:toXScale="1.0"
            android:toYScale="1.0"
            android:repeatMode="reverse"
            android:repeatCount="5"
            android:pivotX="50%"
            android:pivotY="50%"
            />
    </set>

2 个答案:

答案 0 :(得分:0)

我更正了您的代码,基本上您没有在视图中添加动画:

public class AnimationDemo extends AppCompatActivity {
private static final android.view.animation.AnimationUtils AnimationUtils = ;
TextView textView;
int i = 0;
String[] messages;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_animation_demo);
    messages = new String[]{
            "India", "India is", "India is Great", "India is Great Country!"
    };

    textView = (TextView) findViewById(R.id.tvMessage);
    startAnimation(textView);
}

public void startAnimation(View view) {
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.sample_animation);

    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            Log.d("MY_TAG", "Animation Started!!!");
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            Log.d("MY_TAG", "Animation End!!!");
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            Log.d("MY_TAG", "Repeating...");
        }
    });

    view.startAnimation(animation);
}

}

答案 1 :(得分:0)

我已经找到了 onAnimationRepeat 监听器没有被触发的原因。 如果您使用 anim 文件夹中的 XML 动画定义文件制作动画,则不会触发 onAnimationRepeat 侦听器。

您必须以编程方式使用完整参数定义动画。

就是这样...