Android image development story 1

Source: Internet
Author: User

Android image development story 1

I was originally a TV app, but my company was nervous because I had to get out of my cell phone, so I called it Lei Feng! One of my functions is to process the app ICON in the mobile phone. Processing is nothing more than beautifying the app, merging and cropping the app with the baseboard again, and using a lot of Bitmap knowledge. I have always wanted to write some blogs about Bitmap before. This is an opportunity, so those blogs of Bitmap were born. In this series, I will publish some knowledge about Bitmap for your reference and communication.

In mobile phones, images generally refer to Bitmap images. Why do we need Bitmap? When developing applications, you will use some images to express the UI. Users also like to view pictures. Reading text is too slow and not intuitive, you can see what you want to express without looking at your text. For example, all shopping websites will edit a lot of product photos to present them to users, it can be seen that images are common and important in applications. When talking about images, you can't leave the topic of how to avoid OOM. Because OOM is easy to appear when processing many images, it is especially important to learn image processing, next, let's learn about images step by step.

Bitmap image memory usage calculation:

When a Bitmap image is loaded into the memory, it is calculated based on the number of pixels in width * height. You can think of an image as a matrix composed of rows in width and height. Each matrix element represents a pixel, and each pixel is 1 byte integer multiple of the data. The larger the data, the richer the color, the higher the image quality. Bitmap has an enumeration class Config used to configure the compression format of the image, which indicates how much data is used for each pixel to store. The larger the value, the more color information it can store, and the richer it will be, the better the display effect. Config. ALPHA_8 is 1 byte, Config. RGB_565 and Config. ARGB_4444 is both 2 bytes, Config. RGB_565 has no Alpha value, so it is used to configure images without transparency. the ARGB_8888 is 4 bytes. Generally, the image is configured according to this parameter. The following code obtains the Configuration:

?
1 2 3 4 5 6 7 8 9 10 11 12 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; }


What do you need to pay attention to when using images:

1. Problems with the android system. The android system allocates a certain amount of memory for each application. The amount of memory allocated depends on the manufacturer and model. The value can be obtained through the Runtime class. getRuntime () gets the instance, and then obtains the maximum memory allocated by the system for the APP through the maxMemory () method. totalMemory () gets the heap space allocated by the APP, freeMemory () obtain the available memory. It will automatically expand when it is exhausted, but will not exceed maxMemory. Minimum Memory allocated by dpi for different resolutions provided on the google website;

 

 

2. How big a photo is needed. Many images do not need to be fully loaded into the memory when they are displayed on the mobile phone. For example, if my mobile phone camera takes a photo of 4208x3120, the memory used for loading to the memory is 52 MB, which is terrible. The two photos almost consume your app memory. Generally, you need to process the image to be loaded. This processing mainly reduces the image size and resolution. For example, your control displays the size of 100*100, then you need to reduce the image size to 100*100.

 

3. Release the memory in time. Before Android 2.3.3 (API level 10), Bitmap pixel data and Bitmap objects are stored separately, pixel data is stored in native memory, and objects are stored in Dalvik heap, pixel data in native memory is not released in a foreseeable way, which may cause the application to temporarily exceed its memory limit and crash, so in Android2.3.3 (API 10) you must call the recycle () method to release the memory to avoid OOM. Make sure that this bitmap is no longer used. Otherwise, Canvas: trying to use a recycled bitmap will appear. after Android3.0 (API 11), the pixel data of Bitmap and the Bitmap object are stored in Dalvik heap. Therefore, we do not need to manually call recycle () to release the Bitmap object, the memory is released by the garbage collector.

 

The above are some basic knowledge of Bitmap and precautions for loading it into the mobile phone memory. In the next blog, I will write how to better load Bitmap, how to save memory, and how to improve efficiency. Thank you for your browsing. If you have any questions, please leave a message or contact me more quickly. Please add the coder_online public account. Not only is my article here, there are also various technologies shared by my friends. Let's take a look at them and we look forward to your coming. You can also scan the following QR code and click duang to find us .......

 

 

Related Article

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.