One of the most efficient display images in android-loading a larger image

Source: Internet
Author: User
<span id="Label3"></p><p><p>Read a lot of articles on the internet, find or official documents to introduce the most detailed, the important things simple summary Out. See the Official document address (http://www.bangchui.org/read.php?tid=9) for Details.</p></p><p class="p1"><p class="p1"></p></p><p class="p1"><p class="p1">Displaying the picture in the app can easily cause the app to crash because of an exception "java.lang.OutofMemoryError:bitmap size exceeds VM budget" if you're not Careful. Loading images in Android requires some skill, mainly because:</p></p><p class="p1"><p class="p1">1. typically, device resources are limited, and Android devices allocate only 16M of space to each app. of course, many devices have higher memory settings for the application, and in section 3.7 of this document, the minimum memory (http://source.android.com/compatibility/downloads.html) required for different screen device applications is Described.</p></p><p class="p1"><p class="p1">2. Images usually account for Memory. For example Galxy Nexus Camera resolution up to 2592x1936, if the picture is configured to argb_8888 (default configuration after 2.3), loading a photo will require 19M of memory (2592x1936x4 bytes), will quickly consume the memory of an Application.</p></p><p class="p1"><p class="p1">3. In the Android ui, it is often necessary to load multiple images, such as Listview,viewpager.</p></p><p class="p1"><p class="p1"></p></p><p class="p1"><p class="p1">This article focuses on how to efficiently load large images without spilling your app's memory. Larger images are usually much larger than screen sizes, but loading a high-resolution picture does not give you much visual experience, and you can save memory by loading a compressed-sized picture in the next two steps.</p></p><p class="p1"><p class="p1">The first step is to read the picture size and Type.</p></p><p class="p1"><p class="p1">The Bitmapfactory class provides multiple decoding methods (decodebytearray (), decodefile (), decoderesource (), etc.) so that bitmap can be created from various data sources, These methods will request memory when the bitmap is constructed, which can easily lead to memory overflow for large graphs. Each decode method has an additional flag parameter bitmapfactory.options, which sets its Injustdecodebounds property to true to avoid allocating memory decode, and the returned bitmap object is a null value, but can get the original height of the picture , width, and Format (saved in the Outwidth,outheight,outmimetype of option). In this way, you can get the picture properties before you construct the Picture. Here is the sample code:</p></p><p class="p1"><p class="p1"></p></p><strong><strong>[java]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_1" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="zeroclipboardmovie_1" width="up" height="align=" "middle" bgcolor="#ffffff" data="/http static.blog.csdn.net/scripts/zeroclipboard/zeroclipboard.swf " type=" Application/x-shockwave-flash "><param name=" SRC " value=" http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf "><param name=" pluginspage " value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"> <param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always">< param name= "allowfullscreen" value= "false" ><param name="flashvars" value="id=1&width=18&height=18">< param name= "wmode" value= "transparent" > </object></object> <ol class="dp-j" start="1"> <ol class="dp-j" start="1"> <li class="alt">Bitmapfactory.options Options = <span class="keyword">new Bitmapfactory.options (); </span></li> <li>Options.injustdecodebounds = <span class="keyword">true; </span></li> <li class="alt">Bitmapfactory.decoderesource (getresources (), r.id.myimage, options);</li> <li><span class="keyword">int imageheight = options.outheight;</span></li> <li class="alt"><span class="keyword">int imagewidth = options.outwidth;</span></li> <li>String ImageType = options.outmimetype;</li> </ol> </ol><p><p></p></p><p class="p1"><p class="p1">To avoid memory overflow, it is a good idea to check the dimensions before loading the image unless you guarantee that the source image does not have a large image.</p></p><p class="p1"><p class="p1">The second step is to load the reduced image into Memory.</p></p><p class="p1"><p class="p1">Now that you know the size of the original picture, depending on the actual situation you want to load it down how many times after the Picture. For example, If you use a 128x96 imageview to display a 1024x768 image, there is no need to load the original read into Memory. Load a reduced image into memory, just set the insamplesize of the Bitmapfactory.options object to true, then set a value for Insamplesize (you can understand that Insamplesize is n, the picture shrinks to 1 /n size).</p></p><p class="p1"><p class="p1">Here is a sample code for loading a picture</p></p><p class="p1"><p class="p1"></p></p><strong><strong>[java]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_2" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_2" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="flashvars" value="id=2&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-j" start="1"> <li class="alt"><li class="alt"><span class="keyword">Public <span class="keyword">static <span class="keyword">int calculateinsamplesize (</span> </span></span></li></li> <li><li>bitmapfactory.options Options, <span class="keyword">int reqwidth, <span class="keyword">int Reqheight) {</span> </span></li></li> <li class="alt"><li class="alt"><span class="comment">//Raw Height and width of image</span></li></li> <li><li><span class="keyword">final <span class="keyword">int height = options.outheight; </span></span></li></li> <li class="alt"><li class="alt"><span class="keyword">final <span class="keyword">int width = options.outwidth; </span></span></li></li> <li><li><span class="keyword">int insamplesize = <span class="number">1; </span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">if (height > Reqheight | | width > Reqwidth) {</span></li></li> <li class="alt"><li class="alt"><span class="keyword">if (width > Height) {</span></li></li> <li><li>Insamplesize = Math.Round ((<span class="keyword">float) height/(<span class="keyword">float) reqheight); </span></span></li></li> <li class="alt"><li class="alt">} <span class="keyword">Else {</span></li></li> <li><li>Insamplesize = Math.Round ((<span class="keyword">float) width/(<span class="keyword">float) reqwidth); </span></span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>}</li></li> <li class="alt"><li class="alt"><span class="keyword">return insamplesize; </span></li></li> <li><li>}</li></li> </ol><p><p><br><br></p></p><p><p></p></p><p class="p1"><p class="p1"></p></p><strong><strong>[java]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_3" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_3" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="flashvars" value="id=3&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-j" start="1"> <li class="alt"><li class="alt"><span class="keyword">Public <span class="keyword">static Bitmap decodesampledbitmapfromresource (Resources res, <span class="keyword">int resId,</span> </span></span></li></li> <li><li><span class="keyword">int reqwidth, <span class="keyword">int Reqheight) {</span> </span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="comment">//first decode with injustdecodebounds=true to check dimensions</span></li></li> <li class="alt"><li class="alt"><span class="keyword">final Bitmapfactory.options Options = <span class="keyword">new Bitmapfactory.options (); </span></span></li></li> <li><li>Options.injustdecodebounds = <span class="keyword">true; </span></li></li> <li class="alt"><li class="alt">Bitmapfactory.decoderesource (res, resId, options);</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="comment">//Calculate insamplesize</span></li></li> <li><li>Options.insamplesize = calculateinsamplesize (options, reqwidth, reqheight);</li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="comment">//Decode bitmap with Insamplesize set</span></li></li> <li class="alt"><li class="alt">Options.injustdecodebounds = <span class="keyword">false; </span></li></li> <li><li><span class="keyword">return bitmapfactory.decoderesource (res, resId, options); </span></li></li> <li class="alt"><li class="alt">}</li></li> </ol><p><p>One of the most efficient display images in android-loading a larger image</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.