import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;
import android.widget.ImageView;
import android.widget.ListView;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
/**
* 项目名: NewsDemo
* 包名: com.darrow.newsdemo
* 文件名: ImageLoader
* 创建者: Dw
* 创建时间: 2017/3/11 0011 下午 12:23
* 描述: 处理图片的加载
*/
public class ImageLoader {
private ImageView mImageView;
private String mUrl;
//创建Caches
private LruCache<String,Bitmap> mCaches;
private ListView mListView;
private Set<NewsAsyncTask> mTask;
public ImageLoader(ListView listView){
mListView = listView;
mTask = new HashSet<>();
//先获取当前应用所可用的内存大小
int maxMemory = (int) Runtime.getRuntime().maxMemory();
//设置所需要的缓存大小
int cacheSize = maxMemory/4;
mCaches = new LruCache<String,Bitmap>(cacheSize){
//在每次加入内存缓存的时候调用
@Override
protected int sizeOf(String key, Bitmap value) {
//返回bitmap的实际大小
return value.getByteCount();
}
};
}
//加入到缓存
public void addBitmapToCache(String url,Bitmap bitmap){
//先判断当前缓存是否存在
if(getBitmapFromURL(url) == null){
mCaches.put(url,bitmap);
}
}
//从缓存中获取数据
public Bitmap getBitmapFromCache(String url){
return mCaches.get(url);
}
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//对身份认证进行判断
if (mImageView.getTag().equals(mUrl)) {
mImageView.setImageBitmap((Bitmap) msg.obj);
}
}
};
//使用多线程方法加载图片
public void showImageByThread(ImageView imageView,final String url){
mImageView = imageView;
mUrl = url;
new Thread(){
@Override
public void run() {
super.run();
Bitmap bitmap = getBitmapFromURL(url);
//发送message给Handler处理
//通过这种方式可以使用message已提取到的消息,从而提高效率
Message message = Message.obtain();
message.obj = bitmap;
mHandler.sendMessage(message);
}
}.start();
}
//通过url转化成bitmap对象
public Bitmap getBitmapFromURL(String urlString){
Bitmap bitmap;
InputStream is = null;
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
is = new BufferedInputStream(connection.getInputStream());
bitmap = BitmapFactory.decodeStream(is);
//别忘了最后将资源释放
connection. disconnect();
//添加一个模拟网络加载慢的异常。
//Thread.sleep(1000);
is.close();
return bitmap;
} catch (java.io.IOException e) {
e.printStackTrace();
} finally {
//将InputStream释放
try {
is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
//使用AsyncTask方式加载图片
public void showImageByAsyncTask(ImageView imageView,String url ){
//从缓存中取出图片
Bitmap bitmap = getBitmapFromCache(url);
//判断缓存,如果没有那么必须去下载
if (bitmap == null){
imageView.setImageResource(R.mipmap.ic_launcher);
}else{
imageView.setImageBitmap(bitmap);
}
}
//用来加载从start到end的所有图片
public void loadImages(int start,int end){
for (int i= start;i<end;i++){
String url = NewsAdapter.URLS[i];
//从缓存中取出图片
Bitmap bitmap = getBitmapFromCache(url);
//判断缓存,如果没有那么必须去下载
if (bitmap == null){
NewsAsyncTask task = new NewsAsyncTask(url);
task.execute(url);
mTask.add(task);
}else{
ImageView imageView = (ImageView) mListView.findViewWithTag(url);
imageView.setImageBitmap(bitmap);
}
}
}
//取消所有任务
public void cancelAllTasks() {
if (mTask != null){
for (NewsAsyncTask task : mTask){
task.cancel(false);
}
}
}
private class NewsAsyncTask extends AsyncTask<String,Void,Bitmap>{
// private ImageView mImageView;
private String mUrl;
public NewsAsyncTask(String url){
// mImageView = imageView;
mUrl = url;
}
@Override
protected Bitmap doInBackground(String... params) {
String url = params[0];
//从网络上获取图片
Bitmap bitmap = getBitmapFromCache(url);
if(bitmap != null){
//下载完图片后应该保存到缓存中,以便下次使用
addBitmapToCache(url,bitmap);
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
ImageView imageView = (ImageView) mListView.findViewWithTag(mUrl);
if (imageView != null && bitmap != null) {
imageView.setImageBitmap(bitmap);
}
mTask.remove(this);
}
}
}