几次setAnimation调用后Android视图轮换没有发生

前端之家收集整理的这篇文章主要介绍了几次setAnimation调用后Android视图轮换没有发生前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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();
}

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

最佳答案
问题是它没有在主线程上运行,修复我们使用的:

anim = new RotateAnimation( m_lastAngle,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);
                }
            });
原文链接:https://www.f2er.com/android/430955.html

猜你在找的Android相关文章