Android——实现ImageLoader工具类(附带修改圆形头像)

很多时候多次用到从网上根据图片地址下载图片到本ImageView里面。

不用每次都调用那么多代码,减少冗余,可以自己定义一个工具类来多次调用即可。

ImageLoader工具类



public class ImageLoaderUtils {
    private static Builder icf;
    private static ImageLoader imageloader;
    //ImageLoader的初始化工作
    public static void initImagLoader(Context context)
    {
        imageloader = ImageLoader.getInstance();
        icf = new ImageLoaderConfiguration.Builder(context);
        File file = new File("/mnt/sdcard/cache/");//缓存的位置
        DiskCache diskCache = new UnlimitedDiscCache(file);
        icf.diskCache(diskCache);
        icf.diskCacheSize(10 * 1024 * 1024);
        imageloader.init(icf.build());
    }

    //获得图片缓存

    public static Bitmap getDiscCacheImage( String uri){//这里的uri一般就是图片网址    
        File file = DiskCacheUtils.findInCache(uri,  imageloader.getDiskCache());    
        try {  

            String path= file.getPath();  
            return BitmapFactory.decodeFile(path);  


       } catch (Exception e) {  
           // TODO: handle exception  
           e.printStackTrace();  
       }  


        return null;    
    } 

    //提供图片地址URL,一个ImageView的控件对象即可将网上图片加载到本控件。
    public static void getImageLoader(String url, ImageView iv,Context context) {
        // TODO Auto-generated method stub
        ImageLoaderUtils.initImagLoader(context);

        DisplayImageOptions dio = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.default_user_leftdrawer)// 设置图片在下载期间显示的图片
                .showImageForEmptyUri(R.drawable.default_user_leftdrawer)// 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.default_user_leftdrawer)// 设置图片加载/解码过程中错误时候显示的图片
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(200))// 是否设置为圆角,弧度为多少
                .displayer(new FadeInBitmapDisplayer(0))// 是否图片加载好后渐入的动画时间
                .build();

        imageloader.displayImage(url, iv, dio);
    }
    //String url 网上图片地址,根据图片转换成圆形头像
    public static void getYuanImg(ImageView iv,Context context,String url)
    {
        ImageLoaderUtils.initImagLoader(context);
        Bitmap bm = BitmapUtils.createCircleImage(ImageLoaderUtils.getDiscCacheImage(url));
        iv.setImageBitmap(bm);
    }
}

void getYuanImg()方法是用来将网上图片直接变成圆形图片,类似 QQ头像,需要用到一个方法BitMapUtils类。

public class BitmapUtils {
    /** 
     * 转换图片成圆形 
     *  
     * @param bitmap 传入Bitmap对象 
     * @return 
     */  
    public static Bitmap createCircleImage(Bitmap bitmap) {  
        int width = bitmap.getWidth();  
        int height = bitmap.getHeight();  
        float roundPx;  
        float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;  
        if (width <= height) {  
            roundPx = width / 2;  
            top = 0;  
            bottom = width;  
            left = 0;  
            right = width;  
            height = width;  
            dst_left = 0;  
            dst_top = 0;  
            dst_right = width;  
            dst_bottom = width;  
        } else {  
            roundPx = height / 2;  
            float clip = (width - height) / 2;  
            left = clip;  
            right = width - clip;  
            top = 0;  
            bottom = height;  
            width = height;  
            dst_left = 0;  
            dst_top = 0;  
            dst_right = height;  
            dst_bottom = height;  
        }  
        Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);  
        Canvas canvas = new Canvas(output);  
        final int color = 0xff424242;  
        final Paint paint = new Paint();  
        final Rect src = new Rect((int) left, (int) top, (int) right,  
                (int) bottom);  
        final Rect dst = new Rect((int) dst_left, (int) dst_top,  
                (int) dst_right, (int) dst_bottom);  
        final RectF rectF = new RectF(dst);  
        paint.setAntiAlias(true);  
        canvas.drawARGB(0, 0, 0, 0);  
        paint.setColor(color);  
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
        canvas.drawBitmap(bitmap, src, dst, paint);  
        return output;  
    }

}

有很多人在登陆时候可能会遇到一个问题,在 int width = bitmap.getWidth(); 这里报

异常,这是因为你之前登陆已经有过头像信息,需要将该app数据清空,然后重新登

陆。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值