How to increase performance of ViewPager? - android

I want to know how to increase performance of ViewPager. I have ViewPager which has around 25 swipes with large images. I have done memory optimization to avoid OutOfMemoryError. Its working fine.But still there is a lag while swiping. I have set offscreen page limit to 2. If I increase offscreen page limit. It will load more pages in Memory and again I will end up with OOM. So any suggestion how to avoid lag while swiping?

Your ViewPager cannot cache more than 2 pages? It sounds like you need to tweak your fragments a bit more. For example, make sure you don't stretch the background image, and make sure that your layouts are as simple as possible (i.e. don't use unneeded layouts). If you can see how much memory your app is using for these two pages.
Once you verify that see if you can increase the offscreen page limit. Then check that you are properly destroying the fragments.

You can use FragmentStatePagerAdapter istead using FragmentPagerAdapter.
More detailed explanation given here.

Another Possible solution in case of images is that you can try putting them in drawable-hdpi folder, they will load faster. You can look at an answer here.

Related

improve layout performance

I am freelancing to solve crash of an android app. It happens on startup, and I know it is because the activity_main.xml. It has too many views, too many nesting levels. Apart from that (wich I will try to reduce)
what are other strategies to improve layout performance in Android?
The user interface cannot be changed or simplified.
For example:
- for repeated elements (50 - 100), does using 'include' makes it faster?
- the same for defining properties in styles instead of in every item.
I have been recently working on improving performance of app and renderd a fast smooth UI let me share you my experience :
The vision of performance in terms of UI is:
Lower the latency of screen draws
Create fast, consistent frame rates to avoid jank/lag.
And there are some thumb rules for layouts .
Minimum view hierarchy.
minimum background drawbles
minimizing overdraw of window :
minimizing overdraw of Views
using drawble left instead of image and textview aligned horizontally.
using lists/recyclerview very often when repetitive view
and Many more . let me share some links which are really helpful
https://medium.com/#elifbon/android-application-performance-step-1-rendering-ba820653ad3#.pp7hpnv07
https://www.safaribooksonline.com/library/view/high-performance-android/9781491913994/ch04.html
https://riggaroo.co.za/optimizing-layouts-in-android-reducing-overdraw/
https://www.hackerearth.com/notes/rendering-performance-in-android-overdraw/
Letme know if it's helpful. Cheers
You can try following from https://developer.android.com,
Optimizing Layout
Inspect Your Layout
Revise Your Layout
Use Lint
AS well as
Improving Layout Performance
Optimizing Layout Hierarchies
Re-using Layouts with
Loading Views On Demand
Re-using Layouts with
I have answered a related question before and my advice is still applicable in this topic.
Handling loads:
Lazy Loading - Load only those piece of information that is really needed by now. Example : A Movie app: Load only those latest movies rather than those old movies unless the user told you to do so. The idea here is that decided only on which is necessary to load first and load other else later on. The lesser the load the greater the performance is while without sacrificing a good content.
Caching - If you keep on downloading things from your server chances are it might take too long to load and your splash screen will be visible for longer period of time or some of your UI might freeze specially when you do it in the Main thread. With caching you will need to fetch fewer data from the network since you've downloaded some of them already.
Things to consider in creating views.
Avoid a super deep nested views.
Avoid a deep nested weights.
For image loading use some popular library like Picasso, Glide etc.
does using 'include' makes it faster?
A bit, Reusing layouts is particularly powerful as it allows you create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text. It also means that any elements of your application that are common across multiple layouts can be extracted, managed separately, then included in each layout. So while you can create individual UI components by writing a custom View, you can do it even more easily by re-using a layout file.
Source : Official docs
Addendum
When things are still wrong and the Android framework doesn't provide what you really need; the last approach is to create your own view via extending View or ViewGroup. Creating your own view/layout requires time and much effort but you gain more controls since it is your own view/layout implementation, let say you have the power to change the world.

Large Gridview 1100/1100 on android

I have to show 64/64 bitmaps in a grid of 1100/1100 but the scrolling is not smooth. What is the optimum way in displaying such ammount of image data?
This is just too much memory consumption for an android app.. I don't think you really want to load all of those at the same time. I would rethink the requirements first.
There should be a solution that still satisfies the user and does not load that many views into memory. I don't know what is the use-case here, but I would suggest implementing something like paging. Load a limited amount of items, and only if user wants to see more, then show progress bar and load more.
E.g. you can load more if user scrolls to the last item, or just create a button like "Show more". I'm not sure which solution will be okay for your app. Also, I would actually load new items into the same GridView replacing previously loaded bitmaps, because each of them requires a lot of memory. You can always control the scrolling position programmatically if needed.

