Big picture of Android buffet loading

Source: Internet
Author: User

Android Buffet big Picture loading principle
    1. Using BitmapFactory.decodeStreeam() the method, the method invokes the native layer code to create the bitmap (two overloads are called)
    2. Use BitmapFactory.Options a method with parameters to specify the size of the build bitmap
Ideas
    1. Determine the size of the bitmap according to the View size or window size
    2. Put the determined dimensions into theBitmapFactory.Options
    3. Call BitmapFactory.decodeStreeam() Build Bitmap
Steps
  1. Open an input stream based on a picture path or URI

    InputStream is = getContentResolver().openInputStream(imageUri);
  2. 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();
  3. Generate bitmapfactory.options based on target size

    optionnew BitmapFactory.Options();option.inSampleSize = dstSize;
  4. 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

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.