旋转带动画的图像

Ari*_*Roy 23 android image-rotation android-animation android-imageview

图像的两种状态

是)我有的

我有一个箭头图像(如左图).当用户点击它时,它应该用动画旋转180度,看起来应该是正确的.

我做了什么

private void rotate(float degree, final int toggleV) {

        final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);

        rotateAnim.setDuration(500);
        toggle.startAnimation(rotateAnim);
        rotateAnim.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {


                if (toggleV == 1)
                    toggle.setImageResource(R.drawable.toggle_up);
                else
                    toggle.setImageResource(R.drawable.toggle_down);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

问题

我看到动画工作正常,但设置图像时有一点闪烁.可能是因为动画结束和设置图像时的时差.

如何删除此闪烁问题?你有更好的方法吗?

Zso*_*agy 54

首先,您最低SDK要求是什么?如果它至少是Android 3.0,您可以使用更新的动画框架,并使用以下内容为您的Image设置动画:

imageView.animate().rotation(180).start();
Run Code Online (Sandbox Code Playgroud)

关于闪烁:我不会在旋转后重置ImageView的源图像,我只需保留原始图像并确保旋转动画在动画后填充,使图像旋转.闪烁很可能是由于在更改源图像时View的重新布局/重绘造成的.

可能引起进一步的视觉伪像(闪烁?),因为原始旋转的图像和旋转的静态图像可能在几个像素中不同.

  • 好吧,如果您使用我提供的代码示例,此代码专门告知查看"旋转到180度".因此,如果它已经旋转到180度,那么它什么都不做.在单击侦听器中,您可以检查View的当前旋转(.getRotation),如果它不是0,则以类似的方式将其旋转回0. (9认同)
  • 当我按下ImageView时,它只会旋转一次。但是它应该在我再次按下它之后再次旋转,依此类推。但这不会发生。任何的想法? (2认同)