Android Art--bitmap efficient load and Cache code Analysis (2)

Source: Internet
Author: User

Bitmap load and Cache code analysis:

Compression of pictures

For example, there is a 1024x768 pixel image to be loaded into memory, but eventually you need to use the size of the image is actually only 128*96, then we will waste a large part of memory, which is obviously not necessary, this is an example:

public static int Calculateinsamplesize (
Bitmapfactory.options Options, int reqwidth, int reqheight) {
Raw height and width of image
Final int height = options.outheight;
Final int width = options.outwidth;
int insamplesize = 1;

if (Height > Reqheight | | width > reqwidth) {

Final int halfheight = HEIGHT/2;
Final int halfwidth = WIDTH/2;

Calculate the largest insamplesize value is a power of 2 and keeps both
Height and width larger than the requested height and width.
while ((halfheight/insamplesize) > Reqheight
&& (halfwidth/insamplesize) > Reqwidth) {
Insamplesize *= 2;
}
}

return insamplesize;
}
In order to use the above method, we must remember to use Injustdecodebounder to get the original size before cutting, and remember to set the Injustdecodebounds to False when you are finished using it:

public static Bitmap Decodesampledbitmapfromresource (Resources res, int resId,
int reqwidth, int reqheight) {

First decode with injustdecodebounds=true to check dimensions
Final Bitmapfactory.options Options = new Bitmapfactory.options ();
Options.injustdecodebounds = true;
Bitmapfactory.decoderesource (res, resId, options);

Calculate insamplesize
Options.insamplesize = calculateinsamplesize (options, Reqwidth, reqheight);

Decode Bitmap with Insamplesize set
Options.injustdecodebounds = false;
Return Bitmapfactory.decoderesource (res, resId, options);
}

After the trimming is done, we can load the bitmap into the ImageView, for example, we set the desired size to 100*100:

Mimageview.setimagebitmap (
Decodesampledbitmapfromresource (Getresources (), R.id.myimage, 100, 100));

Memory Caching Technology
Another way to cache images is in memory cache technology. In Android, there is a class called LRUCache that is specifically designed to handle image caching.
It has a feature that when the cached image reaches a predetermined value, the least recently used images are recycled.
Steps: (1) to set the memory size of the cached image, I set this to 1/8 of the phone memory,
How to get the phone memory: int maxmemonry = (int) (Runtime.getruntime (). MaxMemory ()/1024);
(2) The key-value pairs inside the LRUCache are URLs and corresponding images.
(3) Rewrite a method called sizeof, which returns the number of pictures.

Private lrucache<string, bitmap> Mmemorycache;
Private Lrucacheutils () {
if (Mmemorycache = = null)
Mmemorycache = new lrucache<string, bitmap> (
MAXMEMONRY/8) {
@Override
protected int sizeOf (String key, Bitmap Bitmap) {
Override this method to measure the size of each picture, returning the number of pictures by default.
return Bitmap.getrowbytes () * bitmap.getheight ()/1024;
}

@Override
protected void Entryremoved (Boolean evicted, String key,
Bitmap OldValue, Bitmap newvalue) {
LOG.V ("tag", "Hard cache are full, push to soft cache");

}
};
}
(4) The following methods are to empty the cache, add pictures to the cache, get pictures from the cache, remove from the cache.
Removing and clearing the cache is something that must be done, because the image cache is improperly handled to report a memory overflow, so be sure to pay attention.
public void ClearCache () {
if (Mmemorycache! = null) {
if (mmemorycache.size () > 0) {
LOG.D ("Cacheutils",
"Mmemorycache.size ()" + mmemorycache.size ());
Mmemorycache.evictall ();
LOG.D ("Cacheutils", "mmemorycache.size ()" + mmemorycache.size ());
}
Mmemorycache = null;
}
}

Public synchronized void Addbitmaptomemorycache (String key, Bitmap Bitmap) {
if (mmemorycache.get (key) = = null) {
if (key! = null && bitmap! = null)
Mmemorycache.put (key, bitmap);
} else
LOG.W (TAG, "The res is aready exits");
}

Public synchronized Bitmap Getbitmapfrommemcache (String key) {
Bitmap BM = mmemorycache.get (key);
if (key! = null) {
return BM;
}
return null;
}

/**
* Remove Cache
*
* @param key
*/
Public synchronized void Removeimagecache (String key) {
if (key! = null) {
if (Mmemorycache! = null) {
Bitmap BM = mmemorycache.remove (key);
if (BM! = NULL)
Bm.recycle ();
}
}
}

Android Art--bitmap efficient load and Cache code Analysis (2)

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.