Xutils之BitmapUtils使用

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

转自:http://blog.csdn.net/rain_butterfly/article/details/37812371

	private void loadBitmap(){
		BitmapUtils bitmapUtils = new BitmapUtils(this);

		// 加载网络图片,以下都可以配置BitmapDisplayConfig,BitmapLoadCallBack
//		bitmapUtils.display(imgView, "http://bbs.lidroid.com/static/image/common/logo.png");

		// 加载本地图片(路径以/开头, 绝对路径)
		bitmapUtils.display(imgView, Environment.getExternalStorageDirectory()+"/ic_launcher.png");
//
//		// 加载assets中的图片(路径以assets开头)
//		bitmapUtils.display(imgView, "assets/img/wallpaper.jpg");

		// 使用ListView等容器展示图片时可通过PauseOnScrollListener控制滑动和快速滑动过程中时候暂停加载图片
//		listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));
//		listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true, customListener));
	}

自定义加载图片类,设置了默认图片、加载失败时的图片和压缩类型以及动画效果:

package com.home.testxutils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.widget.ImageView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.bitmap.callback.DefaultBitmapLoadCallBack;

public class XUtilsImageLoader {//框架里面设置了缓存和异步操作,不用单独设置线程池和缓存机制(也可以自定义缓存路径)  
  
    private BitmapUtils bitmapUtils;  
    private Context mContext;  
  
    public XUtilsImageLoader(Context context) {  
        this.mContext = context;  
        bitmapUtils = new BitmapUtils(mContext);  
        bitmapUtils.configDefaultLoadingImage(R.drawable.defaultimg);//默认背景图片  
        bitmapUtils.configDefaultLoadFailedImage(R.drawable.defaultimg);//加载失败图片  
        bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);//设置图片压缩类型  
  
    }  
    /** 
     *  
     * @author sunglasses 
     * @category 图片回调函数 
     */  
    public class CustomBitmapLoadCallBack extends  
            DefaultBitmapLoadCallBack<ImageView> {  
  
        @Override  
        public void onLoading(ImageView container, String uri,  
                BitmapDisplayConfig config, long total, long current) {  
        }  
  
        @Override  
        public void onLoadCompleted(ImageView container, String uri,  
                Bitmap bitmap, BitmapDisplayConfig config, BitmapLoadFrom from) {  
            // super.onLoadCompleted(container, uri, bitmap, config, from);  
            fadeInDisplay(container, bitmap);  
        }  
  
        @Override  
        public void onLoadFailed(ImageView container, String uri,  
                Drawable drawable) {  
        }  
    }  
  
    private static final ColorDrawable TRANSPARENT_DRAWABLE = new ColorDrawable(  
            android.R.color.transparent);  
    /** 
     * @author sunglasses 
     * @category 图片加载效果 
     * @param imageView 
     * @param bitmap 
     */  
    private void fadeInDisplay(ImageView imageView, Bitmap bitmap) {//目前流行的渐变效果  
        final TransitionDrawable transitionDrawable = new TransitionDrawable(  
                new Drawable[] { TRANSPARENT_DRAWABLE,  
                        new BitmapDrawable(imageView.getResources(), bitmap) });  
        imageView.setImageDrawable(transitionDrawable);  
        transitionDrawable.startTransition(500);  
    }  
    public void display(ImageView container,String url){//外部接口函数  
        bitmapUtils.display(container, url,new CustomBitmapLoadCallBack());  
    }  
}  

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值