android - How to get the MvxGridView to be efficient and performant? -
using xamarin , mvvmcross, i'm writing android application loading images album mvxgridview custom binding:
<mvxgridview android:id="@+id/grid_photos" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:numcolumns="3" android:verticalspacing="4dp" android:horizontalspacing="4dp" android:stretchmode="columnwidth" android:fastscrollenabled="true" local:mvxbind="itemssource allphotos" local:mvxitemtemplate="@layout/item_photo_thumbnail" />
which uses item_photo_thumbnail.axml:
<imageview local:mvxbind="picturepath photopath" style="@style/imageview_thumbnail" />
here binding class:
public class picturepathbinding : mvxtargetbinding { private readonly imageview _imageview; public picturepathbinding(imageview imageview) : base(imageview) { _imageview = imageview; } public override mvxbindingmode defaultmode { { return mvxbindingmode.oneway; } } public override type targettype { { return typeof(string); } } public override void setvalue(object value) { if (value == null) { return; } string path = value string; if (!string.isnullorempty(path)) { java.io.file imgfile = new java.io.file(path); if (imgfile.exists()) { // first decode injustdecodebounds=true check dimensions bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(imgfile.absolutepath, options); // calculate insamplesize options.insamplesize = calculateinsamplesize(options, 100, 100); // decode bitmap insamplesize set options.injustdecodebounds = false; bitmap mybitmap = bitmapfactory.decodefile(imgfile.absolutepath, options); _imageview.setimagebitmap(mybitmap); } } } protected override void dispose(bool isdisposing) { if (isdisposing) { var target = target imageview; if (target != null) { target.dispose(); target = null; } } base.dispose(isdisposing); } private int calculateinsamplesize(bitmapfactory.options options, int reqwidth, int reqheight) { // raw height , width of image int height = options.outheight; int width = options.outwidth; int insamplesize = 1; if (height > reqheight || width > reqwidth) { int halfheight = height / 2; int halfwidth = width / 2; // calculate largest insamplesize value power of 2 , keeps both // height , width larger requested height , width. while ((halfheight / insamplesize) > reqheight && (halfwidth / insamplesize) > reqwidth) { insamplesize *= 2; } } return insamplesize; } }
the problem i'm having is sluggish , slow. love each image load asynchronously. don't know how that. in .net (xaml), gridview control automatically (with virtualization), i'm realizing in android, might have manually handled?
can me this?
i working remote images, instead of local, have been using download cache plugin gives mvximageview class. in may give benefit.
so far experience android runs if foreground default, part. right now, of calculation code inside of binding class, going run in foreground.
what make run faster is:
- use observablecollection itemssource.
- kick off thread in start (or start) of view model add items observablecollection. can accomplish task.run()
- try process as possible in background thread each item before adding observablecollection
when updating observablecollection background thread, actual update has done on ui thread. done if using mvxviewmodel base view model.
this.invokeonmainthread(() => myobservablecollection.add(myitem) );
following pattern should windows based clients well.
Comments
Post a Comment