旋转带有动画的图像 [英] Rotate an Imagewith Animation

查看:224
本文介绍了旋转带有动画的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么

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

I have an arrow image (like the left one). When the user clicks on it, it should rotate 180 degree with an animation and should look like the right one.

我做了什么

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) {

            }
        });
    }

问题

我看到动画效果很好,但是在设置图像时出现了一些闪烁.可能是由于动画结束和设置图像时的时差所致.

I see that the animation works fine but there is a little flicker while setting the image. May be because of the time difference when the animation ends and the image is set.

如何解决此闪烁问题?您有更好的方法吗?

How can I remove this flicker issue? Do you have any better approach to do this?

推荐答案

首先,您对SDK的最低要求是什么?如果至少是Android 3.0,则可以使用较新的动画框架,并使用以下方式对Image进行动画处理:

First of all, what is you minimum SDK requirement? In case it's at least Android 3.0, you can use the newer animation framework, and animate your Image with something like this:

imageView.animate().rotation(180).start();

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

About the flicker: I wouldn't reset the source image of the ImageView after the rotation, I'd just leave in the original and make sure that the rotation animation fills after the animation, leaving the image rotated. The flicker is most likely caused by the View's relayout/redraw upon changing the source image.

由于原始旋转的图像和旋转的静态图像可能在几个像素上有所不同,可能会导致进一步的视觉伪像(闪烁?).

Further visual artifacts (flicker?) may be caused because the original-rotated image and the rotated static image might differ in a few pixels.

这篇关于旋转带有动画的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