Android Flip View动画是生涩的

时间:2015-04-30 02:07:26

标签: android animation

这是一个奇怪的。我有一个动画,将视图翻转90度,然后另一个视图旋转另一个90度,允许更改图像。这适用于三星Tab 3;但是在Tab 4上,动画非常生涩。如果我打开'配置文件GPU渲染' - '在屏幕上显示为条形图'动画变得非常流畅。

我已将调试日志放入动画中,并且我发现applyTransformation函数被调用的次数相同,当它是生涩的并且它是平滑的时。所以我可以把它归结为屏幕没有足够的刷新。任何想法都会很棒。

这是翻转动画。     公共类AnimatedFlip扩展了Animation     {         私人最终浮动mFromDegrees;         私人最终浮动mToDegrees;         私人最终浮动mCenterX;         私人最终浮动mCenterY;         私人相机mCamera;

    public AnimatedFlip(float aFromDegrees, float aToDegrees, float aCenterX, float aCenterY)
    {
        mFromDegrees = aFromDegrees;
        mToDegrees = aToDegrees;
        mCenterX = aCenterX;
        mCenterY = aCenterY;
    }

    @Override
    public void initialize(int aWidth, int aHeight, int aParentWidth, int aParentHeight)
    {
        super.initialize(aWidth, aHeight, aParentWidth, aParentHeight);
        mCamera = new Camera();
    }

    @Override
    protected void applyTransformation(float aInterpolatedTime, Transformation aTransformation)
    {
        float degrees = mFromDegrees + ((mToDegrees - mFromDegrees) * aInterpolatedTime);

        final Matrix matrix = aTransformation.getMatrix();

        mCamera.save();
        mCamera.rotateY(degrees);
        mCamera.getMatrix(matrix);
        mCamera.restore();

        matrix.preTranslate(-mCenterX, -mCenterY);
        matrix.postTranslate(mCenterX, mCenterY);
    }
}

动画以此调用开始。

mFlipStartingView.setVisibility(View.VISIBLE);
applyRotation(0, 90, mFlipStartingView);

这是执行动画的功能。

private void applyRotation(float aStart, float aEnd, final ImageView aView)
{
    // Find the center of image
    float centerX = aView.getWidth() / 2.0f + aView.getX();
    float centerY = aView.getHeight() / 2.0f + aView.getY();

    // Create a new 3D rotation with the supplied parameter
    // The animation listener is used to trigger the next animation
    final AnimatedFlip rotation = new AnimatedFlip(aStart, aEnd, centerX, centerY);
    rotation.setDuration(mFlipAnimationTime);
    rotation.setFillAfter(false);
    rotation.setInterpolator(new LinearInterpolator());
    rotation.setAnimationListener(new Animation.AnimationListener()
    {
        @Override public void onAnimationStart(Animation animation)
        {
        }

        @Override public void onAnimationEnd(Animation animation)
        {
            if (aView != mFlipView)
            {
                mFlipStartingView.setVisibility(View.INVISIBLE);
                mFlipView.setVisibility(View.VISIBLE);
                applyRotation(-89, 0, mFlipView);
            }
        }

        @Override public void onAnimationRepeat(Animation animation)
        {
        }
    });
    aView.startAnimation(rotation);
}

1 个答案:

答案 0 :(得分:0)

所以我添加了一个Hack来使这个工作在Tab4上。在应用旋转功能结束时,我添加了以下代码。

    // hack for the Tab 4
    new CountDownTimer(mFlipAnimationTime * 2, 33)
    {
        @Override public void onTick(long millisUntilFinished)
        {
            mFlipView.refreshDrawableState();
            mFlipView.invalidate();
            mFlipStartingView.refreshDrawableState();
            mFlipStartingView.invalidate();
        }

        @Override public void onFinish()
        {
            mFlipView.refreshDrawableState();
            mFlipView.invalidate();
            mFlipStartingView.refreshDrawableState();
            mFlipStartingView.invalidate();
        }
    }.start();

这可确保动画视图至少在1/30秒内更新。