Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Android memory optimization of the optimal load Bitmap     - Install RAID 6 (Striping double distributed parity) (Linux)

- Linux md5sum verify file integrity (Linux)

- MySQL management partition table (Database)

- Understanding and Memcached MongoDB arbitration node, Zookeeper, Redis Recovery Programme Thoughts (Database)

- Linux (Debian) install software, missing dynamic link libraries .so (Linux)

- Windows 8.1 hard drive to install Ubuntu 14.04 dual system reference tutorials and multi-drive Precautions (Linux)

- The oh-my-zsh into true my zsh (Linux)

- How to clear the DNS query cache under Linux / Unix / Mac (Linux)

- Linux initialization init systems - Systemd (Linux)

- SYN attack hacker attack and defense of the basic principles and prevention technology (Linux)

- Compression decompression command under Linux (Linux)

- Use install_updates upgrade GAMIT / GLOBK (Linux)

- Linux Learning --- disk partition / relational representation + mount (Linux)

- TeamCity continuous integration in the Linux installation (Linux)

- CentOS 7 Change Hostname (Linux)

- Ubuntu firewall installation and configuration (Linux)

- Linux scheduling summary (Linux)

- The best tools and techniques to find data on a Unix system (Linux)

- Linux System Getting Started Tutorial: how to find information on Linux-embedded module (Linux)

- Java rewrite the hashcode method (Programming)

  Android memory optimization of the optimal load Bitmap
  Add Date : 2018-11-21      
  An article on the Android Bitmap memory footprint calculation and loading considerations, wrote Bitmap Bitmap basics and use the precautions you need to know, I will write in this Android app Bitmap creation and loading.

BitmapFactory use:

When it comes to image loading must say BitmapFactory, see the name to know his role, is a production Bitmap factory, below is some of the factory method:

See BitmapFactory can use the array to store data Bitmap, Bitmap resource ID, Bitmap files as a data source to create a Bitmap object, look at the specific circumstances of your data source program which is provided. These methods for each data source provides two methods, there is need to look at BitmapFacotry.Options argument, which is very important, because he was able to greatly reduce your consumption of memory.

BitmapFacotry.Options of inJustDecodeBounds parameters:

To conserve memory, in many cases to go through the original image scaling processing, according to the size of the control handle to the corresponding size of the picture, then use BitmapFactory create Bitmap, in many cases will use the following code:

BitmapFactory.Options options = new BitmapFactory.Options ();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource (getResources (), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

Note that in the above options.inJustDecodeBounds = true inJustDecodeBounds parameters, in order to avoid my translation is inaccurate here I first posted the original google:

If set to true, the decoder will return null (no bitmap), but the out ... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.

I will say it is not to the pixel area of the bitmap memory allocation at decode time, in addition to other information this distinction you can get to the Bitmap. So there is a lot of sense, you have neither consume memory and get a picture of information for you to help deal with the next image.

BitmapFacotry.Options of inSampleSize parameters:

Back You have to get the original picture size, the next step is to take the picture scaled to the size you need, can be set by inSampleSize parameters, google original explanation:

If set to a value> 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width / height of the original, and 1/16 the number of pixels Any value < = 1 is treated the same as 1. Note:. the decoder will try to fulfill this request, but the resulting bitmap may have different dimensions that precisely what has been requested. also, powers of 2 are often faster / easier for the decoder to honor. (no English documents Kan Bukan I still want to google the original stickers, my English is rotten, the translation may not be accurate)

This probably means is that you can adjust the memory parameters when you needed to decode the original image, a bit like sampling rate, will lose some of the pixel value is greater than 1, when a power of 2 is more conducive to operations. For example: When inSampleSize == will return 4:00 a size (length and width) is 1/4 of its original size, the pixel is 1/16 of the original picture. This value is how to calculate it?

public static int calculateInSampleSize (
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    finalint height = options.outHeight;
    finalint width = options.outWidth;
    int inSampleSize = 1;

    if (height> reqHeight || width> reqWidth) {

        finalint halfHeight = height / 2;
        finalint 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)> reqHeight
                && (HalfWidth / inSampleSize)> reqWidth) {
            inSampleSize * = 2;

    return inSampleSize;

In the first set, when decode options.inJustDecodeBounds = true, and then get to the picture parameter set to false, this is the skill decode time, put the following complete code stickers can be used as a tool method:

public static Bitmap decodeSampledBitmapFromResource (Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds = true to check dimensions
    finalBitmapFactory.Options options = newBitmapFactory.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;
    returnBitmapFactory.decodeResource (res, resId, options);

The above method from google official website, no need to make changes, this is the programmer ism it, the key is to know why so write. Here is a method I wrote can be directly used as tools.

    / **
    * For image compression, mainly to solve the control to display image memory for causing too much OOM problem, the general compressed image size and its control should be used to display a similar size.
    * @param Context Context
    * @param ResId image asset Id
    * @param ReqWidth desired width compression
    * @param ReqHeight desired compression height
    * @return Compressed image
    * /
    public static Bitmap compressBitmapFromResourse (Context context, int resId, int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options ();
        / *
        * When the first analysis, inJustDecodeBounds set to true,
        * Do not allocate memory for the bitmap, although bitmap return value is empty, but you can get the image size
        * /
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource (context.getResources (), resId, options);

        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height> reqHeight || width> reqWidth) {
            final int heightRatio = Math.round ((float) height / (float) reqHeight);
            final int widthRatio = Math.round ((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio heightRatio: widthRatio;?
        options.inSampleSize = inSampleSize;
        inSampleSize using the calculated values // parsing picture again
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource (context.getResources (), resId, options);

These are loaded into memory Bitmap some tips on Android, we are not in the future be able to apply it well, because to avoid loading image caused such a problem OOM it? In addition to technical image loading, image cache for an application is also very important, the next article will explain the Android memory optimization of the memory cache.
- php security study (Linux)
- 20 Unix / Linux command skills (Linux)
- Oracle row and column switch to turn columns (Database)
- CentOS7 install NTFS-3G driver (Linux)
- How to add and delete bookmarks in Ubuntu (Linux)
- How to install Hadoop on CentOS7 (Server)
- To install network scanning and packet sniffer tool Nmap 7.00 under ubuntu (Linux)
- Vim simple configuration (Linux)
- Unity Greeter Badges: the lost session icon back to the login screen Ubuntu (Linux)
- Deep understanding of C # generics (Programming)
- Using shell users or virtual users to login to pureftpd (Linux)
- Grading defense against Linux server attacks (Linux)
- Four levels of intrusion on Linux server and counter-measures (Linux)
- Workspace Go language and environment variables GOPATH (Linux)
- Linux hybrid character device (Linux)
- how to quickly find a file on Linux system (Linux)
- Python extension module Ganglia 3.1.x (Linux)
- Ten to improve the efficiency of the Linux bash tricks (Linux)
- MySQL can not write the data keyword conflicts (Database)
- xCAT deployment (Linux)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.