我使用以下代码旋转一个名为 photo
的 ImageView
:
RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(400);
rotate.setFillEnabled(true);
rotate.setFillAfter(true);
rotate.setInterpolator(new LinearInterpolator());
rotate.setAnimationListener(new Animation.AnimationListener() {
});
photo.startAnimation(rotate);
它旋转得很好并且持续存在。稍后我可能想改变这个ImageView中的图片。但是,由于前一张图像上的动画持续存在,我的新图像出现旋转。如何在切换图像之前“撤消”此动画?
最佳答案
显然,最好的方法是使用另一个持续时间为零的动画。如果我在这个函数中有我的原始动画:
private void rotatePhoto(int fromDegrees, int toDegrees, int duration) {
RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(duration);
rotate.setFillEnabled(true);
rotate.setFillAfter(true);
rotate.setInterpolator(new LinearInterpolator());
rotate.setAnimationListener(new Animation.AnimationListener() {
});
photo.startAnimation(rotate);
}
我可以使用如下所示将照片旋转 90 度:rotatePhoto(0,90,400)
。之后,要“撤消”动画,我可以使用 rotatePhoto(0,0,0)
关于Android撤销 View 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32575218/