Android Bitmap obtains the width and height without loading images.

Source: Internet
Author: User

Android Bitmap obtains the width and height without loading images.

Code reference:

 

/*** According to View (mainly ImageView) to obtain the thumbnail of an image * @ param path * @ param viewWidth * @ param viewHeight * @ return */private Bitmap decodeThumbBitmapForFile (String path, int viewWidth, int viewHeight) {BitmapFactory. options options = new BitmapFactory. options (); // if it is set to true, it indicates that the Bitmap object is parsed and does not occupy the memory options. inJustDecodeBounds = true; BitmapFactory. decodeFile (path, options); // set the zoom ratio options. inSampleSize = computeSca Le (options, viewWidth, viewHeight); // if it is set to false, resolve the Bitmap object and add it to the memory options. inJustDecodeBounds = false; return BitmapFactory. decodeFile (path, options);}/*** calculate the Bitmap scaling ratio based on the width and height of the View (mainly ImageView. By default, no scaling * @ param options * @ param width * @ param height */private int computeScale (BitmapFactory. options 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 and height of the image's View, the zoom ratio is calculated if (bitmapWidth> viewWidth | bitmapHeight> viewWidth ){ Int widthScale = Math. round (float) bitmapWidth/(float) viewWidth); int heightScale = Math. round (float) bitmapHeight/(float) viewWidth); // to ensure that the image is not scaled or deformed, The inSampleSize = widthScale For more information, see:

 

 

BitmapFactory. options options = new BitmapFactory. options ();/*** the key is to set options. inJustDecodeBounds = true; * decodeFile () is returned here. The returned bitmap is null, but options is called. outHeight has already included the image height */options. inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory. decodeFile ("/sdcard/test.jpg", options); // The returned bitmap is null/*** options. outHeight is the height of the original image */Log. e ("Test", "Bitmap Height =" + options. outHeight );

Based on these methods, you can set the image size to 1/2, 1/4, 1/8, 1/16, or before loading Bitmap images ..)

 

The following describes inSampleSize In the Android source code:

 

public int inSampleSizeAdded in API level 1If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2.

 

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.