5

我一直在尝试编写将图像旋转到特定角度的活动方法。旋转有效,但问题是该方法每秒左右调用一次,但仅在前几次旋转。在某个时刻,动画图像只是静止不动,即使动画正在设置并以不同的值开始!它假设移动图像,就像前几次一样。

private void animateNeedle(final float sfAngle)
{
    final View viewNeedle = findViewById(R.id.needle);
    final RotateAnimation needleAnimation;

    RotateAnimation anim;

    anim = new RotateAnimation( 0,
        sfAngle,
        m_nNeedleWidth / 2,
        m_nNeedleHeight);
    anim.setDuration(200);
    anim.setFillAfter(true);
    viewNeedle.setAnimation(anim);
    anim.setAnimationListener(new AnimationListener()
    {

        @Override
        public void onAnimationEnd(final Animation animation)
        {
            viewNeedle.clearAnimation();
        }

        @Override
        public void onAnimationRepeat(final Animation animation)
        {
            // TODO Auto-generated method stub
        }

        @Override
        public void onAnimationStart(final Animation animation)
        {
            // TODO Auto-generated method stub
        }
    });

    anim.start();
    findViewById(android.R.id.content).postInvalidate();
}

我究竟做错了什么?我确保在动画期间不会调用该函数两次,但这没有帮助。

4

2 回答 2

2

我尝试使用您的代码,它可以在以后多次调用动画。我添加此代码以在旋转结束时调用:

..
                viewNeedle.startAnimation(anim);
                anim.start();
..

所以要制作一个重复的动画,它就像这样:

 @Override
            public void onAnimationEnd(final Animation animation)
            {
                viewNeedle.clearAnimation();
                viewNeedle.startAnimation(anim);
                anim.start();
            }

所以我可以不止一次调用那个动画..

希望对你有帮助..

于 2013-11-08T09:37:06.283 回答
2

问题是它没有在主线程上运行,为了修复它,我们使用了:

anim = new RotateAnimation( m_lastAngle,
                                sfAngle,
                                viewNeedle.getWidth() / 2,
                                viewNeedle.getHeight());
    anim.setDuration(1000);
    anim.setFillAfter(true);
    viewNeedle.setAnimation(anim);
    anim.setAnimationListener(new AnimationListener()
    {
        @Override
        public void onAnimationEnd(final Animation animation)
        {
            // viewNeedle.clearAnimation();
        }

        @Override
        public void onAnimationRepeat(final Animation animation)
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationStart(final Animation animation)
        {
            m_lastAngle = sfAngle;
        }
    });

    runOnUiThread(new Runnable()
    {

        @Override
        public void run()
        {
            viewNeedle.startAnimation(anim);
            findViewById(android.R.id.content).invalidate();
        }
    });

我们还使用了:

    final View viewNeedle = findViewById(R.id.needle);
    viewNeedle.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener()
            {
                @Override
                public void onGlobalLayout()
                {
                    scaleView(viewNeedle);
                }

                /**
                 * Scale a view.
                 * 
                 * @param viewToScale - View to scale.
                 */
                private void scaleView(final View viewToScale)
                {
                    int height = (int) (viewToScale.getHeight() / GraphicsPoint
                            .getScalePoint().y);
                    int width = (int) (viewToScale.getWidth() / GraphicsPoint
                            .getScalePoint().x);
                    final LayoutParams layoutParams = viewToScale
                            .getLayoutParams();
                    layoutParams.height = height;
                    layoutParams.width = width;
                    viewToScale.setLayoutParams(layoutParams);
                    viewToScale.getViewTreeObserver()
                            .removeOnGlobalLayoutListener(this);
                }
            });
于 2013-11-09T11:20:50.430 回答