Skip to content
This repository has been archived by the owner on Feb 22, 2022. It is now read-only.

feature: Offscreen pages caching functionality #4

Closed
quintos opened this issue Oct 27, 2012 · 8 comments
Closed

feature: Offscreen pages caching functionality #4

quintos opened this issue Oct 27, 2012 · 8 comments
Assignees
Milestone

Comments

@quintos
Copy link

quintos commented Oct 27, 2012

Hi,

I think it would be a nice addition to add some sort of offscreen pages cache like the way a Android ViewPager/PagerAdapter work. Right now i have pages that contain an image that needs to be fetched from the internet so i need to be able to pre-instantiate those pages 1/more pages ahead and also keep 1/more previous pages to be able to flip back easily.

I have taken the liberty of trying something out and used some code from the ViewPager and PagerAdapter source but i cannot get it to work correctly yet but is more to illustrate what i mean.
Maybe you want to have a look at it. (It's not working correctly: after some flipping forward and backwards, the pages repeat and indexes are wrong. haven't figured out yet what the problem is.)

I forked your project here:
https://github.com/murimons/android-flip

thnx

@openaphid
Copy link
Owner

FlipViewController.sideBufferSize is designed to control the number of preloaded pages. The default value is 1, which means 1 previous page, the current page and 1 next page are loaded after flipping to a page.

Unfortunately in your scenario, the lazily loaded content may not be visible during animation as the screenshot of the page was took before they're ready.

I think we may need an API to notify FlipViewController to update the screenshot on demand.

@quintos
Copy link
Author

quintos commented Oct 30, 2012

Indeed.

Do you think you can make it re-use Bitmaps?

@ghost ghost assigned openaphid Nov 6, 2012
openaphid pushed a commit that referenced this issue Nov 6, 2012
Remove an unused field in ViewDualCards
@openaphid
Copy link
Owner

@murimons I implemented a rough solution. The key idea is to use FlipViewController.refreshPage(View pageView) to make the animation reload content of an updated page.

The codes are developed in a new branch: feature/api-content-update

Please let me know if it meets your requirement. I'll merge it back to the main branch later

@openaphid openaphid reopened this Nov 6, 2012
openaphid pushed a commit that referenced this issue Nov 6, 2012
For #4
Update the APK file
openaphid pushed a commit that referenced this issue Nov 8, 2012
@openaphid
Copy link
Owner

Two new methods are added to support refreshing a page manually:
FlipViewController.refreshPage(View pageView)
FlipViewController.refreshPage(int pageIndex)

@quintos
Copy link
Author

quintos commented Nov 13, 2012

Excelent!

This meets my requirement indeed!

question:

In the docs you say that for an active page this would be an expensive operation. Do you mean a page that is being viewed at that moment or flipped? So when i flip to a page which is still in the middle of lazy loading my image and when that finishes (and i'm still looking at it) , i refresh the screenshot, this would be a heavy operation? in terms of memory?

thanks!

@openaphid
Copy link
Owner

An active page is the one of which content is involved in animation. You can treat the current page and its next one be the active pages as both of them are needed to render the animation.

The refreshing routine is no more heavier than manually flipping page. It triggers the following operations for the active pages:

  1. Makes the page draw in a bitmap in the UI thread to get a screenshot. (The memory consumed is corresponding to the size of the page, but normally it would be fine and it doesn't trigger large GC pause as bitmap doesn't store data in VM heap.)
  2. Binds the bitmap to an OpenGL texture in GL thread, and recycles it.

The first step is "heavy" as it asks the page to re-draw itself, which could be a bit slow for a very complex view hierarchy.

My suggestion in the doc is a note for the potential cost. For example, if you are rendering a view animation in a page, then it better not to call refreshPage() for each frame of the animation.

@quintos
Copy link
Author

quintos commented Nov 15, 2012

I understood that It doesn't store pixels in the heap but the Bitmap memory does get accounted against the heap. So if a refresh allocates a native Bitmap it also uses up the memory in the Java heap. But GC could still be fast because the Bitmap object in the heap is small. (Is GC speed related to object size?)

I'm still having issues with Bitmaps in my app causing OOM exceptions because of this (Not related to android-flip). I'm now trying to find a way to re-use Bitmap objects.

@openaphid
Copy link
Owner

The native memory occupied by bitmap instances can lead to OOM as the VM budget is constrained by the sum of heap memory and native memory.

Please check out gclog.py if you want to check the real memory usage of your app.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants