以下是一個簡單的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
回調監聽動畫結束事件。