Today to introduce the picture loading frame Android-universal-image-loader
The download path on GitHub is: Https://github.com/nostra13/Android-Universal-Image-Loader
can also be self-Baidu download.
First to encapsulate a class cachetool, because the other way to load the picture is a bit cumbersome, so here only a simple and practical loading method is encapsulated:
Import Android.graphics.bitmap;import Android.widget.imageview;import com.ncct.app.r;import Com.nostra13.universalimageloader.core.displayimageoptions;import Com.nostra13.universalimageloader.core.imageloader;import Com.nostra13.universalimageloader.core.assist.ImageLoadingListener; /* * * Picture Loading frame * * @author Jiang * * */publicclass Cachetool {
Showstubimage the pictures displayed in the load
Showimageforemptyuri 404 Pictures displayed
Showimageonfail load failed to display picture
Private Static New Displayimageoptions.builder (). Showstubimage (r.drawable.loading_img)
. Showimageforemptyuri (R.drawable.loading_error). Showimageonfail (r.drawable.loading_error). Cach Einmemory (true). Cacheondisc (true). Bitmapconfig (Bitmap.Config.RGB_565). build (); /** * load images and listen for callback results * * @param IV * @param URL * @param mimageloadinglistener*/ Public Static voiddisplayimg (ImageView IV, String URL, Imageloadinglistener mimageloadinglistener) {imageloader.getinstance (). D Isplayimage (URL, iv, Options, Mimageloadinglistener); } /** * Load Picture * * @param IV * @param URL*/ Public Static voiddisplayimg (ImageView IV, String URL) {imageloader.getinstance (). DisplayImage (URL, iv, options); } /** * Clear Memory*/ Public Static voidClearmemorycache () {imageloader.getinstance (). Clearmemorycache (); } /** * Clear Cache*/ Public Static voidCleardiskcache () {imageloader.getinstance (). Cleardisccache (); }}
Packaged well, there are detailed introduction, here the above Imageloadinglistener interface callback, press CTRL + LEFT mouse button to enter the Java file in the jar package:
/******************************************************************************* * Copyright 2011-2013 Sergey Tarasevich * * Licensed under the Apache License, Version 2.0 (the "License"); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is distribute D on ' As is ' BASIS, * without warranties or CONDITIONS of any KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/Package Com.nostra13.universalimageloader.core.assist;import Android.graphics.bitmap;import Android.view.View ;/** * Listener for image loading process.<br/> * Can use {@link Simpleimageloadinglistener} for implementing Only needed methods. * * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) * @see Simpleimageloadinglistener * @see Failreason * @since 1.0 .0*/ Public InterfaceImageloadinglistener {/** is called when image loading task was started * * @param imageuri loading Image URI * @param view View for Image*/ voidonloadingstarted (String imageuri, view view); /** is called when a error was occurred during image loading * * @param imageuri loading image URI * @param view view for image. Can be <b>null</b>. * @param Failreason {@linkplain Failreason the reason} Why image loading is failed*/ voidonloadingfailed (String imageuri, view view, Failreason Failreason); /** is called when image is loaded successfully (and displayed in View if one was specified) * * @param im Ageuri Loaded Image URI * @param view view for image. Can be <b>null</b>. * @param loadedimage Bitmap of Loaded and decoded image*/ voidOnloadingcomplete (String imageuri, view view, Bitmap loadedimage); /** is called when image loading task was cancelled because View for image were reused in newer task * @p Aram Imageuri Loading Image URI * @param view view for image. Can be <b>null</b>. */ voidonloadingcancelled (String imageuri, view view);}
From the above code we can understand the interface we can hear the start, failure, completion, cancellation of the action.
Start using it now:
Private ImageView My_head; @Override protectedvoid onCreate (Bundle savedinstancestate) { super.oncreate ( Savedinstancestate); Requestwindowfeature (window.feature_no_title); Setcontentview (r.layout.personcenter); = (ImageView) Findviewbyid (r.id.my_head); " http://pic.nipic.com/2007-11-09/200711912453162_2.jpg " ; Cachetool.displayimg (My_head, URL); }
Android Picture loading frame--universal-image-loader