Can we change the setOffscreenPageLimit() of ViewPager dynamically in Android?

I am using FragmentStatePagerAdapter along with ViewPager to hold my fragments. User will have one image by default and he can keep adding upto 10. When I add 10 pages, I am seeing lots of OutOfMemory issues at different places in my app code, most of them at while creating bitmaps. Also the background images are not getting loaded so I see balck bakground. I currently have setOffscreenPageLimit(10). Due to this all my fragments and it's views (I have many imageviews, textview etc in every fragments) are held in memory. It works fine even at this stage. But if I swipe through the pages few times or run any other feature which needs bitmaps to be created (which eventually need RAM) then it sucks. Sometimes it leads to ANR as it could not space to create even thread or crash due to OOM.
Could you give me an idea, if you've used already, that if I can randomly change the setOffscreenPageLimit to lower value so that ViewPager will clear the rest of the fragments?
-I am thinking of changing the screen limit in onTrimLowMemory() in my Activity.Thinking I can handle any memory level - TRIM_MEMORY_RUNNING_MODERATE, _LOW or _CRITICAL.
I also got an idea to decide the number of off screen pages based on the available which I get through getMemoryClass() of ActivityManager.
If I can change setOffscreenPageLimit value dynamically, how can I let ViewPager know that it is been modified.
I read in the articles that android:largeHeap is of not that useful if device offered RAM is already low.Though it is allowed, it is not a good idea at all.
Enabling these bits in ViewPageer would help ?
PERSISTENT_NO_CACHE - Used to indicate that no drawing cache should be kept in memory.
Or any other suggestion to avoid OOMs, especially in the context of ViewPager with FragmentStatePagerAdapter?
I could get these points triggered from this http://developer.android.com/training/articles/memory.html#Android and from other articales from stackoverflow related to memory.
Spare few minutes and throw some light.

How did they make such layout - which elements they used?

At the moment, I am using Euro 2012 app and it has some interesting layout parts. If you go to matches->knockout screen, you'll see 3 screens like this.
If you slide left or right, you move between them not like between one activity to another, but just like all these elements are laid on one big canvas and you slide each screen into focus. Even thou, if you slide left/right, the screen cannot be stopped in the way that half of each image is visible, so I guess it's not a big canvas. Somehow you can slide screen like one big image, and yet it always lock perfectly as if they used 3 activities for this.
How did the do this?
It's a ViewPager, available in Android support package and described in their blog.
The designing is not so much difficult, As i think to draw that boxes showing quarterfinals, semifinals.
They having layout( we can do by placing image in background also).
The major role played by FrameLayout which shows you that effect(emphasis), the flages are fetch from url as matches are decided(not essentially images only names are sufficient as flags came inside app and gets applied as updated).
After your click i think the start image gets change it's just replacing that image with new one.
As you said they are not on single canvas (but if they did customization of heir Viewpager that it's not stopping in between activities), but i think it's on single canvas.
If your taking about quickness of loading and all it's up to you as much code and processing you optimized it gives you smooth feel and fast processing.
We can appreciate the work but we can not say it's too much difficult as i think it's quit easy...
Hope this explanation helps you to understand ....

Display huge tiled image on android phone

I have a very large image (a map) that I need to display. I already have the image in "tiled" format - 256x256 pieces.
Also I got tiles for several "zoom" levels.
At the moment the issue is to display the deepest zoom level, where you'd have really a lot of tiles.
For example, a medium sized map will contain 4 rows and 26 columns of tiles for deep level.
I tried approaching the problem using a 2 dimensional scroll view and image views inside it - 1 per tile.
The problems is that it crashes. When I try displaying 4 rows and 20 columns it doesn't crash, obviously it's a memory issue.
So the question here - how to display all that, taking into account limited phone RAM.
I do understand there should be a way to dealocate memory for images that are out of sight, and only display those which are currently in visible area of the scroll view, but I don't know how to do that.
Would be happy to hear any clues or maybe there's alternative approach to these things.
Thanks.
I think you might better use the grid view instead of arrays of scroll view (but I am not sure if it support side/updown scroll at the same time.
And then in your adapter, override the getView method. There you can control the recycling of your images.
The project I am doing also have issues with image and RAM, what I am basically doing is like:
image.recycle();
System.gc();
I tried doing the above stuff like 50fps with the image is like 800x400x32bit and still not running into out of memory issue. But if I take away the System.gc(), it crash immediately.

Categories

Resources