在 Android 应用中不断淡入和淡出图像

Fade in and out an image constantly in Android app

我试图使图像不断淡入和淡出,但它只是淡入和淡出一次。 我怎样才能让它不断重复? 这是代码:

Animation fadeIn = new AlphaAnimation(0, 1);
    fadeIn.setDuration(1000);

    Animation fadeOut = new AlphaAnimation(1, 0);
    fadeOut.setStartOffset(1000);
    fadeOut.setDuration(1000);

    AnimationSet animation = new AnimationSet(true);
    animation.addAnimation(fadeIn);
    animation.addAnimation(fadeOut);
    ImageView loading = (ImageView)findViewById(R.id.loading);
    loading.startAnimation(animation);

你应该重复你的动画:

animation.setRepeatCount(Animation.INFINITE);

使用 Animator,这非常简单:

Animator alphaAnimator = ObjectAnimator.ofFloat(loading, View.ALPHA, 0f, 1f);
alphaAnimator.setDuration(1000);
alphaAnimator.setRepeatMode(ValueAnimator.REVERSE);
alphaAnimator.setRepeatCount(ValueAnimator.INFINITE);
alphaAnimator.start();

尝试使用 Animation class 中的方法 setRepeatMode(int repeatMode)