Android Buffet big Picture loading principle
- Using
BitmapFactory.decodeStreeam() the method, the method invokes the native layer code to create the bitmap (two overloads are called)
- Use
BitmapFactory.Options a method with parameters to specify the size of the build bitmap
Ideas
- Determine the size of the bitmap according to the View size or window size
- Put the determined dimensions into the
BitmapFactory.Options
- Call
BitmapFactory.decodeStreeam() Build Bitmap
Steps
-
Open an input stream based on a picture path or URI
InputStream is = getContentResolver().openInputStream(imageUri);
-
Get screen or view size
If you can determine the view size to use the view size, if not (such as dynamically adjusted view, adaptive view, etc.) to get the most close to the view of the size, do not get the current Activity window size (smaller than the screen size)
- Get window Size
WindowManager windowManager = getWindowManager();
Display defaultDisplay = windowManager.getDefaultDisplay();
defaultDisplay.getHeight();
defaultDisplay.getWidth();
- Get View Size
view.getMeasuredWidth();
view.getMeasuredHeight();
-
Generate bitmapfactory.options based on target size
optionnew BitmapFactory.Options();option.inSampleSize = dstSize;
-
Use the options call Bitmapfactory.decodestream () to generate the bitmap
Bitmap bitmap = BitmapFactory.decodeStream(isnulloption);
Full code
InputStream is=NULL;Try{intScreenwidth=getwindowmanager (). Getdefaultdisplay (). GetWidth ();intScreenheight=getwindowmanager (). Getdefaultdisplay (). GetHeight ();intMaxsize=math.max (Screenwidth,screenheight);//Take the long side as the subject is= Getcontentresolver (). Openinputstream (Imageuri); bitmapfactory.options option =NewBitmapfactory.options (); Option.insamplesize = maxSize; Bitmap Bitmap = Bitmapfactory.decodestream ( is,NULL, option); Imageview.setimagebitmap (bitmap);}Catch(Exception e) {E.printstacktrace ();}Try{if( is!=NULL) is. Close ();}
Big picture of Android buffet loading