First of all, I generally use the adapter is Baseadapter, of which two methods are the most important, namely: Getcount,getview, when the ListView is optimized, first use Convertview and viewholder mates to optimize, The use of Convertview is the control reuse, which adds to reduce the use of memory, the use of viewholder is to reduce the number of FindByID. But after the control, in the image loading, there will be a picture dislocation problem, This is because the control has the last remaining picture in it, so we need to set a default diagram at the time of initialization, clear the last remaining image, so as to avoid image dislocation. Note: In the process of image loading generally cause OOM (memory overflow), this time we need to use a level three cache policy for processing, first level three cache is: 1: Strong reference (Lrucache) 2: Soft Reference (softrenfence)3:SD Card (now being Disklrucache) first when we need to load the picture: first from the strong reference inside, if the strong reference is not, from the soft reference inside to find, if the soft reference is not from the SD card inside, if the SD card is not loaded from the network, By loading the requested image into a strong reference (Lrucache). Second: Strong reference generally take the memory of One-fourth, when the strong reference is full, after the Lrucache algorithm to tell part of the picture deleted, while depositing into the soft reference inside, when the soft reference is full, will be part of the picture into the SD card inside, When the SD card is full, half of the deletion, (when deleted according to the date of the order), Note: Strong references cannot be reclaimed by the garbage collection mechanism, soft references can be recycled at any time by the garbage collection mechanism.: Half of the time we loaded the URL of the picture, is the server after two samples of the URL , only load the original image when you click on a larger view.: No matter in the strong reference, soft reference or SD card when we save the picture is a key value of the form of the pair, his key is the URL, in general, using MD5 to encrypt the URL, the purpose is to avoid the URL has illegal characters . Two sampling: Note: Two sampling points two, the first step we need to obtain the width and height of the picture, at the same time get to the need to sample the width and height, through the width and height of the sample ratio we need to calculate, the second step: by sampling ratio let our pictures really achieve sample scaling . case as follows: First step:If I have a picture is 200*200, then I want to show the thumbnail of this picture on a 50*50 ImageView , then my compression ratio should be 4, ( This is the first step of the operation, I first load the image boundary into memory, this load operation will not consume much memory ) Step Two:On the basis of the first sampling, I'll take two samples. At two samples, I passed the results of the first sample as a parameter to the bitmapfactory, so that when the picture is loaded the system will not load the entire picture, but will only load a thumbnail image of the image come in, so not only to improve the loading rate, It also greatly saves memory, and for the user, he will not have visual differences How the specific code is implemented: First sampling : get New Bitmapfactory.options () first to get the option object , injustdecodebounds Set toTrueOnly loads the border of the picture in, and does not load the specific pixel point of the picture,PassBitmapfactory.decodefile (FilePath, Options);To load the picture for the first time,PassOutwidthThe width of the original,PassOutheightGet the original high,The next step is to get a sample rate by the width of the control and the height of the controlSampleSize,Next, the second sampling settingInjustdecodeboundsForFalseSet the zoom ratio at the same time,Set upInpreferredconfigPicture formats for pictures ( alpha_8,rgb_565,argb_4444,argb_8888), and finally loading of pictures . Specific implementation code:bitmapfactory.options Options = new Bitmapfactory.options ();///First: When set to true, just get the boundary, that is, the width is highoptions.injustdecodebounds=true;Bitmap Bitmap = Bitmapfactory.decodefile (File.getabsolutepath (), options);///second time: Set the value of options to config.rgb_565, which will reduce the memory by half compared to the default config.argb_8888;options.inpreferredconfig= Bitmap.Config.RGB_565;//Reduce the border to the original width and height of the 1/100;Options.insamplesize=math.max (options.outwidth,options.outheight)/100;//set to False, indicating that not only the bounding box is loaded,Options.injustdecodebounds=false;
Bitmap=bitmapfactory.decodefile (File.getabsolutepath (), options)
Android Listview Performance Optimization