3x3网格图片点击旋转时动画结束后额外跳转90度如何解决?
问题原因说明
你当前使用的RotateAnimation属于Android旧版补间动画体系,这类动画仅会修改View绘制时的临时渲染矩阵,不会更改View的真实属性值。你设置setFillAfter(true)后,动画结束时会保留最后一帧的渲染效果,但此时View本身的rotation属性仍然是动画开始前的旧值。这种情况下你再手动调用setRotation()设置新的角度,相当于「动画残留的临时旋转效果」和「View真实属性的旋转效果」叠加,就会出现多旋转90度的问题。
解决方案
方案1:使用属性动画实现(推荐)
直接用Android属性动画ObjectAnimator,它会直接修改View的真实rotation属性,动画结束后不需要额外调用setRotation(),从根源避免叠加问题。示例代码如下:
// prevRotation 为动画开始前的view实际rotation值,targetRotation 为目标角度 ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(yourImageView, "rotation", prevRotation, targetRotation); rotateAnim.setDuration(999); rotateAnim.setInterpolator(new LinearInterpolator()); rotateAnim.start();
方案2:保留现有补间动画的修改方法
如果不想切换动画方案,只需要在动画结束回调中先清除动画的临时效果,再设置真实旋转属性即可:
给你的RotateAnimation添加动画监听,修改逻辑如下:
RotateAnimation rotate = new RotateAnimation(prevRotation, rotationClockwise, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotate.setDuration(999); rotate.setInterpolator(new LinearInterpolator()); rotate.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationEnd(Animation animation) { // 先清除动画的临时渲染效果 yourImageView.clearAnimation(); // 再设置View的真实旋转属性 yourImageView.setRotation(targetRotation); } @Override public void onAnimationRepeat(Animation animation) {} }); yourImageView.startAnimation(rotate);
内容的提问来源于stack exchange,提问作者Bolo
相关产品推荐
相关产品推荐

