标签:
参考了http://blog.csdn.net/xiaanming/article/details/26810303这篇文章,对原作者感谢。
优秀无需多说,github项目地址:https://github.com/nostra13/Android-Universal-Image-Loader。
其主要特征:
1.新建一个Android appication.名字叫IApplicaiton.
2.把下载下来的项目里的sample里的libs下的jar拷贝到此项目中。
3.新建一个类MyApplicaiton继承application:
package com.example.iapplication;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import android.app.Application;
public class MyApplication extends Application {
	 @Override  
	    public void onCreate() {  
	        super.onCreate();  
	  
	        //创建默认的ImageLoader配置参数  
	        ImageLoaderConfiguration configuration = ImageLoaderConfiguration  
	                .createDefault(this);  
	          
	        //Initialize ImageLoader with configuration.  
	        ImageLoader.getInstance().init(configuration);  
	    }  
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.iapplication"
    android:versionCode="1"
    android:versionName="1.0" >
	
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
	 <uses-permission android:name="android.permission.INTERNET" />  
	 <!-- Include next permission if you want to allow UIL to cache images on SD card -->  
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:name="MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.iapplication.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
注意application节点和权限的加入。
接下来,加载图片。
5.定义一个layout布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView
        android:layout_gravity="center"
        android:id="@+id/image"
        android:src="@drawable/ic_empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>//使用displayImage方法加载
	public void advanceLoad2(){
		final ImageView mImageView = (ImageView) findViewById(R.id.image);  
        String imageUrl = "http://smartcost.com.cn/global/upload/img/20150206/14223432211081942.jpg";  
        //显示图片的配置  
        DisplayImageOptions options = new DisplayImageOptions.Builder()  
                .showImageOnLoading(R.drawable.ic_stub)  
                .showImageOnFail(R.drawable.ic_error)  
                .cacheInMemory(true)  
                .cacheOnDisk(true)  
                .bitmapConfig(Bitmap.Config.RGB_565)  
                .build();  
          
        ImageLoader.getInstance().displayImage(imageUrl, mImageView, options);  
	}imageLoader.displayImage(imageUrl, mImageView, options, new SimpleImageLoadingListener(), new ImageLoadingProgressListener() {  
              
            @Override  
            public void onProgressUpdate(String imageUri, View view, int current,  
                    int total) {  
                  
            }  
        });   String imagePath = "/mnt/sdcard/image.png";  
        String imageUrl = Scheme.FILE.wrap(imagePath);  
          
//      String imageUrl = "http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg";  
          
        imageLoader.displayImage(imageUrl, mImageView, options);  //图片来源于Content provider  
        String contentprividerUrl = "content://media/external/audio/albumart/13";  
          
        //图片来源于assets  
        String assetsUrl = Scheme.ASSETS.wrap("image.png");  
          
        //图片来源于  
        String drawableUrl = Scheme.DRAWABLE.wrap("R.drawable.image");  
当我们快速滑动GridView,ListView,我们希望能停止图片的加载,而在GridView,ListView停止滑动的时候加载当前界面的图片,这个框架当然也提供这个功能,使用起来也很简单,它提供了PauseOnScrollListener这个类来控制ListView,GridView滑动过程中停止去加载图片
listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));  
        gridView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling)); 
OutOfMemoryError
虽然这个框架有很好的缓存机制,有效的避免了OOM的产生,一般的情况下产生OOM的概率比较小,但是并不能保证OutOfMemoryError永远不发生,这个框架对于OutOfMemoryError做了简单的catch,保证我们的程序遇到OOM而不被crash掉,但是如果我们使用该框架经常发生OOM,我们可以使用一下方式去改善。
我们在使用该框架的时候尽量的使用displayImage()方法去加载图片,loadImage()是将图片对象回调到ImageLoadingListener接口的onLoadingComplete()方法中,需要我们手动去设置到ImageView上面,displayImage()方法中,对ImageView对象使用的是Weak references,方便垃圾回收器回收ImageView对象,如果我们要加载固定大小的图片的时候,使用loadImage()方法需要传递一个ImageSize对象,而displayImage()方法会根据ImageView对象的测量值,或者android:layout_width and android:layout_height设定的值,或者android:maxWidth and/or android:maxHeight设定的值来裁剪图片
Android Universal-Image-Loader初级使用
标签:
原文地址:http://blog.csdn.net/howlaa/article/details/45533905