package com.example.android.demo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v4.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.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
/**
* Created by Howy on 2017/2/12.
*/
public class ImageLoader {
private ImageView mImageView;
private String mUrl;
private LruCache<String, Bitmap> mCaches;
private ListView mListView;
private Set<NewsAsyncTask> mTasks;
public ImageLoader(ListView listView) {
this.mListView = listView;
mTasks = 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) {
return value.getByteCount();
}
};
}
public void addBitmapToCache(String url, Bitmap bitmap) {
if (getBitmapfromCache(url) == null) {
mCaches.put(url, bitmap);
}
}
public Bitmap getBitmapfromCache(String url) {
return mCaches.get(url);
}
private Handler handler = 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 imageUrl) {
mImageView = imageView;
mUrl = imageUrl;
new Thread() {
@Override
public void run() {
super.run();
Bitmap bitmap = getBitmapFromURL(imageUrl);
Message message = Message.obtain();
message.obj = bitmap;
handler.sendMessage(message);
}
}.start();
}
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);
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public void showImageByAsyncTask(ImageView imageView, final String imageUrl) {
Bitmap bitmap = getBitmapfromCache(imageUrl);
if (bitmap == null) {
imageView.setImageResource(R.mipmap.ic_launcher);
} else {
imageView.setImageBitmap(bitmap);
}
}
class NewsAsyncTask extends AsyncTask<String, Void, Bitmap> {
// private ImageView mImageView;
private String mUrl;
public NewsAsyncTask(String url) {
// this.mImageView = mImageView;
this.mUrl = url;
}
@Override
protected Bitmap doInBackground(String... params) {
String url = params[0];
Bitmap bitmap = getBitmapFromURL(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);
}
mTasks.remove(this);
}
}
public void loadImage(int start, int end) {
for (int i = start; i < end; i++) {
String url = NewsListAdapter.URLS[i];
Bitmap bitmap = getBitmapfromCache(url);
if (bitmap == null) {
NewsAsyncTask mAsyncTask = new NewsAsyncTask(url);
mAsyncTask.execute(url);
mTasks.add(mAsyncTask);
} else {
ImageView imageView = (ImageView) mListView.findViewWithTag(url);
imageView.setImageBitmap(bitmap);
}
}
}
public void cancelAllTasks() {
if (mTasks != null) {
for (NewsAsyncTask task : mTasks) {
task.cancel(false);
}
}
}
}