• listview加载网络图片


     
    public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {
    
    	    private ListView listView;
    	    private AsyncImageLoader asyncImageLoader;
    
    	    public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts, ListView listView) {
    	        super(activity, 0, imageAndTexts);
    	        this.listView = listView;
    	        asyncImageLoader = new AsyncImageLoader();
    	    }
    
    	    public View getView(int position, View convertView, ViewGroup parent) {
    	        Activity activity = (Activity) getContext();
    
    	        // Inflate the views from XML
    	        View rowView = convertView;
    	        ViewCache viewCache;
    	        if (rowView == null) {
    	            LayoutInflater inflater = activity.getLayoutInflater();
    	            rowView = inflater.inflate(R.layout.image_and_text_row, null);
    	            viewCache = new ViewCache(rowView);
    	            rowView.setTag(viewCache);
    	        } else {
    	            viewCache = (ViewCache) rowView.getTag();
    	        }
    	        ImageAndText imageAndText = getItem(position);
    
    	        // Load the image and set it on the ImageView
    	        String imageUrl = imageAndText.getImageUrl();
    	        ImageView imageView = viewCache.getImageView();
    	        imageView.setTag(imageUrl);
    	        Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
    	            public void imageLoaded(Drawable imageDrawable, String imageUrl) {
    	                ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
    	                if (imageViewByTag != null) {
    	                    imageViewByTag.setImageDrawable(imageDrawable);
    	                }
    	            }
    	        });
    			if (cachedImage == null) {
    				imageView.setImageResource(R.drawable.default_image);
    			}else{
    				imageView.setImageDrawable(cachedImage);
    			}
    	        // Set the text on the TextView
    	        TextView textView = viewCache.getTextView();
    	        textView.setText(imageAndText.getText());
    
    	        return rowView;
    	    }
    
    }
    -------------------------------------------------------------------------------------------------
    public class AsyncImageLoader {

         private HashMap<String, SoftReference<Drawable>> imageCache;
          
             public AsyncImageLoader() {
                 imageCache = new HashMap<String, SoftReference<Drawable>>();
             }
          
             public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
                 if (imageCache.containsKey(imageUrl)) {
                     SoftReference<Drawable> softReference = imageCache.get(imageUrl);
                     Drawable drawable = softReference.get();
                     if (drawable != null) {
                         return drawable;
                     }
                 }
                 final Handler handler = new Handler() {
                     public void handleMessage(Message message) {
                         imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
                     }
                 };
                 new Thread() {
                     @Override
                     public void run() {
                         Drawable drawable = loadImageFromUrl(imageUrl);
                         imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
                         Message message = handler.obtainMessage(0, drawable);
                         handler.sendMessage(message);
                     }
                 }.start();
                 return null;
             }
          
            public static Drawable loadImageFromUrl(String url) {
                URL m;
                InputStream i = null;
                try {
                    m = new URL(url);
                    i = (InputStream) m.getContent();
                } catch (MalformedURLException e1) {
                    e1.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Drawable d = Drawable.createFromStream(i, "src");
                return d;
            }
          
             public interface ImageCallback {
                 public void imageLoaded(Drawable imageDrawable, String imageUrl);
             }

    }
  • 相关阅读:
    Windows向虚拟机Linux传输文件方法
    Postgresql ERROR: permission denied for relation app_info
    Spring Boot: Cannot determine embedded database driver class for database type NONE
    零宽度正预测先行断言是什么呢,看msdn上的官方解释定义
    php 的curl 模拟登陆
    使用ProxychainsMac下安装及配置
    利用onekeyup即可实现验证码的点击刷新功能
    程序中使用gc_enable() 和 gc_disable()开启和关闭
    通过ReflectionMethod,我们可以得到Person类的某个方法的信息
    order by id asc得出的排序是什么原理
  • 原文地址:https://www.cnblogs.com/hgb1116/p/5967955.html
Copyright © 2020-2023  润新知