Android Image cache framework Android-Universal-Image-Loader (2)

Source: Internet
Author: User

This article tells you how to use it. In fact, this is not a very difficult framework. The general usage process is as follows:

// Obtain the cache image directory File cacheDir = StorageUtils. getOwnCacheDirectory (activity, "imageloader/Cache"); ImageLoaderConfiguration config = new ImageLoaderConfiguration. builder (activity ). memoryCacheExtraOptions (800,760) // Save the maximum length and width of each cached image. threadPoolSize (3 ). diskCache (new UnlimitedDiscCache (cacheDir) // The thread pool size. The default value is 3. memoryCacheSize (2*1024) // sets the maximum cache byte. denyCacheImageMultipleSizesInMemory () // The cache displays the same image of different sizes. imageDownloader (new BaseImageDownloader (activity, 5*1000, 30*1000) // connectTimeout // s) Timeout time. build (); imageLoader. init (config );

The directory set above is in my mobile phone's/storage/sdcard0/imageloader/cache. the file does not end with .jpg).png. It is a bunch of unformatted files that can be exported and opened through extension export.

Step 2: Set the image display options:

 

DisplayImageOptionsoptions = new DisplayImageOptions. builder (). showImageOnLoading (R. drawable. ic_launcher) // sets the image displayed during the download process. showImageForEmptyUri (R. drawable. ic_launcher) // sets the image to be displayed when the image Uri is null or incorrect. showImageOnFail (R. drawable. ic_launcher) // sets the image displayed when an error occurs during image loading/decoding. cacheInMemory (true) // sets whether the downloaded image is cached in memory. cacheOnDisk (true) // sets whether the downloaded image is cached in the SD card. displayers (new FadeInBitmapDisplayer (100 )). considerExifParams (true ). displayer (new FadeInBitmapDisplayer (100) // specifies the animation time after the image is loaded. displayer (new RoundedBitmapDisplayer (1 )). build (); // This must be set. If it is not set, the image cannot be filled with the entire control. This setting sets the rounded corner effect, if you do not like the rounded corner, setting it to 1 is almost ineffective.


The above code can be used normally. 

The following is an official column subscribe. It is very simple for android to see how to use the code below.

public class ImageListActivity extends AbsListViewBaseActivity {DisplayImageOptions options;String[] imageUrls;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.ac_image_list);Bundle bundle = getIntent().getExtras();imageUrls = bundle.getStringArray(Extra.IMAGES);options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_stub).showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).cacheInMemory(true).cacheOnDisk(true).considerExifParams(true).displayer(new RoundedBitmapDisplayer(20)).build();listView = (ListView) findViewById(android.R.id.list);((ListView) listView).setAdapter(new ItemAdapter());listView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView
  parent, View view, int position, long id) {startImagePagerActivity(position);}});}
// Note that when you press the return key, you can clear the images in the memory. However, it is better to manually clear the memory if you are too dependent on automatic memory management, @ Overridepublic void onBackPressed () {AnimateFirstDisplayListener. displayedImages. clear (); super. onBackPressed ();} private void startImagePagerActivity (int position) {Intent intent = new Intent (this, ImagePagerActivity. class); intent. putExtra (Extra. IMAGES, imageUrls); intent. putExtra (Extra. IMAGE_POSITION, position); startActivity (intent);} private static class ViewHolder {TextView text; ImageView image;} class ItemAdapter extends BaseAdapter {private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener (); @ Overridepublic int getCount () {return imageUrls. length ;}@ Overridepublic Object getItem (int position) {return position ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (final int position, view convertView, ViewGroup parent) {View view = convertView; final ViewHolder holder; if (convertView = null) {view = getLayoutInflater (). inflate (R. layout. item_list_image, parent, false); holder = new ViewHolder (); holder. text = (TextView) view. findViewById (R. id. text); holder. image = (ImageView) view. findViewById (R. id. image); view. setTag (holder);} else {holder = (ViewHolder) view. getTag ();} holder. text. setText ("Item" + (position + 1); imageLoader. displayImage (imageUrls [position], holder. image, options, animateFirstListener); return view ;}} private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {static final List
 
  
DisplayedImages = Collections. synchronizedList (new shard list
  
   
(); @ Overridepublic void onLoadingComplete (String imageUri, View, Bitmap loadedImage) {if (loadedImage! = Null) {ImageView imageView = (ImageView) view; boolean firstDisplay =! DisplayedImages. contains (imageUri); if (firstDisplay) {FadeInBitmapDisplayer. animate (imageView, 500); displayedImages. add (imageUri );}}}}}
  
 


This is a demo. It is best to configure this image option into a single column Class to facilitate management and reduce memory overflow.

Related Article

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.