Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize Bitmap resources

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will give you a detailed explanation on how to optimize Bitmap resources. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Optimization reason

That is, why the image Bitmap resources should be optimized, as shown below:

two。 Optimization direction

This article will optimize the use of image Bitmap resources & memory management from the following aspects:

3. Specific optimization scheme

Next, I will explain in detail the specific optimization scheme for each optimization direction.

Next, I will explain each of the above schemes in detail.

3.1 release picture resources after use

Optimization reason: if the picture resources are not released after use, it is easy to cause memory leakage and lead to memory overflow.

Optimization scheme:

a. Before Android2.3.3 (API 10), call the Bitmap.recycle () method

b. After Android2.3.3 (API 10), use soft reference (SoftReference)

Description: before and after Android2.3.3 (API 10), Bitmap objects-their pixel data are stored in different locations, resulting in different ways to release image resources, as shown below:

Note: if you call Bitmap.recycle () and then draw Bitmap, there will be a Canvas: trying to use a recycled bitmap error.

3.2 adapt & scale the picture according to the resolution

Optimization reason: if the resolution of Bitmap does not match the resolution of the current device, the Bitmap will be stretched, and when the resolution of Bitmap increases, the memory occupied will increase accordingly, mainly because the memory footprint of Bitmap increases according to the size of x and y.

Optimization scheme:

3.3 choose the appropriate decoding method as needed

Optimization reason: the memory footprint of different image decoding methods varies greatly, as follows

Pay special attention to:

1. The higher the number of bitmap bits, the more color information it can store, and the more realistic the image is.

two。 In general, ARGB_8888 is used, which occupies the most memory = 1 pixel and 4 bytes

3. Example: if there is a picture of 480x800 in the format = ARGB_8888, it will take up the memory of 1500KB.

Optimization scheme: choose the appropriate decoding method according to the requirements. Note that:

1. Use parameters: BitmapFactory.inPreferredConfig settin

two。 Decoding method is used by default: ARGB_8888

3.4 set up picture cache

Optimization reason: repeatedly loading image resources consumes too many resources (CPU, memory & traffic)

Optimization scheme

This is the end of the article on "how to optimize Bitmap resources". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report