// 本地照片绝对路径
String imageUrl = (String) t;
// 得到 ImageView
ImageView grid_item = holder.getView(R.id.grid_item);
// 设置 tag ,标记用的,反正图片显示错位
grid_item.setTag(imageUrl);
/**
* 显示 图片
*
* @param context
* : 上下文
* @param imageView
* : ImageView 控件
* @param sourcePath
* : 图片 地址
* @param r_Id
* : 默认 图片 id ,R.drowable.id;
* @param callback
* :图片显示 回调
*/
new ImageLoader().displayBmp(mContext,grid_item, imageUrl, R.drawable.img_bg,this);
public ImageLoader() {
// 取应用内存的 8/1 作为 图片缓存用
int cacheSize = maxMemory / 8;
// 得到 LruCache
mLruCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getByteCount();
}
};
}
/**
* 将图片存储到LruCache
*/
public void putBitmapToLruCache(String key, Bitmap bitmap) {
if (getBitmapFromLruCache(key) == null && mLruCache != null) {
mLruCache.put(key, bitmap);
}
}
/**
* 从LruCache缓存获取图片
*/
public Bitmap getBitmapFromLruCache(String key) {
return mLruCache.get(key);
}
/**
* 显示 图片
*
* @param context
* : 上下文
* @param imageView
* : ImageView 控件
* @param sourcePath
* : 图片 地址
* @param r_Id
* : 默认 图片 id ,R.drowable.id;
* @param callback
* :图片显示 回调
*/
public void displayBmp(final Context context, final ImageView imageView, final String sourcePath, final int r_Id,
final ImageCallback callback) {
final String path;
if (!TextUtils.isEmpty(sourcePath)) {
path = sourcePath;
} else {
return;
}
// 先 试着 从 缓存 得到 图片 , path 作为 图片的 key
Bitmap bmp = mLruCache.get(path);
if (bmp != null) {
if (callback != null) {
// 回调 图片 显示
callback.imageLoad(imageView, bmp, sourcePath);
}
// imageView.setImageBitmap(bmp);
return;
}
// 如果 bmp == null ,给 imageView 显示默认图片
imageView.setImageResource(r_Id);
// 启动 线程池
threadPoolUtils.getExecutorService().execute(new Runnable() {
Bitmap bitmap = null;
@Override
public void run() {
// TODO Auto-generated method stub
try {
// 加载 图片 地址 对应 的 缩略图
bitmap = revitionImageSize(imageView, sourcePath);
} catch (Exception e) {
}
if (bitmap == null) {
try {
// 如果 缩略图 没加载成功 显示 默认 设置的图片
bitmap = BitmapFactory.decodeResource(context.getResources(), r_Id);
} catch (Exception e) {
}
}
if (path != null && bitmap != null) {
// 将 缩略图 放进 缓存 , path 作为 key
putBitmapToLruCache(path, bitmap);
}
if (callback != null) {
handler.post(new Runnable() {
@Override
public void run() {
// 回调 图片 显示
callback.imageLoad(imageView, bitmap, sourcePath);
}
});
}
}
});
}
public Bitmap revitionImageSize(ImageView imageView, String path) throws IOException {
// 得到 布局 ImageView 的 宽高
int img_width = imageView.getWidth();
int img_height = imageView.getHeight();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
in.close();
int height = options.outHeight;
int width = options.outWidth;
Bitmap bitmap = null;
int inSampleSize = 1;
// 计算出实际宽高和目标宽高的比率
final int heightRatio = Math.round((float) height / (float) img_height);
final int widthRatio = Math.round((float) width / (float) img_width);
// 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高
// 一定都会大于等于目标的宽和高。
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
// 调用上面定义的方法计算inSampleSize值
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
in = new BufferedInputStream(new FileInputStream(new File(path)));
bitmap = BitmapFactory.decodeStream(in, null, options);
in.close();
return bitmap;
}
int height = options.outHeight; int width = options.outWidth;
/**
* 显示图片回调
*
* @author Administrator
*
*/
public interface ImageCallback {
public void imageLoad(ImageView imageView, Bitmap bitmap, Object... params);
}
/**
* 图片 缓存回调
*/
@Override
public void imageLoad(ImageView imageView, Bitmap bitmap, Object... params) {
if (imageView != null && bitmap != null) {
String url = (String) params[0];
// 判断 这里的 url 是否 对应 imageView.getTag()
// 如果 将这句 判断 去掉 那么 就会出现 经常出现的 图片 显示 错位 问题 !!!!
if (url != null && url.equals((String) imageView.getTag())) {
((ImageView) imageView).setImageBitmap(bitmap);
}
}
}
dependencies {
compile 'com.zts:imageloader:1.1.1'
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有