• 从内存缓存图片


    从内存缓存图片

    MemoryCacheUtils

     1 public class MemoryCacheUtils {
     2  
     3     private HashMap<String, Bitmap> hashlist=new HashMap<String, Bitmap>();
     4     /**
     5      * 
     6      * 从内存中读
     7      * @param url
     8      * @return
     9      */
    10     public  Bitmap getBitmapFrommemory(String url){
    11         Bitmap bitmap = hashlist.get(url);
    12         return bitmap;
    13     }
    14     
    15 
    16     /**
    17      * 
    18      * 写入内存
    19      * @param url
    20      * @param bitmap
    21      */
    22     public  void setBitmapTomemory(String url,Bitmap bitmap){
    23         hashlist.put(url, bitmap);
    24     }
    25 }

    2.在NetCacheUtils中调用setBitmapTomemory(String url,Bitmap bitmap)方法获取

      1 /**
      2  * 
      3  * 从网络下载图片
      4  * @author admin
      5  *
      6  */
      7 public class NetCacheUtils {
      8     private LocalCacheUtils mlocalcacheutils;
      9     private MemoryCacheUtils mmemorycacheutils;
     10 
     11     public NetCacheUtils(LocalCacheUtils localcacheutils, MemoryCacheUtils memorycacheutils) {
     12         mlocalcacheutils=localcacheutils;
     13         mmemorycacheutils=memorycacheutils;
     14     }
     15 
     16     public void getBitmapFromNet(ImageView iv_photo, String url) {
     17         // TODO Auto-generated method stub
     18         BitmapTask bitmaptask=new BitmapTask();
     19         bitmaptask.execute(iv_photo,url);//开启AsyncTask,参数在doInBackground获取
     20     }
     21     /*AsyncTask  异步任务即做一些简单的异步处理  ;是handle与线程池的封装
     22      * 第一个泛型:参数类型泛型
     23      * 第二个泛型:更新进度泛型
     24      * 第三个泛型:onProgressUpdate的返回结果的泛型
     25      * 
     26      */
     27     
     28     class BitmapTask extends AsyncTask<Object, Void, Bitmap>{
     29 
     30         private ImageView pic;
     31         private String murl;
     32         /**
     33          * 后台耗时方法在此执行,子线程
     34          */
     35         @Override
     36         protected Bitmap doInBackground(Object... params) {
     37             pic = (ImageView) params[0];
     38             murl = (String) params[1];
     39             
     40             pic.setTag(murl);//将图片与url绑定
     41             return downloadBitmap(murl);
     42         }
     43         /**
     44          * 更新进度,主线程
     45          */
     46         @Override
     47         protected void onProgressUpdate(Void... values) {
     48             // TODO Auto-generated method stub
     49             super.onProgressUpdate(values);
     50         }
     51         /**
     52          * 后台耗时方法结束之后,在此执行,主线程
     53          */
     54         @Override
     55         protected void onPostExecute(Bitmap result) {
     56             if(result!=null){
     57                 
     58                 String tag = (String) pic.getTag();
     59                 if(tag.equals(murl)){
     60                     pic.setImageBitmap(result);
     61                 }
     62                 
     63             }
     64             mlocalcacheutils.setBitmapTolocal(murl, result);
     65             mmemorycacheutils.setBitmapTomemory(murl, result);
     66             System.out.println("从网络上加载图片啦");
     67             
     68         }
     69     }
     70 
     71     /**
     72      * 
     73      * 下载图片
     74      * @return 
     75      */
     76     private Bitmap downloadBitmap(String url){
     77         HttpURLConnection conn=null;
     78         try {
     79             conn=(HttpURLConnection) new URL(url)
     80             .openConnection();
     81             
     82             conn.setConnectTimeout(5000);
     83             conn.setReadTimeout(5000);
     84             conn.setRequestMethod("GET");
     85             conn.connect();
     86             
     87             int responseCode = conn.getResponseCode();//响应码
     88             
     89             if(responseCode==200){//表示成功连接
     90                 InputStream inputStream = conn.getInputStream();
     91                 Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
     92                 return bitmap;
     93             }
     94             
     95         } catch (IOException e) {
     96             
     97             e.printStackTrace();
     98         }
     99         finally{
    100             conn.disconnect();
    101         }
    102         return null;
    103         
    104     }
    105 }

    3.在中调用Bitmap getBitmapFrommemory(String url)方法

     1 /**
     2  * 
     3  * 
     4  * 子定义图片加载工具
     5  * @author admin
     6  *
     7  */
     8 public class MyBitMaputils {
     9     NetCacheUtils netcache;
    10     LocalCacheUtils localcacheutils;
    11     MemoryCacheUtils memorycacheutils;
    12     public MyBitMaputils(){
    13         memorycacheutils=new MemoryCacheUtils();
    14         localcacheutils=new LocalCacheUtils();
    15         netcache=new NetCacheUtils(localcacheutils,memorycacheutils);
    16         
    17     }
    18     Bitmap bitmap =null;
    19     public void display(ImageView iv_photo, String url) {
    20         iv_photo.setImageResource(R.drawable.news_pic_default);//默认图片,防止图片的复用
    21         //内存缓存
    22         bitmap= memorycacheutils.getBitmapFrommemory(url);
    23         if(bitmap!=null){
    24             iv_photo.setImageBitmap(bitmap);
    25             System.out.println("从内存中读取图片");
    26             return;
    27         }
    28         //本地缓存
    29         bitmap = localcacheutils.getBitmapFromlocal(url);
    30         if(bitmap!=null){
    31             iv_photo.setImageBitmap(bitmap);
    32             memorycacheutils.setBitmapTomemory(url, bitmap);//若本地中有,则从本地获取
    33             System.out.println("从本地读取图片");
    34             return;//从本地读取就不需要从网络读取了
    35         }
    36         
    37         //网络缓存(第一次)
    38         netcache.getBitmapFromNet(iv_photo,url);
    39     }
    40 
    41     
    42 }
  • 相关阅读:
    Shell高级编程学习笔记(基础篇)
    基于Python的机器学习实战:Apriori
    基于Python的机器学习实战:AadBoost
    Theano教程:Python的内存管理
    基于theano的降噪自动编码器(Denoising Autoencoders--DA)
    基于theano的深度卷积神经网络
    推荐系统之矩阵分解及C++实现
    推荐系统之协同过滤的原理及C++实现
    一步一步详解ID3和C4.5的C++实现
    Principal components analysis(PCA):主元分析
  • 原文地址:https://www.cnblogs.com/wangying222/p/5270020.html
Copyright © 2020-2023  润新知