为什么硬件加速不能在我的View上运行?

时间:2014-09-07 18:01:54

标签: android animation android-animation hardware-acceleration facebook-messenger

我正在使用Facebook's Rebound library复制其聊天头实现中看到的有弹性的动画。问题是,大多数时候动画都会断断续续。一些图片将更好地解释这一点。这是黄油般流畅的聊天头像:

Facebook Messenger

这是我的尝试(注意白色View的动画几乎所有帧都跳过了):

Stuttering animation

偶尔会顺利运作:

Smooth animation

下面是我目前正在使用的代码(如果你想快速设置,整个项目是up on Github)。我猜这与我的View无法正确启用硬件加速有关。我Spring中有SpringSystem个,一个用于“气泡”(Android图标),另一个用于内容(点击气泡时显示的白色View)。如何解决这个问题的任何帮助将不胜感激。感谢。

AndroidManifest.xml

    <application android:hardwareAccelerated="true" ...>
        ...
    </application>

AppService.java

    // the following code is in AppService#onCreate()
    // AppService extends android.app.Service
    // full code at https://github.com/vickychijwani/BubbleNote

    mContent.setLayerType(View.LAYER_TYPE_HARDWARE, null);

    final Spring bubbleSpring = system.createSpring();
    bubbleSpring.setCurrentValue(1.0);
    bubbleSpring.addListener(new SpringListener() {
        @Override
        public void onSpringUpdate(Spring spring) {
            float value = (float) spring.getCurrentValue();
            params.x = (int) (mPos[0] * value);
            params.y = (int) (mPos[1] * value);
            mWindowManager.updateViewLayout(mBubble, params);
            // fire the second animation when this one is about to end
            if (spring.isOvershooting() && contentSpring.isAtRest()) {
                contentSpring.setEndValue(1.0);
            }
        }

        // ...
    });

    final Spring contentSpring = system.createSpring();
    contentSpring.setCurrentValue(0.0);
    contentSpring.addListener(new SpringListener() {
        @Override
        public void onSpringUpdate(Spring spring) {
            // always prints false?!
            Log.d(TAG, "hardware acc = " + mContent.isHardwareAccelerated());
            float value = (float) spring.getCurrentValue();
            // clamping is required to prevent flicker
            float clampedValue = Math.min(Math.max(value, 0.0f), 1.0f);
            mContent.setScaleX(value);
            mContent.setScaleY(value);
            mContent.setAlpha(clampedValue);
        }

        // ...
    });

1 个答案:

答案 0 :(得分:10)

我已经通过浏览框架源代码来解决这个问题。

TL; DR :手动将WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED附加到View / Window时,将WindowManager添加到布局标记中。在清单中设置android:hardwareAccelerated=true将无效。


我是manually attaching my View to the WindowManager(因为我需要在Service中创建我的用户界面以模拟聊天标题),如下所示:

    // code at https://github.com/vickychijwani/BubbleNote/blob/eb708e3910a7279c5490f614a7150009b59bad0b/app/src/main/java/io/github/vickychijwani/bubblenote/BubbleNoteService.java#L54
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    mBubble = (LinearLayout) inflater.inflate(R.layout.bubble, null, false);
    // ...
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    // ...
    mWindowManager.addView(mBubble, params);

我们去挖掘......

欢迎使用Android框架

我开始在View#draw(...)进行调试,然后将调用堆栈调到ViewRootImpl#draw(boolean)。在这里,我遇到了这段代码:

    if (!dirty.isEmpty() || mIsAnimating) {
        if (attachInfo.mHardwareRenderer != null && attachInfo.mHardwareRenderer.isEnabled()) {
            // Draw with hardware renderer.
            mIsAnimating = false;
            mHardwareYOffset = yoff;
            mResizeAlpha = resizeAlpha;

            mCurrentDirty.set(dirty);
            dirty.setEmpty();

            attachInfo.mHardwareRenderer.draw(mView, attachInfo, this,
                    animating ? null : mCurrentDirty);
        } else {
            // If we get here with a disabled & requested hardware renderer, something went
            // wrong (an invalidate posted right before we destroyed the hardware surface
            // for instance) so we should just bail out. Locking the surface with software
            // rendering at this point would lock it forever and prevent hardware renderer
            // from doing its job when it comes back.
            // Before we request a new frame we must however attempt to reinitiliaze the
            // hardware renderer if it's in requested state. This would happen after an
            // eglTerminate() for instance.
            if (attachInfo.mHardwareRenderer != null &&
                    !attachInfo.mHardwareRenderer.isEnabled() &&
                    attachInfo.mHardwareRenderer.isRequested()) {

                try {
                    attachInfo.mHardwareRenderer.initializeIfNeeded(mWidth, mHeight,
                            mHolder.getSurface());
                } catch (OutOfResourcesException e) {
                    handleOutOfResourcesException(e);
                    return;
                }

                mFullRedrawNeeded = true;
                scheduleTraversals();
                return;
            }

            if (!drawSoftware(surface, attachInfo, yoff, scalingRequired, dirty)) {
                return;
            }
        }
    }

在我的情况下ViewRootImpl#drawSoftware()被调用,它使用软件渲染器。嗯......这意味着HardwareRenderernull。所以我去寻找HardwareRenderer的构建点, // Try to enable hardware acceleration if requested final boolean hardwareAccelerated = (attrs.flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0; if (hardwareAccelerated) { // ... mAttachInfo.mHardwareRenderer = HardwareRenderer.createGlRenderer(2, translucent); // ... } 位于ViewRootImpl#enableHardwareAcceleration(WindowManager.LayoutParams)

FLAG_HARDWARE_ACCELERATED

啊哈!这是我们的罪魁祸首!

回到手头的问题

在这种情况下,即使我在清单中设置了Window,Android也不会为此android:hardwareAccerelated=true自动设置 mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); mBubble = (LinearLayout) inflater.inflate(R.layout.bubble, null, false); // ... final WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, // NOTE WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); // ... mWindowManager.addView(mBubble, params); 。所以修复只是:

{{1}}

虽然动画仍然不像Facebook那样流畅。我想知道为什么......(在任何人问之前:不,在动画期间没有丰富的日志;是的,我尝试过发布版本)