Android View.invalidate(Rect) different behavior b

2019-07-21 13:39发布

问题:

I have a custom view (a Waveform) that needs to be synchronized with a media player progress. The problem is that I'm having a different behavior when using invalidate(Rect) Here is the code that makes the update in my custom view:

public void setProgress(int currentDuration) {
    // ... Some code to calculate the dirty width
    invalidate(mDirtyWidth,0,mDirtyWidth+ mDirtyWidthRegion, viewHeight);
}

This is working perfectly on a Samsung Galaxy 4 (Android 4.4.2). Screenshot with "Show GPU view updates" enabled

But in a Nexus 7 (Android 5.1), the whole view gets invalidated each time:

回答1:

After Android 5.0, the canvas clipBounds always is your view bounds with hardwareAccelerated, the invalidate(dirty rect)'s dirty rect is ignored

5.0 ViewRootImpl.java http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.0_r1/android/view/ViewRootImpl.java/

            dirty.setEmpty();
            mBlockResizeBuffer = false;
            mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, this);

4.4 ViewRootImpl.java http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/android/view/ViewRootImpl.java/?v=source

            mCurrentDirty.set(dirty);
            dirty.setEmpty();
            attachInfo.mHardwareRenderer.draw(mView, attachInfo, this,
                    animating ? null : mCurrentDirty);