以下是一个简单的Android开场动画类的完整实现代码:
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
public class SplashAnimation {
private static final int ANIMATION_DURATION = 1000;
public static void animate(Context context, View view, AnimatorListener listener) {
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
fadeIn.setInterpolator(new DecelerateInterpolator());
fadeIn.setDuration(ANIMATION_DURATION / 2);
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setDuration(ANIMATION_DURATION / 2);
fadeOut.setStartDelay(ANIMATION_DURATION / 2);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(fadeIn, fadeOut);
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
// 动画开始时的回调
}
@Override
public void onAnimationEnd(Animator animation) {
// 动画结束时的回调
if (listener != null) {
listener.onAnimationEnd();
}
}
@Override
public void onAnimationCancel(Animator animation) {
// 动画取消时的回调
}
@Override
public void onAnimationRepeat(Animator animation) {
// 动画重复时的回调
}
});
animatorSet.start();
}
public interface AnimatorListener {
void onAnimationEnd();
}
}
这个动画类实现了一个简单的渐隐渐现的开场动画。可以通过调用animate
方法来启动动画,传入相应的Context
和View
对象,并可以通过AnimatorListener
回调监听动画结束事件。