android - ViewPager animation outOfMemoryError -


i have 5 slidingtabs. want place viewpager animation in 1 of them.

fragment class:

public class adfragment extends fragment {  int[] mresources = {         r.drawable.six,         r.drawable.five,         r.drawable.four,         r.drawable.eight,         r.drawable.one,         r.drawable.two,         r.drawable.three,         r.drawable.seven,         r.drawable.nine,         r.drawable.ten, }; int currentpage; public adfragment() {     // required empty public constructor } timer swipetimer;   @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     // inflate layout fragment     view view = inflater.inflate(r.layout.fragment_ad, container, false);     custompageradapter mcustompageradapter = new custompageradapter(getactivity(),mresources);      final viewpager mviewpager = (viewpager) view.findviewbyid(r.id.pager);     mviewpager.setadapter(mcustompageradapter);      final handler handler = new handler();     final runnable update = new runnable() {         public void run() {             if (currentpage == mresources.length) {                 currentpage = 0;             }             mviewpager.setcurrentitem(currentpage++, true);         }     };      swipetimer = new timer();     swipetimer.schedule(new timertask() {          @override         public void run() {             handler.post(update);         }     }, 500, 3000);      return view; } 

}

adapter class:

class custompageradapter extends pageradapter {  context mcontext; layoutinflater mlayoutinflater; int[] mresources; private resources resource;  public custompageradapter(context context,int[] mresources) {     mcontext = context;     mlayoutinflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service);     this.mresources = mresources; }  @override public int getcount() {     return mresources.length; }  @override public boolean isviewfromobject(view view, object object) {     return view == ((linearlayout) object); }  @override public object instantiateitem(viewgroup container, int position) {     view itemview = mlayoutinflater.inflate(r.layout.pager_item, container, false);      imageview imageview = (imageview) itemview.findviewbyid(r.id.imageview);     imageview.setimageresource(mresources[position]);      container.addview(itemview);      return itemview; }  @override public void destroyitem(viewgroup container, int position, object object) {     container.removeview((linearlayout) object); } @override public object instantiateitem(view view, int position) {     imageview myview = new imageview(mcontext);      bitmap bitmap = bitmapfactory.decoderesource(mcontext.getresources(), mresources[position]);     myview.setimagebitmap(bitmap);      ((viewpager) view).addview(myview);     return myview; } } 

at moment have 10 images in slideshow.when open tab fragment described above, wait period, try go tab , again - get:

    01-27 14:13:53.794 12652-12652/igor.com.totalsoulbeta e/androidruntime: fatal exception: main                                                                     process: igor.com.totalsoulbeta, pid: 12652                                                                     java.lang.outofmemoryerror                                                                         @ android.graphics.bitmapfactory.nativedecodeasset(native method)                                                                         @ android.graphics.bitmapfactory.decodestream(bitmapfactory.java:587)                                                                         @ android.graphics.bitmapfactory.decoderesourcestream(bitmapfactory.java:422)                                                                         @ android.graphics.drawable.drawable.createfromresourcestream(drawable.java:840)                                                                         @ android.content.res.resources.loaddrawable(resources.java:2113)                                                                         @ android.content.res.resources.getdrawable(resources.java:700)                                                                         @ android.content.res.xresources.getdrawable(xresources.java:521)                                                                         @ android.support.v4.content.contextcompat.getdrawable(contextcompat.java:323)                                                                         @ android.support.v7.widget.tintmanager.getdrawable(tintmanager.java:175)                                                                         @ android.support.v7.widget.tintmanager.getdrawable(tintmanager.java:168)                                                                         @ android.support.v7.widget.appcompatimagehelper.setimageresource(appcompatimagehelper.java:51)                                                                         @ android.support.v7.widget.appcompatimageview.setimageresource(appcompatimageview.java:72)                                                                         @ igor.com.totalsoulbeta.custompageradapter.instantiateitem(custompageradapter.java:46) 

the size of each image not more 150kb. question is: possible perfome such animation 25 images without outofmemoryerror? if yes, need change in code? how can clear heap , need that? if it's no best way of animating, please, advise me variant

thanks in advance!

a viewpager should able show number of pages. @ time has 3 pages in memory (unless change offscreen page limit). probleme may lie timer leaking something. documentation says :

when timer no longer needed, users should call cancel(), releases timer's thread , other resources. timers not explicitly cancelled may hold resources indefinitely.

does if add in fragment ? :

@override public void ondestroyview() {     super.ondestroyview();      swipetimer.cancel();     swipetimer = null; } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -