这是单击按钮时旋转图像的java代码。图像旋转它是完美的,但如果我在旋转未结束时再次单击按钮,动画将自动重启,而不是结束动画。我怎么能等到动画结束?我找到了Animation.AnimationListener,我认为onAnimationEnd对我很有用,但是我无法将它集成到我的代码中...请帮助我: - )
package com.example.helloword;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class Rotation_test extends Activity {
private float statdegree = (float) 0.0;
private float enddegree = (float) 90.0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rotation_test);
// ---------------------------------------------------------------------------------------------
Button buttonRotateCenter = (Button) findViewById(R.id.rotatecenter);
final ImageView floatingImage = (ImageView) findViewById(R.id.floatingimage);
// AnimationRotation
final Animation animationRotateCenter = new RotateAnimation(statdegree,
enddegree, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animationRotateCenter.setDuration(5000L);
animationRotateCenter
.setInterpolator(new AccelerateDecelerateInterpolator());
// \AnimationRotation
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
floatingImage.startAnimation(animationRotateCenter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_rotation_test, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
您必须使用一个布尔变量来跟踪动画是否正在进行中。然后在动画监听器和按钮单击中使用此变量,如下面的代码
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
if(!anim_in_progress)
floatingImage.startAnimation(animationRotateCenter);
}
});
animationRotateCenter.setAnimationListener(new anim_listener());
}
boolean anim_in_progress=false;
class anim_listener implements AnimationListener
{
@Override
public void onAnimationEnd(Animation arg0) {
anim_in_progress=false;
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
anim_in_progress=true;
}
}
答案 1 :(得分:0)
从未尝试过,但可能有效
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
if(!floatingImage.hasTransientState())
floatingImage.startAnimation(animationRotateCenter);
}
});