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

架包位于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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值