|
Preface:
Originally I was doing TV applications, but because the company wants a cell phone, staff nervous, so I transferred me to support it, I was told Lei Feng it! I made a phone function is to deal with the application of ICON, treatment is nothing more than beautify and re-synthesis and cutting the base plate, and uses a lot of knowledge of the Bitmap. Originally before always wanted to write something about Bitmap blog, this is just a chance so Bitmap knowledge blog series was born. Bitmap this series I will learn some knowledge of release for your reference and exchange.
Picture in your phone generally refers Bitmap images Bitmap why say it? Because everyone in the development of applications that they will use some pictures to show UI, users also like to look at pictures, see the text for information is too slow and not intuitive, if a good graphic design, basically do not watch to see pictures of your text on know what you want to express, for example, all shopping sites will edit many commodities with plans to be presented to the user, you can see in the picture application of common and important. As long as it comes to picture how to avoid OOM can not leave this topic, because when dealing with lots of pictures is prone OOM, then the learning image processing is particularly important, step by step, let us learn the relevant knowledge of the picture.
Bitmap image memory for computing:
Bitmap image loaded into memory when it is in accordance with: * width * height pixel bits to be calculated. You can put pictures as the line width, the column height of the matrix, each matrix element represents a pixel, each pixel is 1byte integer multiple of the data, the data, the more rich color representation, display the picture quality is higher. Bitmap has an enumeration class Config to configure the image compression format, each pixel is much data to store representatives, the greater the value the more color information can be stored, the more abundant, display it the better. Config.ALPHA_8 is 1 byte, Config.RGB_565 and Config.ARGB_4444 are 2 bytes, Config.RGB_565 no Alpha value to specify no transparency so multiple images, Config.ARGB_8888 is 4 bytes, are in accordance with the general picture of this to configure . The following is to get the configuration code:
static int getBytesPerPixel (Config config) {
if (config == Config.ARGB_8888) {
return 4;
} Else if (config == Config.RGB_565) {
return 2;
} Else if (config == Config.ARGB_4444) {
return 2;
} Else if (config == Config.ALPHA_8) {
return 1;
}
return 1;
}
You need to pay attention to what use the picture:
1, Android system own problems.
android system assigned to each application a certain memory space allocated depends on the number of manufacturers and models, numerical Runtime class can get, Runtime.getRuntime () Gets an instance, then () method to obtain the system can be assigned by APP maxMemory the maximum memory, totalMemory () Gets the currently allocated APP memory heap space, freeMemory () Gets the current available memory, when it is depleted will automatically expand, but not more than maxMemory. The figure below shows the minimum memory google official website provides different resolutions in different dpi allocated;
2, require much photos.
In fact, when a lot of pictures displayed on the phone need not be completely loaded into memory in the picture, such as my cell phone camera to take a picture is 4208 * 3120, loaded into memory in the memory is 52M, which is very scary two photos almost put your app to run out of memory. Under normal circumstances you need to load image processing, this process is mainly to reduce the image size, lower resolution, such as your controls display size is 100 * 100, and then you take pictures reduced to 100 * 100 .
3, the timely release of memory.
Before Android 2.3.3 (API level 10), Bitmap Bitmap pixel data and objects are stored separately, the pixel data is stored in native memory, the object is stored in the Dalvik heap, and native memory pixel data is not in a predictable the way to release, could cause the application to temporarily exceed the limit of its memory and crash, so Android2.3.3 (API 10) before you have to call recycle () method to freed memory to avoid OOM, of course, the premise is no longer sure that the bitmap use, otherwise there will be. "Canvas: trying to use a recycled bitmap" after Android3.0 (API 11), Bitmap Bitmap object pixel data and stored together in Dalvik heap, so we do not have to manually call recycle () to release Bitmap object to release the memory of all the garbage collector to do.
These are some of the basics of learning and Bitmap loaded into the phone memory matters that need attention, the next article I will write how to better load Bitmap, how to save memory, how efficient. |
|
|
|