Android day10-Multimedia

Source: Internet
Author: User

Multimedia refers to text, pictures, audio, video

First, the picture article

1. Calculation of image size

picture size = Resolution * Bit depth/8 (in bytes)

Resolution: Is the image of the long * width, the unit is a phase.

Bit depth: Is the number of bits that each phase represents. (Right-click the picture's properties to see the image's bit depth)

for BMP images, there are several storage formats:

1) Monochrome: Each pixel can represent a maximum of 2 colors, 2 = 2 of the 1, as long as the length of 1 is used to represent the bits

Bit deeply 1, then one pixel is 1/8 byte, which is log22/8.

2) 16 colors: Each pixel can represent a maximum of 16 colors, 16=2 4 square, as long as the length of 4 bits to represent

, the bit is deeply 4, then a pixel is 4/8 byte, which is log216/8.

3) 256 colors: Each pixel can represent a maximum of 256 colors, 256=2 8 square, as long as the use of a length of 8 bits to

Indicates that the bit is deeply 8, then a pixel is 8/8 byte, which is log2256/8.

4) 24-bit: Each pixel can represent a maximum of 1600多万多 colors, 16777216 = 2 of 24 times, as long as the use of length

For the bits of 24, the bit depth is 24, then a pixel is 24/8 bytes, which is

Log216777216/8.

Here is a picture of my cut:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/70/6B/wKioL1W3nymA9Y9XAAHX1je88tM897.jpg "title=" in various formats. PNG "alt=" Wkiol1w3nyma9y9xaahx1je88tm897.jpg "/>
For other common format pictures, such as PNG, JPG and so on, the picture is compressed using compression algorithm, so the image pixel

The size can not be easily calculated, here is not spent too much time to study.


calculation of 2.Android sheet size

Under the Android system, images are usually loaded with bitmapfactory.

The default load for bitmap in Android uses argb_8888 color mode, which consumes 4byte per pixel.

For example: A 512*512 picture, no matter what format, loading into the memory are occupied 512*512*4=1MB, the

To, the Android image occupies memory size, only with the image resolution (pixels) and load the use of the color mode related)

Additional Reference Links: http://www.cnblogs.com/fengzhblog/p/3227471.html

http://my.oschina.net/u/1389206/blog/324731


3.Android loading big picture

1) oom anomaly

I use the ImageView control provided by Android to load a picture of a 2400*3200 photo directly

ImageView IV = (ImageView) Findviewbyid (R.ID.IV);             Bitmap Srcbitmap = Bitmapfactory.decodefile (Environment.getexternalstoragedirectory (). GetPath () + "/bigpic.bmp"); Iv.setimagebitmap (SRCBITMAP);


Then the following exception was reported:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/70/6E/wKiom1W3pofiZO5oAAEuYPq2X0E663.jpg "title=" Load large picture error. PNG "alt=" wkiom1w3pofizo5oaaeuypq2x0e663.jpg "/> The line above which I marked with a red rectangle, meaning that the 30720012-byte allocation exceeds the maximum value of the heap size

16777216 bytes. So how did these two numbers come from?

When I created the AVD, I allocated the heap size to 16MB = 16777216 bytes, so 16777216

Refers to the size of the AvD heap, which is the maximum memory space allocated by the virtual machine to each application.

In the above image size calculation, has already talked about the image size computation method in the Android, my code is uses the

Bitmap to load the picture, so the size of the picture is 2400*3200*4 = 30720000 (12 less than 30720012)

A byte??? )

It can be seen that when Android loads the image, the resolution of the image is too large, a memory overflow exception occurs.

Out of memory. So here's how to avoid this anomaly in Android.

2) Zoom load large picture

Picture resolution (size) is too large, then load the picture size is smaller, then how should it be smaller? So so

The situation is to load the image according to the screen resolution of the phone. The steps are as follows:

1th step: Get Phone resolution

2nd step: Get Picture resolution

3rd step: Calculate the bloom ratio

4th step: To load the picture by the bloom ratio

The code is as follows:

        //[1] Get phone resolution      by WindowManager this class to achieve.         //Get WindowManager Object              WindowManager windowManager =  (WindowManager)   Getsystemservice (window_service);         //get Display Object              Display display =  Windowmanager.getdefaultdisplay ();             int  screen_width  = display.getwidth ();             int screen_height = display.getheight ();                 //[2] Get the picture resolution              file file&nbSp;= new file (Environment.getexternalstoragedirectory (). GetPath ()  +  "/dog.bmp");             BitmapFactory.Options opts =  New options ();             opts.injustdecodebounds = true;        //indicates that no memory space is allocated for the pixels of the picture             bitmapfactory.decodefile (File.getPath ( ),  opts);                     //This method returns NULL when the above property of opts is set to true, but the          The size information of the     //image is stored in the OPTs object. The API is very clear about the use of this method.             int pic_width =  Opts.outwidth;            inT PIC_HEIGHT = OPTS.OUTHEIGHT;        //[3] Calculate zoom ratio              int scale = 1;     //Final zoom ratio             // I personally think that the tight computing scale ratio should be up and down             int scale_x  =  (int)  math.ceil (pic_width * 1.0 / screen_width);             int scale_y =  (int)  math.ceil (pic_height  * 1.0 / screen_height);             scale =  scale_x > scale_y ? scale_x:scale_y ;             //if the scaling ratio is less than or equal to 1, there is no need to scale.             scale = scale > 1 ? scale:1;         //[4] by bloom ratio to load pictures              opts.insamplesize = scale;                 //Specifies the zoom ratio when the picture is loaded, and if it is 4, the size of the picture will change to the original 1/16.             //about this property, the API is very clear.             opts.inJustDecodeBounds =  false;            bitmap scaledbitmap =  bitmapfactory.decodefile (File.getpath (),  opts);             iv.setimagebitmap (SCALEDBITMAP);


This article is from the "line of the World" blog, please be sure to keep this source http://4259297.blog.51cto.com/4249297/1679405

Android day10-Multimedia

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.