Efficient loading of Android development bitmap

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.