public class AsyncImageLoader {
private static final String TAG = "AsyncImageLoader";
//第一一个map集合,添加软引用。
private Map<String, SoftReference<Drawable>> imageCache=
new HashMap<String, SoftReference<Drawable>>();
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();
}
}
//handler时间处理
final Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
callback.imageLoaded((Drawable) msg.obj, imageUrl);
}
};
//开启一个线程。
new Thread(){
public void run() {
Drawable drawable=loadImageFromUrl(imageUrl);
imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
handler.sendMessage(handler.obtainMessage(0,drawable));
};
}.start();
return null;
}
protected Drawable loadImageFromUrl(String imageUrl) {
try {
//直接返回图片
return Drawable.createFromStream(new URL(imageUrl).openStream(),"src");
} catch (Exception e) {
}
return null;//返回空
}
//第一一个接口
public interface ImageCallback{
public void imageLoaded(Drawable imageDrawable,String imageUrl);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
关于接口的一些特点:
接口的变量一般都为常量。
接口的方法没方法是实现方式,不能被实例化。
接口没构造方法。
接口不能实现接口,但是接口可以继承接口
接口的方法可以通过类来实现。
下面是我写的小介绍案例
interface Animal{ void eatfood(); void running();} class Dog implements Animal{ public void eatfood(){ System.out.println("开始吃骨头啦!"); } public void running(){ System.out.println("小狗跑来跑去!"); }} class Cat implements Animal{ public void eatfood(){ System.out.println("小猫吃鱼!"); } public void running(){ System.out.println("小猫树上跑!"); }} public class InterTest{ public static void main(String[] args) { Animal p=new Animal(); p.eatfood(); p.running(); p=new Dog(); p.eatfood(); p.running(); p=new Cat(); p.eatfood(); p.running(); }}
<span style="font-size:18px;"><span style="color:#333333;background: white;"></span></span>
<span style="color:#909090;">------- </span><a target=_blank style="color: rgb(126, 93, 58);" href="http://www.itheima.com/" target="blank" rel="nofollow">android培训</a><span style="color:#909090;">、</span><a target=_blank style="color: rgb(126, 93, 58);" href="http://www.itheima.com/" target="blank" rel="nofollow">java培训</a><span style="color:#909090;">、</span><a target=_blank style="color: rgb(126, 93, 58);" href="http://www.itheima.com/" target="blank" rel="nofollow">IOS培训</a><span style="color:#909090;">、</span><a target=_blank style="color: rgb(126, 93, 58);" href="http://www.itheima.com/" target="blank" rel="nofollow">.Net培训</a><span style="color:#909090;">期待与您交流! ----------</span>