android - What is the use of decoding bitmapfactory twice? -


i came across on developer's website on loading large bitmaps efficiently tutorial.

public static bitmap decodesampledbitmapfromresource(resources res, int resid,     int reqwidth, int reqheight) {      // first decode injustdecodebounds=true check dimensions     final bitmapfactory.options options = new bitmapfactory.options();     options.injustdecodebounds = true;     **bitmapfactory.decoderesource(res, resid, options);**      // calculate insamplesize     options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight);      // decode bitmap insamplesize set     options.injustdecodebounds = false;     return bitmapfactory.decoderesource(res, resid, options); } 

my question point of decoding resources first time can set insamplesize , decoding it.

this common strategy when downloading images.

since never want download image higher resolution can displayed, , since android quite difficult when comes memory management, system allows first assess size image going be, , when download, can control how down-sampling want.

down-sampling, put, means how many pixels you're going skip. example, downsampling of 1 no reduction. downsampling of 2 however, skip every other pixel both horizontally , vertically, resulting in bitmap of half width , half height, , 1 quarter memory.

if you'll @ code:

final bitmapfactory.options options = new bitmapfactory.options();     options.injustdecodebounds = true;     **bitmapfactory.decoderesource(res, resid, options);** 

what's happening here when calling "decoderesource", you're passing options object injustdecodebounds = true. telling bitmapfactory not load image pixels, instead, decode bounds of image - cheaper operation. when this, result of bitmapfactory null, parameters of options (outwidth, outheight) have valid values describing width / height of image. this, can compute sample size want, , download actual image, size optimal application.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -