?? In Android app development, we often need to deal with pictures, and pictures a very troublesome problem is the use of memory is very large, often lead to oom, understand bitmap related information, different SDK version of the Android image processing changes, And some of the ways to optimize the processing of our normal development in the picture will be very helpful.
?? Before we begin the content of this section, we'll start by distinguishing the concepts of several nouns:
Drawable: General-purpose graphical objects for loading images in commonly used formats, such as png,jpg images, as well as the 13 drawable types of visual objects that you learned before! We can understand it as a picture-frame for painting!
Bitmap (bitmap): We can think of him as an easel, we first put the picture above, and then we can do some processing, such as the acquisition of image file information, do rotary cutting, zoom out and other operations!
Canvas: As its name, canvas, we can paint (draw), you can use Paint (brush), to draw various shapes or write, but also can use path (path) to draw multiple points, and then connect to a variety of graphics!
Matrix: Used for graphic effects processing, color matrix (ColorMatrix), and the use of matrix for image translation, zooming, rotation, tilt, etc.!
I. Bitmap related APIs
About Bitmap,bitmapfactory,bitmapfacotry.options
1.Bitmap
Bitmap Common methods:
Common methods Public Boolean Compress(Bitmap.compressformat format,intQuality, OutputStream stream) compresses the bitmap to the specified OutputStream, which can be understood as saving the bitmap to a file! Format: formats, png,jpg, etc. quality: compression quality, 0-100,0 for minimum quality compression, 100 maximum mass (PNG lossless, ignores quality setting) stream: The output stream return value represents whether the stream is successfully compressed to the specified flow!void Recycle(): Reclaim the memory space occupied by the bitmap and mark the bitmap as deadBoolean isRecycled(): Determines whether the bitmap memory has been disposedint getwidth(): Gets the width of the bitmapint getheight(): Gets the height of the bitmapBoolean ismutable(): Whether the picture can be modifiedint Getscaledwidth(Canvas canvas): Gets the width of the image after the specified density conversionint Getscaledheight(Canvas canvas): Gets the height of the specified density converted image static method: BitmapCreateBitmap(Bitmap src): Create a non-new image with Src as the original BitmapCreatescaledbitmap(Bitmap SRC,intDstwidth,intDstheight,BooleanFilter): Creates a new image with Src as the original, specifying the aspect of the new image and whether it changes. BitmapCreateBitmap(intWidthintHeight, config config): Creates a bitmap of the specified format and size bitmapCreateBitmap(Bitmap source,intXintYintWidthintHeight) creates a new picture with source, specifying the starting coordinates and the height of the new image. Public StaticBitmapCreateBitmap(Bitmap source,intXintYintWidthintHeight, Matrix m,BooleanFilter
2.BitmapFactory
?? The construction method of bitmap is private and cannot be instantiated outside, only through JNI instantiation! Of course, will certainly provide us with an interface to create bitmap, and this interface class is: bitmapfactory! Come on, open the Bitmapfactory class, we click on the left side of the structure can see Bitmapfactory to provide us with these methods, most of them are decodexxx, through various forms to create bitmap!
3.bitmapfacotry.options
?? Then we found out that each method would have an option type parameter, click to see: So we found the goods is a static internal class: Bitmapfacotry.options! And he is used to set the decode when the option!
We set the values for some of these parameters, such as Injustdecodebounds set to True to avoid oom (memory overflow).
Common settings:
inJustDecodeBounds——如果设置为trueinSampleSize——图片缩放的倍数。如果设为4,则宽和高都为原来的1/4,则图是原来的1/16inininScaled——设置为true时进行图片压缩,从inDensity到inTargetDensity。
Two. How the memory size of the bitmap is calculated
Conclusion:
A argb_8888 Bitmap-occupied memory formula:
Bitmapinram = bitmapwidth*bitmapheight bytes
We read the drawable directory below the image, using the Decoderesource method, the system will read the pictures in these directories when the image is scaled, which is also to be considered.
the size of the Bitmap in memory actually depends on:
Color format, as we have mentioned earlier, if it is ARGB8888 then is a pixel 4 bytes, if it is RGB565 that is 2 bytes
The resource directory where the original file is stored (is hdpi or xxhdpi can not be silly to divide the unclear ha)
The density of the target screen (so under the same conditions, the red meter in the resource consumption of memory must be less than the Samsung S6)
Understanding of argb_8888, Alpha_8, argb_4444, rgb_565
A: Transparency
R: Red
G: Green
B: Blue
Bitmap.config argb_4444: Four bits per pixel, which is a=4,r=4,g=4,b=4, then one pixel takes up 4+4+4+4=16 bit.
Bitmap.config argb_8888: Four bits per pixel, which is a=8,r=8,g=8,b=8, then one pixel takes up 8+8+8+8=32 bit.
Bitmap.config rgb_565: Four bits per pixel, i.e. r=5,g=6,b=5, no transparency, then one pixel occupies 5+6+5=16 bit
Bitmap.config Alpha_8: Four bits per pixel, only transparency, no color.
In general, we are using the argb_8888, so that it is the most memory, because one pixel accounted for 32 bits, 8 bits = 1 bytes, so a pixel accounted for 4 bytes of memory. Suppose there is a picture of 480x800, and if the format is argb_8888, it will consume 1500KB of memory.
Three. Android's underlying image library
The underlying use is the Skia library, the Libjpeg library.
Http://www.cnblogs.com/hrlnw/p/4403334.html
Http://www.kuqin.com/shuoit/20140826/341828.html?url_type=39&object_type=crawler&pos=1
Four. Android Mega map loading scheme
Macro map loading, of course, can not use the regular method, must oom. The principle is relatively simple, there is a class bitmapregiondecoder in the system. There is a third party that can draw on Subsamplingscaleimageview.
Http://www.lai18.com/content/6705664.html
Five. Android loading the relationship between different DPI resources and memory consumption
http://www.tinylab.org/android-loading-a-different-relationship-between-dpi-and-memory-consumption-of-resources/
Resources
Android Bitmap API Summary and how to use
http://blog.csdn.net/shell812/article/details/49781255
A small summary of graphics and processing related APIs in Android
http://blog.csdn.net/zjngogo/article/details/49665113
API Demo Learning--android Bitmap Learning Summary
Http://blog.sina.com.cn/s/blog_5da93c8f0100w3xz.html
Bitmap (bitmap) Full resolution Part 1
http://www.kancloud.cn/kancloud/android-tutorial/87244
Android development around the hole: how much memory does your Bitmap account for?
https://mp.weixin.qq.com/s?__biz=MzA3NTYzODYzMg==&mid=403263974&idx=1&sn= b0315addbc47f3c38e65d9c633a12cd6&scene=0&key= 41ecb04b0511100390dcd25b8d443be2a357c0de12cdba7e7b1c4d06e1c2a9331881b1697239b4a3b89b86ef42a7eba9&ascene=0 &uin=mtyzmjy2mte1&devicetype=imac+macbookpro10,1+osx+osx+10.11.2+build (15C50) &version=11020201 &pass_ticket=9p5lnih3k3g8i2fvfncykognoueuiq1+6elldguwkgc=
Android loading the relationship between different DPI resources and memory consumption
http://www.tinylab.org/android-loading-a-different-relationship-between-dpi-and-memory-consumption-of-resources/
Android Bitmap Facets
https://mp.weixin.qq.com/s?__biz=MzA4MjU5NTY0NA==&mid=404530070&idx=1&sn= e2580b69d6ec73dabf8160216aa6702a&scene=1&srcid=0322dgvv5b4milg3vt0q2rk2&key= 710a5d99946419d9e5ab07eaa09e546952524b770e8a63c46c223b7588209d3a542fe4a0827571ac73b03cb78b404289&ascene=0 &uin=mtyzmjy2mte1&devicetype=imac+macbookpro10,1+osx+osx+10.11.4+build (15E65) &version=11020201 &pass_ticket=sqb+poltrz0wixoscj8flyxfv4tofnba7nfos1uig8u=
http://m.blog.csdn.net/article/details?id=7856519
App Performance Optimization series: Memory optimization-bitmap detailed