6

I'm using a ViewAnimator (parent class of ViewFlipper) to make some kind of interactive book. The hierarchy is roughly like this:

<ViewAnimator>
  <include layout="@layout/p0" />
  <include layout="@layout/p1" />
  <include layout="@layout/p2" />
  ...
</ViewAnimator>

So the pages of the book are inside the ViewAnimator. Each page has a FrameLayout with multiple layers of animated ImageViews, TextViews, Buttons... which turned out to be too much. Displaying a new page of the book via viewAnimator.showNext() can take seconds. All that time is spent in ImageView.onDraw() -Traceview.

-Will any layout alternative to ViewAnimator (ViewPager, other implementations of ViewGroup...) improve anything?

-Is there any way to preload the views a ViewFlipper/ViewAnimator/other will show next? (so that the next page of the book is always ready and happy in memory)

-Should I use Canvas or GLSurfaceView? (cannot use the animation framework)

I'm stuck... can you help?

slipbull
  • 1,477
  • 1
  • 11
  • 27
  • I’m trying to use ViewFlipper to mimic the behavior of Panorama Control in Windows Phone and there is the same issue (slow view flipping). How to improve ViewFlipper view switching? – anonim May 19 '12 at 23:56
  • I'd like to add that in case you want to skip to a specific view, instead of `showNext`, you can use what I made here: https://stackoverflow.com/a/55102182/878126 – android developer Mar 11 '19 at 12:48

2 Answers2

5

To improve the performance of the ViewFlipper, you can set

android:measureAllChildren="false"

in the xml. This is normally set to false for FrameLayout, but ViewAnimator enables it by default for some reason.

Setting it to false solved a very strange lag-issue for me.

dac2009
  • 3,521
  • 1
  • 22
  • 22
3

I can't answer all your questions, but I'll give you what I know since it is better than the lack of other answers. :P

ViewPagers load the views on either side by default. This speeds the switch, but might slow the view that is currently open. The number of preloaded views on a viewpager can be set using one of its methods.

If you don't want a ViewPager, you might try actually instantiating the views you want preloaded yourself, and simply setting them to invisible.

I am not experienced with the canvas or GL besides map overlays, but I expect that they COULD be much quicker/responsive/efficient if properly implemented since you wouldn't have any unneeded functionality. Might be messy though, depending on the source of your content.

RECOMMENDATION: Try the ViewPager, it is made for implementations like yours. If you are unsatisfied with it, roll up your sleeves and check into canvas stuff.

bhekman
  • 3,227
  • 21
  • 24