Android (Java) Learning Note 236: Multimedia loading big picture to memory (Bitmap API)

Source: Internet
Author: User

1.Bitmap (API usage )

Android inside the bitmap, a pixel needs 4 byte to express, this is because Android indicates that the color is " argb ", where a is a means of transparency , and then " RGB"

Color Indication Range 00000000 ~ ~ ~ffffffff

2. Load image to Memory:

It says the graphic means using 4byte, like int, so every pixel in Android is represented by an int .

Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.girl);
IV = (ImageView) Findviewbyid (R.ID.IV);
Iv.setimagebitmap (bitmap);

2. Load large image into memory:

Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.very_large_phone);
IV = (ImageView) Findviewbyid (R.ID.IV);
Iv.setimagebitmap (bitmap);

Here the picture Very_large_phone very large, 1.69M (2560*1504);

Then this picture needs 2560*1504*4 = 15400960 byte in Android to represent this picture, or the result of an oom (out of Memony) memory overflow error.

Shows that the Android display picture is not the size of the picture itself , but the resolution .

So how does Android show large pictures (large resolution)?

To avoid oom anomalies, scale the picture according to the size of the screen

1. Get the width and height of the phone screen first

WindowManager wm = (WindowManager) Getsystemservice (window_service);

int ScreenWidth = Wm.getdefaultdisplay (). GetWidth ();

int ScreenHeight = Wm.getdefaultdisplay (). GetHeight ();

2. Get the width and height of the picture

Bitmapfactory.options opts = new Option ();//Create a configuration parameter

Opts.injustdecodebounds = true;//means that the bitmap is not truly parsed, only the width and height of the bitmap is parsed ( no spatial resolution is requested, no memory overflow is natural )

Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.very_large_phone, opts);

int width = opts.outwidth;

int height = opts.outheight;

3. Calculate the scale of the zoom

int scale = 1;

int ScaleX = Width/screenwidth;

int ScaleY = Height/screenheight;

if (ScaleX > ScaleY && scaleY >1) {

     scale = ScaleX;

}

if (scaleY> scaleX && scaleX >1) {

scale = ScaleY;

}

4. True parse bitmap based on scale

Opts.insamplesize = scale;

Opts.injustdecodebounds = false;//true to parse this bitmap, return to bitmap;

Bitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.very_large_phone, opts);//Bitmap after scaling

Iv.setimagebitmap (bitmap);

Android (Java) Learning Note 236: Multimedia loading big picture to memory (Bitmap API)

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.