在Android中使用旋转动画,但单击按钮后图像中没有旋转。
public class MainActivity extends AppCompatActivity {
ImageView spinImage;
Button buton;
Random r;
int degree =0 , degreeold= 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buton = (Button) findViewById(R.id.spinbutton);
spinImage= (ImageView) findViewById(R.id.spinimage);
r= new Random();
buton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
degreeold = degree % 360;
degree = r.nextInt(3600)+720;
degree= 4400;
RotateAnimation rotate = new RotateAnimation( degreeold, degree,
RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF , 0.5f);
rotate.setDuration(3600);
rotate.setFillAfter(true);
rotate.setInterpolator( new DecelerateInterpolator());
rotate.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
spinImage.setAnimation(rotate);
}
});
}
我无法找到图像中没有旋转的错误。在没有任何延迟的情况下运行和应用程序打开时没有错误,但单击按钮时没有动画。
答案 0 :(得分:0)
您尚未启动动画。尝试使用spinImage.startAnimation(rotate);
setAnimation旨在为您提供更精细的控制,如果您想要延迟启动动画或与其他动画一起使用。
来自文档:
设置要为此视图播放的下一个动画。如果您想立即播放动画,请改用startAnimation(android.view.animation.Animation)。此方法允许对开始时间和失效进行细粒度控制,但您必须确保1)动画具有开始时间设置,以及2)视图的父级(控制其子级上的动画)将在以下情况下失效动画应该开始了。
答案 1 :(得分:0)
将spinImage.setAnimation(rotate);
替换为spinImage.startAnimation(rotate);