Android RotateAnimation不会在启动活动之前等待按钮旋转
我在安卓系统中有一个Android RotateAnimation不会在启动活动之前等待按钮旋转,android,rotateanimation,Android,Rotateanimation,我在安卓系统中有一个ImageButton,点击时会旋转。问题在于,当用户点击它并进入下一行的新活动时,它没有完成旋转。我尝试了Thread.sleep(…)和wait(…)但是在动画开始之前,将RotateAnimation(…)与这些一起放置实际上是在休眠 我需要动画实际完成,然后进入startActivity(新意图(..) 这是密码 amazingPicsButton.setOnClickListener(new View.OnClickListener() {
ImageButton
,点击时会旋转。问题在于,当用户点击它并进入下一行的新活动时,它没有完成旋转。我尝试了Thread.sleep(…)
和wait(…)
但是在动画开始之前,将RotateAnimation(…)
与这些一起放置实际上是在休眠
我需要动画实际完成,然后进入startActivity(新意图(..)
这是密码
amazingPicsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
amazingPicsSound = createRandButSound();
amazingPicsSound.start();
rotateAnimation(v);
startActivity(new Intent("com.jasfiddle.AmazingInterface.AMAZINGPICS"));
}
});
}
/** function that produces rotation animation on the View v.
* Could be applied to button, ImageView, ImageButton, etc.
*/
public void rotateAnimation(View v){
// Create an animation instance
Animation an = new RotateAnimation(30, 360, v.getWidth()/2, v.getHeight()/2);
// Set the animation's parameters
an.setDuration(20); // duration in ms
an.setRepeatCount(10); // -1 = infinite repeated
// an.setRepeatMode(Animation.REVERSE); // reverses each repeat
an.setFillAfter(true); // keep rotation after animation
v.setAnimation(an);
// Apply animation to the View
}
您从不要求应用程序等待动画结束后才开始新活动。 看
要了解如何使用动画监听器,动画是一个异步过程,因此如果希望在继续之前完成动画,则需要添加动画监听器,并在动画完成后执行下一行代码:
amazingPicsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
amazingPicsSound = createRandButSound();
amazingPicsSound.start();
rotateAnimation(v);
}
});
然后
public void rotateAnimation(View v){
// Create an animation instance
Animation an = new RotateAnimation(30, 360, v.getWidth()/2, v.getHeight()/2);
// Set the animation's parameters
an.setDuration(20); // duration in ms
an.setRepeatCount(10); // -1 = infinite repeated
// an.setRepeatMode(Animation.REVERSE); // reverses each repeat
an.setFillAfter(true); // keep rotation after animation
an.addAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent("com.jasfiddle.AmazingInterface.AMAZINGPICS"));
}
});
v.setAnimation(an);
}
请注意,startActivity
调用不在AnimationListener
的onAnimationEnd
方法中,而不是在将动画设置到视图中之后