- 浏览: 73123 次
- 性别:
- 来自: 西安
文章分类
- 全部博客 (101)
- Android (45)
- java中多线程的实现 (1)
- Runnable (2)
- Thread (1)
- TCP网络编程 (1)
- PHP (1)
- java (4)
- JDBC (1)
- oauth登录 (1)
- 中文乱码 (1)
- Ajax (1)
- web (2)
- Mysql (2)
- HTML5 (0)
- HTML5 (1)
- entity not found (1)
- JQuery (2)
- 使用jsp和Servlet实现一个验证码 (1)
- 验证码 (1)
- 异常 (1)
- webService (2)
- not insert异常 (1)
- JAVAmail (1)
- 选中分享 (1)
- 控件 (1)
- 方法 (1)
- listView (1)
- android控件 (1)
- jqueryMobile (1)
- servlet开发中文乱码解决方案 (1)
- servlet开发中文乱码解决方案 (1)
- web文本框初始提示 (1)
- web文本框初始提示 (0)
- xml (1)
- java中的加密技术 (1)
- 互联网 (1)
- mysql数据库 (1)
- java单例模式 (1)
- gson转json (1)
最新评论
转载自:
http://www.open-open.com/lib/view/open1384349254555.html
缓存类就一个:
AsyncBitmapLoader
package com.ikuaishou.kuaishou.utils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.os.Handler;
import android.widget.ImageView;
import com.ikuaishou.kuaishou.R;
public class AsyncBitmapLoader {
// 为了加快速度,在内存中开启缓存(主要应用于重复图片较多时,或者同一个图片要多次被访问,比如在ListView时来回滚动)
public Map<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>();
private ExecutorService executorService = Executors.newFixedThreadPool(5); // 固定五个线程来执行任务
private final Handler handler = new Handler();
// SD卡上图片储存地址
private final String path = Environment.getExternalStorageDirectory()
.getPath() + "/ikuaishou";
/**
*
* @param imageUrl
* 图像url地址
* @param callback
* 回调接口
* @return 返回内存中缓存的图像,第一次加载返回null
*/
public Drawable loadDrawable(final String imageUrl,
final ImageCallback callback) {
// 如果缓存过就从缓存中取出数据
if (imageCache.containsKey(imageUrl)) {
SoftReference<Drawable> softReference = imageCache.get(imageUrl);
if (softReference.get() != null) {
return softReference.get();
}
} else if (useTheImage(imageUrl) != null) {
return useTheImage(imageUrl);
}
// 缓存中没有图像,则从网络上取出数据,并将取出的数据缓存到内存中
executorService.submit(new Runnable() {
public void run() {
try {
final Drawable drawable = Drawable.createFromStream(
new URL(imageUrl).openStream(), "image.png");
imageCache.put(imageUrl, new SoftReference<Drawable>(
drawable));
handler.post(new Runnable() {
public void run() {
callback.imageLoaded(drawable);
}
});
saveFile(drawable, imageUrl);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
return null;
}
// 从网络上取数据方法
public Drawable loadImageFromUrl(String imageUrl) {
try {
return Drawable.createFromStream(new URL(imageUrl).openStream(),
"image.png");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 对外界开放的回调接口
public interface ImageCallback {
// 注意 此方法是用来设置目标对象的图像资源
public void imageLoaded(Drawable imageDrawable);
}
// 引入线程池,并引入内存缓存功能,并对外部调用封装了接口,简化调用过程
public void LoadImage(final String url, final ImageView iv) {
if (iv.getImageMatrix() == null) {
iv.setImageResource(R.drawable.headimgurl);
}
// 如果缓存过就会从缓存中取出图像,ImageCallback接口中方法也不会被执行
Drawable cacheImage = loadDrawable(url,new ImageCallback() {
@Override
public void imageLoaded(Drawable imageDrawable) {
iv.setImageDrawable(imageDrawable);
}
});
if (cacheImage != null) {
iv.setImageDrawable(cacheImage);
}
}
/**
* 保存图片到SD卡上
*
* @param bm
* @param fileName
*
*/
public void saveFile(Drawable dw, String url) {
try {
BitmapDrawable bd = (BitmapDrawable) dw;
Bitmap bm = bd.getBitmap();
// 获得文件名字
final String fileNa = url.substring(url.lastIndexOf("/") + 1,
url.length()).toLowerCase();
File file = new File(path + "/image/" + fileNa);
// 创建图片缓存文件夹
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
if (sdCardExist) {
File maiduo = new File(path);
File ad = new File(path + "/image");
// 如果文件夹不存在
if (!maiduo.exists()) {
// 按照指定的路径创建文件夹
maiduo.mkdir();
// 如果文件夹不存在
} else if (!ad.exists()) {
// 按照指定的路径创建文件夹
ad.mkdir();
}
// 检查图片是否存在
if (!file.exists()) {
file.createNewFile();
}
}
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file));
bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 使用SD卡上的图片
*
*/
public Drawable useTheImage(String imageUrl) {
Bitmap bmpDefaultPic = null;
// 获得文件路径
String imageSDCardPath = path
+ "/image/"
+ imageUrl.substring(imageUrl.lastIndexOf("/") + 1,
imageUrl.length()).toLowerCase();
File file = new File(imageSDCardPath);
// 检查图片是否存在
if (!file.exists()) {
return null;
}
bmpDefaultPic = BitmapFactory.decodeFile(imageSDCardPath, null);
if (bmpDefaultPic != null || bmpDefaultPic.toString().length() > 3) {
Drawable drawable = new BitmapDrawable(bmpDefaultPic);
return drawable;
} else
return null;
}
}
使用很简单:
asyncBitmapLoader.LoadImage(list.get(position).getPic(), holder.iv_pic);
http://www.open-open.com/lib/view/open1384349254555.html
缓存类就一个:
AsyncBitmapLoader
package com.ikuaishou.kuaishou.utils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.os.Handler;
import android.widget.ImageView;
import com.ikuaishou.kuaishou.R;
public class AsyncBitmapLoader {
// 为了加快速度,在内存中开启缓存(主要应用于重复图片较多时,或者同一个图片要多次被访问,比如在ListView时来回滚动)
public Map<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>();
private ExecutorService executorService = Executors.newFixedThreadPool(5); // 固定五个线程来执行任务
private final Handler handler = new Handler();
// SD卡上图片储存地址
private final String path = Environment.getExternalStorageDirectory()
.getPath() + "/ikuaishou";
/**
*
* @param imageUrl
* 图像url地址
* @param callback
* 回调接口
* @return 返回内存中缓存的图像,第一次加载返回null
*/
public Drawable loadDrawable(final String imageUrl,
final ImageCallback callback) {
// 如果缓存过就从缓存中取出数据
if (imageCache.containsKey(imageUrl)) {
SoftReference<Drawable> softReference = imageCache.get(imageUrl);
if (softReference.get() != null) {
return softReference.get();
}
} else if (useTheImage(imageUrl) != null) {
return useTheImage(imageUrl);
}
// 缓存中没有图像,则从网络上取出数据,并将取出的数据缓存到内存中
executorService.submit(new Runnable() {
public void run() {
try {
final Drawable drawable = Drawable.createFromStream(
new URL(imageUrl).openStream(), "image.png");
imageCache.put(imageUrl, new SoftReference<Drawable>(
drawable));
handler.post(new Runnable() {
public void run() {
callback.imageLoaded(drawable);
}
});
saveFile(drawable, imageUrl);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
return null;
}
// 从网络上取数据方法
public Drawable loadImageFromUrl(String imageUrl) {
try {
return Drawable.createFromStream(new URL(imageUrl).openStream(),
"image.png");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 对外界开放的回调接口
public interface ImageCallback {
// 注意 此方法是用来设置目标对象的图像资源
public void imageLoaded(Drawable imageDrawable);
}
// 引入线程池,并引入内存缓存功能,并对外部调用封装了接口,简化调用过程
public void LoadImage(final String url, final ImageView iv) {
if (iv.getImageMatrix() == null) {
iv.setImageResource(R.drawable.headimgurl);
}
// 如果缓存过就会从缓存中取出图像,ImageCallback接口中方法也不会被执行
Drawable cacheImage = loadDrawable(url,new ImageCallback() {
@Override
public void imageLoaded(Drawable imageDrawable) {
iv.setImageDrawable(imageDrawable);
}
});
if (cacheImage != null) {
iv.setImageDrawable(cacheImage);
}
}
/**
* 保存图片到SD卡上
*
* @param bm
* @param fileName
*
*/
public void saveFile(Drawable dw, String url) {
try {
BitmapDrawable bd = (BitmapDrawable) dw;
Bitmap bm = bd.getBitmap();
// 获得文件名字
final String fileNa = url.substring(url.lastIndexOf("/") + 1,
url.length()).toLowerCase();
File file = new File(path + "/image/" + fileNa);
// 创建图片缓存文件夹
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
if (sdCardExist) {
File maiduo = new File(path);
File ad = new File(path + "/image");
// 如果文件夹不存在
if (!maiduo.exists()) {
// 按照指定的路径创建文件夹
maiduo.mkdir();
// 如果文件夹不存在
} else if (!ad.exists()) {
// 按照指定的路径创建文件夹
ad.mkdir();
}
// 检查图片是否存在
if (!file.exists()) {
file.createNewFile();
}
}
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file));
bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 使用SD卡上的图片
*
*/
public Drawable useTheImage(String imageUrl) {
Bitmap bmpDefaultPic = null;
// 获得文件路径
String imageSDCardPath = path
+ "/image/"
+ imageUrl.substring(imageUrl.lastIndexOf("/") + 1,
imageUrl.length()).toLowerCase();
File file = new File(imageSDCardPath);
// 检查图片是否存在
if (!file.exists()) {
return null;
}
bmpDefaultPic = BitmapFactory.decodeFile(imageSDCardPath, null);
if (bmpDefaultPic != null || bmpDefaultPic.toString().length() > 3) {
Drawable drawable = new BitmapDrawable(bmpDefaultPic);
return drawable;
} else
return null;
}
}
使用很简单:
asyncBitmapLoader.LoadImage(list.get(position).getPic(), holder.iv_pic);
发表评论
-
Android自动化测试--Espresso框架使用
2016-11-01 10:02 716转载: Android自动化测试--Espresso框架 ... -
浅谈android中仅仅使用一个TextView实现高仿京东,淘宝各种倒计时
2016-11-01 09:54 1050转载:http://blog.csdn.net/u0130 ... -
利用apktool等工具发编译android apk
2016-10-25 09:15 712这个是我的csdn中的一篇关于android app反编译的文 ... -
Error:Execution failed for task ':app:mergeDebugResources'. > Some file crunchin
2016-10-08 10:30 1000向studio中导入微信支付Demo的时候报错了,具体如图: ... -
android studio实用快捷键收集
2016-09-30 17:51 457本人用android studio刚开始,做一下笔记,只适用于 ... -
android中事件分发机制
2016-09-22 11:52 540转载自:http://www.cnblogs.com/linj ... -
一个对sharedpreferences 数据进行加密的开源库
2016-09-18 14:30 619http://www.cnblogs.com/zhaoyanj ... -
LinerLayout滑动后停在顶部
2016-09-18 12:08 653转载自:http://blog.csdn.net/ff2008 ... -
android studio入门知识
2016-09-06 18:11 689http://blog.csdn.net/jdsjlzx/ar ... -
android图片加载OOM解决方案
2016-08-30 15:17 457转载自:http://www.apkbus.com/blog- ... -
android oom连带问题,以及tag错位问题结局方案
2016-08-30 13:48 641http://www.apkbus.com/blog-8430 ... -
android新手指导
2016-08-30 11:59 371http://www.apkbus.com/forum.php ... -
android app自动化测试
2016-07-12 15:51 488android sdk的lib目录下有个monkeyrunne ... -
利用BadgeView实现数字提醒效果
2016-07-06 16:38 939BadgeView是一个第三方开源库, github地址:ht ... -
android文件存储文本
2016-05-17 17:26 468/** * 写入文本 */ private void ... -
android listView的BaseAdapter的抽取
2016-05-12 11:52 623/** * BaseAdapter的抽取 * @autho ... -
android全局异常捕获并发送异常到邮箱
2016-05-12 11:41 1087public class AppException exten ... -
android第三方框架xutils的使用
2016-05-12 11:35 819这里写一些网络请求的相关 //初始化相关参数和对象 publi ... -
android端版本更新
2016-05-12 11:26 695整个思路,先判断服务端当前版本是不是高于本地版本,高的话可以选 ... -
android端从相册或者拍照读取图片
2016-04-16 16:45 792android端从相册或者拍照读取图片,见demo
相关推荐
Android 实现ListView滚动到底部自动加载数据
实现listview自动循环滚动
c# listview 实现循环滚动显示
Android使用缓存优化ListView
android的viewpager+fragment+listView图片缓存,图片缓存使用开源图片下载框架universal-image-loader
监听listview垂直滚动的距离,listview item的高度相同不同都适用,博客地址 http://blog.csdn.net/brastom/article/details/50638274
android listView图片异步加载(拖动时不加载,双缓存)
1 已经成功实现对ListView滚动条的自定义,定义的效果和新浪微博,QQ微博...的效果一样。 2 实现了对ListView分批量加载数据,滑动到ListView底部的时候会自动加载数据。
因项目需要循环展示列表数据,所以就实现了这个无限循环滚动的 LIstView.先说一下原理,原理呢,其实很简单,首先将要展示的数据循环展示三遍 监听ListView的滚动事件,当ListView滚动到第一遍第第二个时,ListView...
android 横向滚动listview,和自带的listview用法一致
我们在使用ListView时 用户时长会 连续滚动数屏,且滚动过程中会触发所有的getView的异步任务,getView线程加载图片则会 开很多条线程或者是线程池不够用 堵塞在 前面的getView任务中。直到 逐个执行完成 到达 当前...
androidlistview 自动往上滚动。效果很炫
知识点:1、解决listView滚动时checkBox的选择状态会发生变化(絮乱);2、ListView的Item长按,点击时间并存,实现同一UI控件多需求响应各类事件;3、点击ListViewItem选中响应Item里的CheckBox;4、计算选中的项数...
ListView对应多个布局的实现方式。ListView滚动中布局错误的解决方案,博客中对于的介绍博文
Android实现带图标的ListView
Android使用ListView实现时间轴
Android项目实战-ListView异步图片加载及压缩缓存
Android支持横行滚动的ListView控件
android studio实现listview的增加,左滑删除 listview以表格样式实现
android listview嵌套listview实现折叠列表