Android Navigation (Introduction) page Viewpager Memory Overflow workaround

Source: Internet
Author: User
Tags set background

Now basic every Android app will standard a launch introduction page, or do product introduction, or do the app function display, since the navigation interface, the basic idea is to use Viewpager implementation. (Image from UI China designer design picture)

Just these days in the navigation interface, my test phone is Meizu MX3, after the test is OK, there is no obvious lag phenomenon, but when I put the debug apk installed on the meter 3 and the charm of the blue, the program run to this navigation interface will immediately crash off, Occasionally there is no crash also will appear obvious lag phenomenon, experience effect is very poor.
Open the memory manager of Android studio to view the runtime allocated memory, then I was forced, 180+m around, on this navigation interface with 180M, also do what Android ...
Changed a lot of places, the effect is still not obvious, check the data have said another adapter inherit from Pageradapter, the method is as follows:

1 classGuideadapterextendspageradapter{2         PrivateList<view>Views ;3         Private FinalLinkedlist<view> recylebin=NewLinkedlist<>();4          PublicGuideadapter (list<view>Views ) {5              This. views=Views ;6         }7 @Override8          Public intGetCount () {9             returnviews.size ();Ten         }    One @Override A          Public Booleanisviewfromobject (View view, Object object) { -             returnview==object; -         }    the @Override -          Public voidDestroyitem (ViewGroup container,intposition, Object object) { - Container.removeview (views.get (position)); -         }    + @Override -          Public intgetitemposition (Object object) { +             return Super. GetItemPosition (object); A         }    at @Override -          PublicObject Instantiateitem (ViewGroup container,intposition) { -             //Set background picture here, increase loading speed, solve oom problem - view view; -             intCount=GetCount (); -             if(!Recylebin.isempty ()) { inview=Recylebin.pop (); -}Else { toview=Views.get (position); +Viewgroup.layoutparams params =NewViewgroup.layoutparams ( - ViewGroup.LayoutParams.MATCH_PARENT, the ViewGroup.LayoutParams.MATCH_PARENT); *View.setbackgroundresource (images[position%Count]); $ view.setlayoutparams (params);Panax Notoginseng             } -Container.addview (view,0); the             returnViews.get (position); +         } A}

Initialize the time to let Viewpager adapter for the above custom adapter can, the basic code is as follows:

1 PrivateViewpager Mviewpager;2     PrivateGuideadapter Mguideadapter;3     Private int[] images;//Displays the ID value of the described picture4     PrivateArraylist<view>Views ;5     PrivateImageview[] Indicator=NULL;//the following navigation indicators, which do not introduce too much here, can be ignored6 7     ...8     ...9     //control initializes the businessTenMviewpager=(Viewpager) Findviewbyid (R.id.viewpager); OneViews=NewArraylist<>(); A     //This puts the ID value of the five picture described here -images=New int[]{r.drawable.bg_1,r.drawable.bg_2,r.drawable.bg_3,r.drawable.bg_4,r.drawable.bg_5} -     //Adapter Instantiation theMguideadapter=NewGuideadapter (views); -     //cycle the business of adding pictures -      for(inti=0;i<images.length;i++){ -ImageView mimageview=NewImageView ( This); +     //The following step will cause oom, so add backgroundresource steps in the custom adapter -     //implemented in the Instantiateitem method, annotated here +     //Mimageview.setbackgroundresource (Images[i]); AViews.add (Mimageview);//added to the dynamic array, where the imageview inside are imageview with no background at     //There is also an indicator addition within the For loop that ignores the business code in the focus of the discussion issue -     } -     //bind adapter for Mviewpager - Mviewpager.setadapter (mguideadapter); -Mviewpager.setcurrentitem (0); -Mviewpager.setoffscreenpagelimit (1); inMviewpager.setonpagechangelistener ( This); -     ... to...

The basic idea is to let the load viewpager too many images not displayed in the custom adapter Destroyitem destroy the release of memory, to prevent oom or memory overflow crash problems, This method only loads the current page and the front and back pages for three pages each time the Viewpager is loaded, so the memory footprint is not very high.

But, however, the problem comes ... After rewriting the test found: Te me or occupy 160m+ memory, then I was silly ... How does not see the effect ... So for a long time are looking for information, find problems, Debug ...
However, half a day has passed.
I still haven't found out where the problem is.
Later, the tutor and I said to see how big your picture, I said 100k look, then he looked at, said let me drag all the pictures into the PNG and then export (always jpg). So do, by the way, look at the new exported image size, looked at a bit larger than the JPG, so the dubious replacement of resources, recompile after the run. Finally surprised to find that the memory footprint has dropped to 40-60m ... Originally is the picture format question, hey, had a half-day head all big, originally is so simple question ...
This also horizontal hint we later Android programming resources in the best not to use JPG format, download the picture resources of the class is also, because the 32-bit PNG color transition smooth and support transparency, JPG is a pixel compressed image, quality has dropped, PNG compression algorithm decompression much faster, JPG can have a high compression ratio (of course there is a loss), so it is better to consider PNG (except for special needs).

Here also thank my mentor, in my development taught me a lot of, the first blog, that is, as their own notepad, in the future can see, but also hope to solve similar problems, the first write, there will be no expression in place, express the unclear place, I hope to leave your comments and suggestions, I can also correct, improve their own.

(This blog belongs to my original, I csdn article original address: http://blog.csdn.net/wiz_chen/article/details/45119783)

Android Navigation (Introduction) page Viewpager Memory Overflow workaround

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.