In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to obtain the relevant knowledge of the size of Bitmap at run time, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to obtain Bitmap at run time. Let's take a look.
First, how to get the size of Bitmap at run time?
Get the actual amount of memory needed for the picture
Public final int getByteCount () {if (mRecycled) {return 0;} return getRowBytes () * getHeight ();}
The amount of memory actually occupied by the picture
Because there is a reuse scene, for example, the size of 400KB has been opened up to display picture A, now we need to display picture B of 300KB, and picture A does not need to be displayed, then the previously developed 400KB may be used to display picture B, so there is no need to re-open up new memory, so picture B actually occupies memory 400KB.
Public final int getAllocationByteCount () {if (mRecycled) {return 0;} return nativeGetAllocationByteCount (mNativePtr);} II. The source of the picture
Pictures in Assets
The files in asstes are basically the same as the images we loaded from SDCard, so I won't explain them separately.
For example: let's take a png image with a width and height of 112px * 131px pixels as an example (the file size is 20kb). If we load it into memory in the following way:
BitmapFactory.decodeStream (assets.open ("test.png"))
What is actually shown is that if it is loaded into memory in ARGB_8888 format, the final size is 112 '131' 4 = 58688KB. In fact, it is the total number of pixels that our picture needs to display multiplied by the memory occupied by a single pixel, because ARGB_8888, you can see that the four channels of a pixel need 8 bits to describe, so a pixel needs 4 bytes of memory, so the total number of pixels multiplied by 4 is the total memory occupied by this picture.
So, it means that the memory size of the image is not only the pixel size, but also related to the format loaded into memory. For example, if we replace the above image with RGB_565 to load, then the memory we get should be: 112 '131 * [(5' 6'5) / 8] = 112 '131' 2 = 29344 bytes, which is fully half the size before.
Pictures in the Drawable directory
When we put the images in different drawable directories, we finally show whether the actual pixels occupied are scaled to a certain extent, or take the 112*131px images as an example:
To put it this way, if you put the above image into the hdpi directory and then load it into memory with ARGB_8888, the memory size algorithm is as follows:
(112 / 1.5) × (131 / 1.5) × 3 × 4 = 77256 bytes
Note that if you put it in-nodpi, the image will not be scaled in different DPI devices, but will only be displayed in 112x131pixels, so it will always take up the same memory in different phones.
Third, optimize the memory volume occupied by pictures.
Use inSampleSize to sample and compress pictures
Use matrix changes to change the size of the picture
Use RGB_565 to load non-transparent pictures
Use 9-patch pictures as background images
Use VectorDrawable
You can use no pictures, no pictures.
This is the end of the article on "how to get the size of Bitmap at run time". Thank you for reading! I believe you all have a certain understanding of "how to get the size of Bitmap at run time". If you want to learn more, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.