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)