Android supports asynchronous image loading (thumbnail display) and exclusive thumbnails.

Source: Internet
Author: User

Android supports asynchronous image loading (thumbnail display) and exclusive thumbnails.

/*** Obtain the thumbnail Based on the specified image path and size * This method has two advantages: * 1. using a small memory space, the bitmap obtained for the first time is actually null, just to read the width and height. * The bitmap read for the second time is the image compressed proportionally, the third bitmap read is the desired thumbnail. * 2. The thumbnail is not stretched for the original image. Here we use the new ThumbnailUtils tool of Version 2.2, so that * images generated using this tool will not be stretched. * @ Param imagePath image path * @ param width specifies the width of the output image * @ param height specifies the height of the output image * @ return generated thumbnail */private void getImageThumbnail (ImageView thumbnails_img, string imagePath, int width, int height) {asy = new AsyncImageTask (thumbnails_img, width, height); asy.exe cute (imagePath );} /*** use an asynchronous thread to load images * thread pool */private final class AsyncImageTask extends AsyncTask <String, Integer, Bitmap> {private ImageView thumb Nails_img; private int width; private int height; public AsyncImageTask (ImageView thumbnails_img, int width, int height) {this. thumbnails_img = thumbnails_img; this. width = width; this. height = height;} protected Bitmap doInBackground (String... params) {img_bitmap = null; // memory saving options. inPreferredConfig = Bitmap. config. ARGB_4444;/* sets the decoder to be decoded in the best way */options. inPurgeable = true; options. ininputretriable = tru E; options. inJustDecodeBounds = true; // If diTher is true, the decoder will attempt to dither the decoded imageoptions. inDither = false; // do not perform image jitter processing // obtain the width and height of the image. Note that bitmap here is null img_bitmap = BitmapFactory. decodeFile (params [0], options); options. inJustDecodeBounds = false; // set it to false // calculate the zoom ratio int h = options. outHeight; int w = options. outWidth; int beWidth = w/width; int beHeight = h/height; int be = 1; if (beWidth <beHeight) {be = beWidth;} else {be = beHeight;} if (be <= 0) {be = 1;} options. inSampleSize = be; // read the image again and read the scaled bitmap. inJustDecodeBounds is set to false img_bitmap = BitmapFactory. decodeFile (params [0], options); // use ThumbnailUtils to create a thumbnail. Specify the Bitmap object to be scaled, img_bitmap = ThumbnailUtils. extractThumbnail (img_bitmap, width, height, ThumbnailUtils. OPTIONS_RECYCLE_INPUT); Return img_bitmap;} // processing result protected void onPostExecute (Bitmap result) {if (result! = Null & thumbnails_img! = Null) {thumbnails_img.setImageBitmap (result); asy_list.add (asy); asy = null; // img_bitmap.recycle (); img_bitmap = null ;}}}

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.