Efficient loading of Android development bitmap
Last Update:2016-08-14
Source: Internet
Author: User
<span id="Label3"></p><p><p>The Bitmapfactory class provides four classes of Methods: decodefile, decoderesource, decodestream, and Decodebytearray<br><br>Used to support the loading of a bitmap object from a file system, a resource, an input stream, and a byte array, and the first two indirectly called the Decodestream<br><br>To avoid oom, you can load pictures efficiently by sampling rate<br><br>What if the sampling rate is obtained?<br><br>1. Set the parameter of the Bitmapfactory.options injustdecodebounds to True and load the picture<br><br>2. Remove the original width height of the picture from the bitmapfactory.options, which correspond to the outwidth and Outheight parameters<br><br>3. Calculate the sample rate based on the rule of the sampling rate and the desired size of the target view Insamplesize<br><br>4. Set the parameter of the Bitmapfactory.options Injustdecodebounds to false and reload the picture<br><br>The above steps are illustrated by Code:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>Bitmap Decodesampledbitmapfromresource (Resource res,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>resId,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>reqwidth,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Reqheight) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>Bitmapfactory.options Options =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">bitmapfactory.options (); Options.injustdecodebounds</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; Bitmapfactory.decoderesource (res, resId, options); Options.insamplesize</span></span>=<span style="color: #000000;"><span style="color: #000000;">calculateinsamplesize (options, reqwidth, reqheight); Options.injustdecodebounds</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">bitmapfactory.decoderesource (res, resId, options);}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Calculateinsamplesize (bitmapfactory.options Options,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>reqwidth,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Reqheight) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Height =<span style="color: #000000;"><span style="color: #000000;">options.outheight; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>width =<span style="color: #000000;"><span style="color: #000000;">options.outwidth; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Insamplesize = 1<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(height > Reqheight | | width ><span style="color: #000000;"><span style="color: #000000;">Reqwidth) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Halfheight = HEIGHT/2<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Halfwidth = WIDTH/2<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>((halfheight/insamplesize) >= reqheight && (halfwidth/insamplesize) >=<span style="color: #000000;"><span style="color: #000000;">Reqwidth) {insamplesize</span></span>*= 2<span style="color: #000000;"><span style="color: #000000;">; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">insamplesize;}</span></span></pre><p><p></p></p><p><p>Efficient loading of Android development bitmap</p></p></span>