android's setimageresource() documentation states:
this bitmap reading , decoding on ui thread, can cause latency hiccup. if that's concern, consider using setimagedrawable(android.graphics.drawable.drawable) or setimagebitmap(android.graphics.bitmap) , bitmapfactory instead.
i'm looking solve exact issue in example application. think i'm supposed "read between lines" here , and replace call :
((imageview) rootview.findviewbyid(r.id.screen_image)).setimageresource(imageid);
to instead call
inputstream = this.getresources().openrawresource(imageid); bitmap imagebitmap = bitmapfactory.decodestream(is); ((imageview) rootview.findviewbyid(r.id.screen_image)).setimagebitmap(imagebitmap);
but within thread, using asynctask
-
am correct in understanding, or there simpler solution?
update:
it looks there entire section on displaying bitmaps efficiently on developer.android.com - i'm investigating now.
the simplest way solve issue create worker thread described in thread & process documentation. mentions, async task can used replace more complex code. final solution change code in oncreateview()
method, replacing original laggy code:
final view rootview = inflater.inflate(r.layout.fragment_screen, container, false); int = getarguments().getint(arg_panel_number); string panel = getresources().getstringarray(r.array.panel_array)[i]; int imageid = getresources().getidentifier(panel.tolowercase(locale.getdefault()), "drawable", getactivity().getpackagename()); ((imageview) rootview.findviewbyid(r.id.screen_image)).setimagedrawable(getresources().getdrawable(imageid));
with new background thread:
final view rootview = inflater.inflate(r.layout.fragment_screen, container, false); // load image in separate thread ensure navigation drawer animation smooth. // replace async task if necessary new thread(new runnable() { public void run() { int = getarguments().getint(arg_panel_number); final string panel = getresources().getstringarray(r.array.panel_array)[i]; int imageid = getresources().getidentifier(panel.tolowercase(locale.getdefault()), "drawable", getactivity().getpackagename()); inputstream = getactivity().getresources().openrawresource(imageid); final bitmap imagebitmap = bitmapfactory.decodestream(is); rootview.post( new runnable() { public void run() { ((imageview) rootview.findviewbyid(r.id.screen_image)).setimagebitmap(imagebitmap); getactivity().settitle(panel); } }); } }).start();
as described in documentation above, using async task more extensible , maintainable if code grows large.
Comments
Post a Comment