Android Universal-Image-Loader framework (II), androidimageloader
1. The "add Progress" dialog box is displayed in the GridView:
The Universal-Image-Loader framework makes it easier to load images. Next, let's take a look at how to append the progress bar in the GridView.
:
Main Interface:
<Span style = "font-size: 18px;"> public class GridActivity extends Activity {private GridView grid; public static final String [] IMAGES = new String [] {"http://img.dapixie.com/uploads/allimg/111105/1-111105145231.jpg ", "http://img3.3lian.com/2014/c2/61/d/1.jpg", "http://img3.3lian.com/2014/c2/61/d/2.jpg", "http://img3.3lian.com/2014/c2/61/d/3.jpg", "http://img3.3lian.com/2014/c2/61/d/4.jpg", "http: // img3.3li An.com/2014/c2/61/d/5.jpg "," http://img3.3lian.com/2014/c2/61/d/6.jpg "," http://img3.3lian.com/2014/c2/61/d/7.jpg "," http://img3.3lian.com/2014/c2/61/d/8.jpg "," http://img3.3lian.com/2014/c2/61/d/9.jpg "," http://img3.3lian.com/2014/c2/61/d/10.jpg "," http://img3.3lian.com/2014/c2/61/d/11.jpg "," http://img3.3lian.com/2014/c2/61/d/12.jpg "}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. OnCreate (savedInstanceState); setContentView (R. layout. grid_activity); grid = (GridView) findViewById (R. id. grid); grid. setAdapter (new ImageAdapter (this);} @ Overrideprotected void onDestroy () {super. onDestroy (); ImageLoader. getInstance (). clearMemoryCache (); // clear the ImageLoader cache. getInstance (). clearDiskCache (); // clear the ImageLoader cache. getInstance (). stop (); // stop} private static class ImageAdapter extends BaseAdapter {Private static final String [] IMAGE_URLS = GridActivity. IMAGES; private LayoutInflater inflater; private DisplayImageOptions options; ImageAdapter (Context context) {inflater = LayoutInflater. from (context); 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 ). bitmapConfig (Bitmap. config. RGB_565 ). build () ;}@ Overridepublic int getCount () {return IMAGE_URLS.length ;}@ Overridepublic Object getItem (int position) {return null ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {final ViewHolder holder; View view = convertView; if (view = null) {v Iew = inflater. inflate (R. layout. grid_listview_item, parent, false); holder = new ViewHolder (); assert view! = Null; holder. imageView = (ImageView) view. findViewById (R. id. image); holder. progressBar = (ProgressBar) view. findViewById (R. id. progress); view. setTag (holder);} else {holder = (ViewHolder) view. getTag ();} ImageLoader. getInstance (). displayImage (IMAGE_URLS [position], holder. imageView, options, new SimpleImageLoadingListener () {@ Overridepublic void onLoadingStarted (String imageUri, View view) {// set the progress bar to 0 holder. progressBar. setProgress (0); // holder is displayed when the download starts. progressBar. setVisibility (View. VISIBLE) ;}@ Overridepublic void onLoadingFailed (String imageUri, View view, FailReason failReason) {// download failed to hide holder. progressBar. setVisibility (View. GONE) ;}@ Overridepublic void onLoadingComplete (String imageUri, View, Bitmap loadedImage) {// download failed to hide holder. progressBar. setVisibility (View. GONE) ;}, new ImageLoadingProgressListener () {@ Overridepublic void onProgressUpdate (String imageUri, View view, int current, int total) {// current progress, total progress holder. progressBar. setProgress (Math. round (100366f * current/total) ;}}); return view ;}} static class ViewHolder {ImageView imageView; ProgressBar progressBar ;}</span>
Main Interface layout:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <GridViewandroid:id="@+id/grid"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:horizontalSpacing="4dip"android:numColumns="3"android:stretchMode="columnWidth"android:verticalSpacing="4dip"android:padding="4dip" /></LinearLayout></span>
Grid_item layout:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="120dip"><ImageViewandroid:id="@+id/image"android:layout_width="match_parent"android:layout_height="120dip"android:adjustViewBounds="true"android:contentDescription="@string/descr_image"android:scaleType="centerCrop" /><ProgressBarandroid:id="@+id/progress"android:layout_width="match_parent"android:layout_height="wrap_content"android:indeterminate="false"android:max="100"android:layout_gravity="bottom"style="@style/ProgressBarStyle" /></FrameLayout></span>
Progress bar style:
<span style="font-size:18px;"><style name="ProgressBarStyle" parent="@android:style/Widget.Holo.ProgressBar.Horizontal"/></span>
ImageLoader Configuration:
<Span style = "font-size: 18px;"> public class MyApp extends Application {@ Overridepublic void onCreate () {super. onCreate (); initImageLoader1 (getApplicationContext ();}/** custom configuration */public static void initImageLoader (Context context) {ImageLoaderConfiguration. builder config = new ImageLoaderConfiguration. builder (context); config. threadPoolSize (3); // Number of threads loaded in the thread pool config. threadPriority (Thread. NORM_PRIORITY-2); Config. denyCacheImageMultipleSizesInMemory (); // multiple sizes of images not cached in the memory config. diskCacheFileNameGenerator (new Md5FileNameGenerator (); // use MD5config to specify the URI name when saving. diskCacheSize (50*1024*1024); // 50 MiBconfig. tasksProcessingOrder (QueueProcessingType. LIFO); config. writeDebugLogs (); // Remove for release app // initialize ImageLoader to use ImageLoader. getInstance (). init (config. build ();}/** default configuration. Generally, you can use the default configuration when there are no special requirements. */Public static void initImageLoader1 (Context context) {// create the default ImageLoader configuration Parameter ImageLoaderConfiguration = ImageLoaderConfiguration. createDefault (context); // initialize ImageLoader to use ImageLoader. getInstance (). init (configuration) ;}}</span>
This example is continued from the previous article and is also written in its project.
If you want to stop loading in the slide and Continue loading the Image after stopping the slide, the Universal-Image-Loader also provides this function. The PauseOnScrollListener class is a listener.
listView.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(), pauseOnScroll, pauseOnFling));
The first parameter of PauseOnScrollListener is the image loading object ImageLoader, and the second parameter is to control whether to pause image loading during the sliding process. If you need to pause uploading true, the third parameter controls whether the image is loaded when the interface is swiped.