android

Android开场动画类完整实现代码

小云
109
2023-08-08 14:07:07
栏目: 编程语言

以下是一个简单的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方法来启动动画,传入相应的ContextView对象,并可以通过AnimatorListener回调监听动画结束事件。

0
看了该问题的人还看了