Android Hundred Day Program: efficient loading large pictures

Source: Internet
Author: User

Question: If the picture is large, all loaded into memory, and the display is not big, then the large picture will not improve the visual effect, and will consume unnecessary memory.

The solution is based on the actual needs of the picture, and then dynamically calculate how much the image should be loaded, but because it is not likely that the size of the picture and the actual need for the same size, it is necessary to load the image size of a 2 of the value of a square, rather than the actual size required.


, load a thumbnail size of 100*100


Create a new project,

Refer to the method on Google: Http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap

Set up a class to call the function in which the picture resource is processed, all the code is as follows:

Package Bill.su.loadbitmap;import Android.content.res.resources;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;public class Bitmaputils {public static int calculateinsamplesize ( Bitmapfactory.options options,int reqwidth, int reqheight) {//Raw height and width of imagefinal int height = options.out height;final int width = Options.outwidth;int insamplesize = 1;if (height > Reqheight | | width > reqwidth) {final in  T halfheight = height/2;final int halfwidth = width/2;//Calculate The largest insamplesize value that is a power of 2 and//keeps both//height and width larger than the requested height and width.while ((halfheight/insamplesize) > re qheight&& (halfwidth/insamplesize) > Reqwidth) {insamplesize *= 2;}} return insamplesize;} public static Bitmap Decodesampledbitmapfromresource (Resources res,int resId, int reqwidth, int reqheight) {//First decod E with injustdecodebounds=true to check dimensionsfinal bitmapfactory.options Options = nEW bitmapfactory.options (); options.injustdecodebounds = true; Bitmapfactory.decoderesource (res, resId, options);//Calculate insamplesizeoptions.insamplesize = Calculateinsamplesize (options, reqwidth,reqheight);//Decode bitmap with insamplesize setoptions.injustdecodebounds = False;return Bitmapfactory.decoderesource (res, resId, Options);}}

XML code for the main interface:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= "bill.su.loadbitmap.MainActivity" >    <imageview        android:id= "@+id/pandaimageview "        android:layout_width=" 100px "        android:layout_height=" 100px "        android:layout_alignparentleft=" true "        android:layout_alignparenttop= "true"/></relativelayout>

Use ImageView to display pictures

Add code to the main interface's logic code:

@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); ImageView ImageView = (ImageView) Findviewbyid (R.id.pandaimageview); Imageview.setimagebitmap (Bitmaputils.decodesampledbitmapfromresource (Getresources (), R.drawable.panda, 100, 100)) ;}
Notice how the R.drawable.panda came. As long as you add a picture named Panda in the Drawable folder of the Res folder in the project folder, the ID is displayed in the Eclipse Refresh project. It doesn't matter if you don't have drawable this folder, just create a new folder for yourself.

If the picture is not displayed, it is likely that the picture resource does not exist, so the item will not prompt the error, directly not displayed.


Look at the project structure and know how to build the project:


Here the main learning code is the code in Bitmaputils, so it is encapsulated, and later can be used as one of their own resource class call.




Android Hundred Day Program: efficient loading large pictures

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.