Android Bitmap The premise of not loading the picture to get the width high

Source: Internet
Author: User

Code reference:

 /** * To get thumbnails of images based on the width and height of the view (mostly ImageView) * @param path * @param viewwidth * @param viewheight * @ return */Private Bitmap Decodethumbbitmapforfile (String path, int viewwidth, int viewheight) {BITMAPFA Ctory.          Options options = new Bitmapfactory.options ();          Set to True to resolve the bitmap object, which does not account for memory Options.injustdecodebounds = true;          Bitmapfactory.decodefile (path, options);                    Set Scaling options.insamplesize = Computescale (options, Viewwidth, viewheight);                    Set to False to parse the bitmap object into memory Options.injustdecodebounds = false;      return Bitmapfactory.decodefile (path, options); /** * Calculates the bitmap scale based on the width and height of the view (mainly ImageView). The default does not scale * @param options * @param width * @param height */private int Computescale (BITMAPFACTORY.O          ptions options, int viewwidth, int viewheight) {int insamplesize = 1; if (Viewwidth = = 0 | | viewheight = = 0) {return insamplesize;          } int bitmapwidth = Options.outwidth;                    int bitmapheight = Options.outheight;              If the width or height of the bitmap is greater than the width of the view in which we set the picture, then the scaling if (Bitmapwidth > Viewwidth | | bitmapheight > Viewwidth) {              int widthscale = Math.Round ((float) bitmapwidth/(float) viewwidth);                            int heightscale = Math.Round ((float) bitmapheight/(float) viewwidth); In order to ensure that the image does not scale deformation, we take the Insamplesize = Widthscale < Heightscale with the smallest width and height ratio?          Widthscale:heightscale;      } return insamplesize;         }
Also refer to:

Bitmapfactory.options Options = new Bitmapfactory.options ();                  /**         * Most critical here, put options.injustdecodebounds = true;         * Here again DecodeFile (), return the bitmap is empty, but at this time call Options.outheight, already contains the picture of the height of         */         Options.injustdecodebounds = true;         Bitmap Bitmap = Bitmapfactory.decodefile ("/sdcard/test.jpg", options); The returned Bitmap is null         /**         *options.outheight is the height of the original picture         *         /LOG.E ("Test", "Bitmap Height = =" + Options.outheight);  

Based on these methods, you can set the picture to be reduced to the original (1/2,1/4,1/8,1/16.) before actually loading the bitmap picture.

Here is a description of insamplesize in the Android source code:

<span style= "Background-color:rgb (51, 255, 51); >public int insamplesize</span>added in API level 1If set to a value > 1, requests the decoder to subsample th E original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension this correspond to a single pixel in the decoded bitmap. For example, insamplesize = = 4 Returns an image which is the width/height of the original, and 1/16 the number of pixel S. Any value <= 1 is treated the same as 1. Note:the decoder uses a final value based on powers of 2,<span style= "Background-color:rgb (51, 255, 51);" > Any other value would be rounded down to the nearest power of 2</span>.

Android Bitmap The premise of not loading the picture to get the width high

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.