Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Android memory optimization of the optimal load Bitmap     - Configure the Linux kernel and use iptables to do port mapping (Linux)

- Thinking in Java study notes - initialization and cleanup (Programming)

- How to remove the files inside the privacy of data on Linux (Linux)

- ARM assembler instruction debugging method (Programming)

- sqlplus connect to Oracle (Database)

- Selection sort, insertion sort, and Shell sort (Programming)

- Java thread pool: ExecutorService, Executors (Programming)

- Ubuntu 14.04 compile and install Apache (Server)

- TPCC-MySQL Benchmark (Database)

- Linux user login and IP restrictions (Linux)

- Two minutes thoroughly so that you understand Android Activity Lifecycle (Programming)

- Disable unnecessary services under Linux (Linux)

- Linux system firewall defense network attacks (Linux)

- MySQL Tutorial: Building MySQL Cluster under Linux (Database)

- Three easy open source firewall on the Linux platform (Linux)

- Linux Getting Started tutorial: hard disk partition and to deal with traps (Linux)

- Detailed software to run UnixBench (Linux)

- Enterprise Hadoop cluster architecture - NFS installation (Server)

- Oracle set the timer task JOB scheduler to execute stored procedures or PL / SQL code block (Database)

- Linux system package manager (rpm, yum, source packages installation) (Linux)

 
         
  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.
     
         
       
         
  More:      
 
- Make full use of the Raspberry Pi SD card space (Linux)
- Close and limit unused ports computer server security protection (Linux)
- to install the deployment of LVS under CentOS 7.0 (Server)
- MySQL performance comparison of large amounts of data storage (Database)
- error no.2013 lost connection Tom with SQLServer during query (Database)
- Source code to compile and install MySQL 5.7.9 (Database)
- GEC2440 easiest a kernel compile Linux2.6.30.4 (Programming)
- Squid proxy server (Server)
- CentOS-based Kickstart automated installation practice (Linux)
- [Android] Eclipse does not update the Android SDK Manager solution [using GoAgent] (Programming)
- Security Knowledge: How to hide a backdoor PHP file tips (Linux)
- Linux file permissions to modify the command: chmod (Linux)
- Erase do with HTML5 Canvas and diffusion effect (Programming)
- Ubuntu users to install Gnome-Pie 0.6.5 (Linux)
- Django 1.8 TEMPLATE_DIRS configuration and STATICFILES_DIRS (Server)
- Usage Linux device tree (Programming)
- sqlplus connect to Oracle (Database)
- OpenResty load balancing MySQL (Database)
- MongoDB start under Linux (Database)
- Spring AOP for logging (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.