我正在写一个游戏应用程序。如果用户按下错误按钮我想要显示动画,当动画完成时,则完成()我的游戏;
目前,我的动画刚刚在调用完成后启动。我希望动画完成。
下面是我的实现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);
}
}
答案 0 :(得分:0)
你能更具体地说这个'动画'是什么吗?它是一个你自己玩框架或只是一个图像或视频的类吗?
如果这是第一个最好的方法是创建一个回调方法,并在动画完成后触发它。
答案 1 :(得分:0)
这样的事情:
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;
}