我在android中有一个ImageButton
,点击时会旋转。问题是,当用户点击它并进入下一行的新活动时,它没有完成旋转。我尝试了Thread.sleep(..)
和wait(..)
,但在动画开始之前,将RotateAnimation(..)
与这些实际上一起睡了。
我需要动画实际完成,然后继续startActivity(new Intent(..))
这是代码
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
}
答案 0 :(得分:0)
您永远不会要求您的应用等待动画结束以开始新活动。 见http://developer.android.com/reference/android/view/animation/Animation.html#setAnimationListener(android.view.animation.Animation.AnimationListener)
了解如何使用AnimationListener
答案 1 :(得分:0)
动画是一个异步过程,因此如果您想在继续之前完成动画,那么您需要添加动画侦听器并在动画完成时执行下一行代码:
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
调用不在onAnimationEnd
的{{1}}方法内,而不是在将动画设置到视图之后。