Я пишу приложение для игры. Если пользователь нажимает неправильную кнопку, я хочу показать анимацию, и когда анимация будет завершена, закончить() мою игру;
в настоящее время моя анимация начинается только после вызова final. Я хочу, чтобы анимация завершилась.
Ниже мой класс, который реализует AnimationListener
public final class DisplayNextView implements Animation.AnimationListener {
View changeView;
Drawable image;
public DisplayNextView(View parChangeView, Drawable parImage) {
this.changeView = parChangeView;
this.image = parImage;
}
public void onAnimationStart(Animation animation) {
changeView.post(new SwapViews(changeView, image));
changeView.postDelayed(new SwapViews(changeView, null), 1000);
}
public void onAnimationEnd(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
}
Это мой основной метод активности, который запускает анимацию
private void applyRotation(View parChangeView, Drawable image, float start, float end) {
// Find the center of image
final float centerX = parChangeView.getWidth() / 2.0f;
final float centerY = parChangeView.getHeight() / 2.0f;
// Create a new 3D rotation with the supplied parameter
// The animation listener is used to trigger the next animation
final Flip3DAnimation rotation =
new Flip3DAnimation(start, end, centerX, centerY);
rotation.setDuration(250);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView (parChangeView,image));
parChangeView.startAnimation(rotation);
//return rotation;
}
Это мой класс SwapView, это поток
public final class SwapViews implements Runnable {
View changeView;
Drawable image;
public SwapViews(View parChangeView, Drawable parImage) {
this.changeView = parChangeView;
this.image = parImage;
}
public void run() {
final float centerX = changeView.getWidth() / 2.0f;
final float centerY = changeView.getHeight() / 2.0f;
Flip3DAnimation rotation;
changeView.setBackgroundDrawable(image);
//TODO should find a better way!!
if (image==null)
changeView.setBackgroundColor(Color.BLACK);
rotation = new Flip3DAnimation(-90, 0, centerX, centerY);
rotation.setDuration(250);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
changeView.startAnimation(rotation);
}
}
Что-то вроде этого:
public final class DisplayNextView implements Animation.AnimationListener {
View changeView;
Drawable image;
boolean mIsCorrect;
Activity mContext;
public DisplayNextView(View parChangeView, Drawable parImage,
boolean isCorrect, Activity context) {
this.changeView = parChangeView;
this.image = parImage;
mIsCorrect = isCorrect;
mContext = context;
}
public void onAnimationStart(Animation animation) {
changeView.post(new SwapViews(changeView, image));
changeView.postDelayed(new SwapViews(changeView, null), 1000);
}
public void onAnimationEnd(Animation animation) {
if (mIsCorrect == false) {
mContext.finish();
}
}
public void onAnimationRepeat(Animation animation) {
}
}
И ваша applyRotation должна быть передана независимо от того, было ли правильное предположение:
private void applyRotation(View parChangeView, Drawable image, float start,
float end, boolean isCorrect) {
// Find the center of image
final float centerX = parChangeView.getWidth() / 2.0f;
final float centerY = parChangeView.getHeight() / 2.0f;
// Create a new 3D rotation with the supplied parameter
// The animation listener is used to trigger the next animation
final Flip3DAnimation rotation = new Flip3DAnimation(start, end,
centerX, centerY);
rotation.setDuration(250);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(parChangeView, image, isCorrect, this));
parChangeView.startAnimation(rotation);
// return rotation;
}
Можете ли вы уточнить, что такое "анимация"? Является ли это классом, в котором вы сами играете фреймы или просто изображение или видео?
если первым является лучший способ создать метод обратного вызова и просто запустить это, когда анимация будет выполнена.