package com.workdemo;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.color;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
public class MyImageView extends ImageView {
private String url;
private AsynImageLoader asynImageLoader;
public MyImageView(Context context) {
this(context, null);
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
/**
* 通过String地址下载图片
*
* @return
*
*/
public Bitmap downloadPicture() {
Bitmap bitmap = null;
try {
URL pictureUrl = new URL(url);
InputStream in = pictureUrl.openStream();
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
public void fillView() {
if (asynImageLoader == null)
asynImageLoader = new AsynImageLoader();
asynImageLoader.showImageAsyn(this, url, R.drawable.process_anim);
}
public void setUrl(String url) {
this.url = url;
}
class AsynImageLoader {
private static final String TAG = "AsynImageLoader";
private Map<String, SoftReference<Bitmap>> caches;
private List<Task> taskQueue;
private boolean isRunning = false;
private AnimationDrawable drawable;
private Task task;
public AsynImageLoader() {
caches = new HashMap<String, SoftReference<Bitmap>>();
taskQueue = new ArrayList<MyImageView.Task>();
isRunning = true;
new Thread(runnable).start();
}
/**
*
* @param imageView
* 需要延迟加载图片的对象
* @param url
* 图片的URL地址
* @param resId
* 图片加载过程中显示的等待动画资源ID
*/
public void showImageAsyn(ImageView imageView, String url, int resId) {
imageView.setTag(url);
Bitmap bitmap = loadImageAsyn(url,
getImageCallback(imageView, resId));
if (bitmap == null) {
// 缓存没有该图片,显示下载等待动画
imageView.setBackgroundResource(resId);
drawable = (AnimationDrawable) imageView.getBackground();
drawable.start();
} else {
imageView.setImageBitmap(bitmap);
}
}
public Bitmap loadImageAsyn(String path, ImageCallback callback) {
if (caches.containsKey(path)) {
SoftReference<Bitmap> rf = caches.get(path);
Bitmap bitmap = rf.get();
if (bitmap == null) {// GC freed this
caches.remove(path);
} else {
Log.i(TAG, "return image in cache" + path);
return bitmap;
}
} else {
task = new Task();
task.path = path;
task.callback = callback;
Log.i(TAG, "new Task ," + path);
if (!taskQueue.contains(task)) {
taskQueue.add(task);
// 唤醒任务下载队列
synchronized (runnable) {
runnable.notify();
}
}
}
// 缓存中没有图片则返回null
return null;
}
/**
*
* @param imageView
* @param resId
* 图片加载完成前显示的等待动画资源ID
* @return
*/
private ImageCallback getImageCallback(final ImageView imageView,
final int resId) {
return new ImageCallback() {
@Override
public void loadImage(String path, Bitmap bitmap) {
if (path.equals(imageView.getTag().toString())) {
imageView.setImageBitmap(bitmap);
drawable.stop();
// 设置图片背景为灰色,用为调试
imageView.setBackgroundColor(getResources().getColor(
color.darker_gray));
} else {
imageView.setImageResource(resId);
}
}
};
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Task task = (Task) msg.obj;
// 调用callback对象的loadImage方法,并将图片路径和图片回传
task.callback.loadImage(task.path, task.bitmap);
}
};
private Runnable runnable = new Runnable() {
@Override
public void run() {
while (isRunning) {
while (taskQueue.size() > 0) {
Task task = taskQueue.remove(0);
task.bitmap = downloadPicture();
caches.put(task.path, new SoftReference<Bitmap>(
task.bitmap));
if (handler != null) {
Message msg = handler.obtainMessage();
msg.obj = task;
handler.sendMessage(msg);
}
}
// 如果队列为空,则令线程等待
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
public Task getTask() {
return task;
}
}
class Task {
String path;
Bitmap bitmap;
ImageCallback callback;
@Override
public boolean equals(Object o) {
Task task = (Task) o;
return task.path.equals(path);
}
}
// 回调接口
public interface ImageCallback {
void loadImage(String path, Bitmap bitmap);
}
public void Recycle(){
if(!asynImageLoader.getTask().bitmap.isRecycled()){
asynImageLoader.getTask().bitmap.recycle();
}
}
}
显示下载的图片的imageview
最新推荐文章于 2020-11-19 18:28:38 发布