使用Universal-Image-Loader总结的图片加载工具类

来源:互联网 发布:薄膜键盘推荐 知乎 编辑:程序博客网 时间:2024/06/10 03:36

架包位于https://github.com/nostra13/Android-Universal-Image-Loader
加载图片来源:图片来源:1:网络;2:sd卡;3.Content provider ;4.assets ;5.drawable

package com.hp.lessonhelper.utils;import android.content.Context;import android.graphics.Bitmap;import android.widget.ImageView;import com.hp.lessonhelper.R;import com.hp.lessonhelper.application.LessonHelperApplication;import com.nostra13.universalimageloader.core.DisplayImageOptions;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;import com.nostra13.universalimageloader.core.download.ImageDownloader.Scheme;/** * @author tangdekun * function 使用ImageLoader加载图片工具类 * */public class ImageLoadUtils {    //图片来源    public static final int RESOURCE_TYPE_INTERNET = 1;//网络图片    public static final int RESOURCE_TYPE_SDCARD = 2;//Sd图片    public static final int RESOURCE_TYPE_CONTENT_PROVIDER = 3;//content provider图片    public static final int RESOURCE_TYPE_ASSETS = 4;//assets图片    public static final int RESOURCE_TYPE_DRAWABLE = 5;//drawable图片    private static ImageLoadUtils instance;    //架包中的ImageLoader    private static ImageLoader imageLoader;    private DisplayImageOptions mImageOptions;    private Context mContext;    private ImageLoadUtils(){        mContext = LessonHelperApplication.getContext();    }    /**     * @return 单例模式     */    public static ImageLoadUtils getInstance(){        if(instance == null){            instance =  new ImageLoadUtils();        }        return instance;    }    /**     * 加载图片     * @param locationType  图片来源:1:网络;2:sd卡;3.Content provider ;4.assets ;5.drawable     * @param imageView     * @param imagePath     */    public void displayImage(int resourceType ,ImageView imageView, String imagePath){        if (imageLoader == null) {            imageLoader = ImageLoader.getInstance();            imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));            mImageOptions = new DisplayImageOptions.Builder()                        .showImageOnFail(R.drawable.ic_launcher)                          .showImageForEmptyUri(R.drawable.ic_launcher)                        .cacheInMemory(true)  //                        .cacheOnDisc(true)                          .bitmapConfig(Bitmap.Config.RGB_565)  //在DisplayImageOptions选项中配置bitmapConfig为Bitmap.Config.RGB_565,因为默认是ARGB_8888, 使用RGB_565会比使用ARGB_8888少消耗2倍的内存                        .build();          }        switch (resourceType) {        case RESOURCE_TYPE_INTERNET:            imageLoader.displayImage(imagePath, imageView, mImageOptions);              break;       //当数据来源于sd,Content provider,drawable,assets中,使用的时候也很简单,我们只需要给每个图片来源的地方加上Scheme包裹起来(Content provider除外),然后当做图片的url传递到imageLoader中,Universal-Image-Loader框架会根据不同的Scheme获取到输入流          case RESOURCE_TYPE_CONTENT_PROVIDER:            imageLoader.displayImage(imagePath, imageView, mImageOptions);              break;        case RESOURCE_TYPE_SDCARD:            String imageUrl = Scheme.FILE.wrap(imagePath);             imageLoader.displayImage(imageUrl, imageView, mImageOptions);              break;        case RESOURCE_TYPE_ASSETS:             //图片来源于assets              String assetsUrl = Scheme.ASSETS.wrap(imagePath);              imageLoader.displayImage(assetsUrl, imageView, mImageOptions);              break;        case RESOURCE_TYPE_DRAWABLE:             //图片来源于drawable              String drawableUrl = Scheme.DRAWABLE.wrap(imagePath);            imageLoader.displayImage(drawableUrl, imageView, mImageOptions);              break;        default:            break;        }    }}

使用Application中提供Context,规避内存泄漏问题

public class LessonHelperApplication extends Application {    //内存泄漏监听器对象    private static Context context;    public static Context getContext(){            return context;    }    @Override    public void onCreate() {        super.onCreate();        context = this;    }
0 0
原创粉丝点击