向下滚动列表视图时将图像从服务器编码为android自定义列表视图

问题描述:

以下是关于我的标题的完整说明。例如,我有自定义列表视图包含图像和文字。我应该从服务器获取它的listview数据。如果服务器返回500多组数据,我可以将所有数据呈现到我的列表视图中,但它会花费时间加载,而不是在列表滚动时呈现数据。我为此返回一些代码来加载图像,但我仍然很不满意这个代码。我是通过调用manager.fetchBitMapThread加载从getView()图像方法(data.getImgUrl(),IV)(ImageLoaderThread)。由于运行时会创建太多的线程。可以建议加载数据自定义列表视图的好主意。看看代码是否有问题。向下滚动列表视图时将图像从服务器编码为android自定义列表视图

我见过OnScrollListener但我没有得到这个想法实现自定义列表视图。

manager.fetchBitMapThread(data.getImgUrl(),iv); 




public class Manager { 

ImageView imageView; 
final int stub_id=R.drawable.stub; 
private final Map<String, Bitmap> bitMap; 

public Manager() { 
    bitMap = new HashMap<String, Bitmap>(); 
} 

public void fetchBitMapThread(final String urlString, final ImageView imageView) { 
    this.imageView = imageView; 
    if (bitMap.containsKey(urlString)) { 
     imageView.setImageBitmap(bitMap.get(urlString)); 
    } 
    imageView.setImageResource(stub_id); 
    final Handler handler = new Handler() { 
     @Override 
     public void handleMessage(Message message) { 
      if(!message.equals(null)) 
      imageView.setImageBitmap((Bitmap) message.obj); 
      else 
      imageView.setImageResource(stub_id); 
     } 
    }; 

    Thread thread = new Thread() { 
     @Override 
     public void run() { 
      // set imageView to a "pending" image 
      Bitmap bitM = fetchBitmap(urlString); 
      Message message = handler.obtainMessage(1, bitM); 
      handler.sendMessage(message); 
     } 
    }; 
    thread.start(); 
} 


public Bitmap fetchBitmap(String urlString) { 
    if (bitMap.containsKey(urlString)) { 
     return bitMap.get(urlString); 
    } 

    Log.d(this.getClass().getSimpleName(), "image url:" + urlString); 
    try { 
     InputStream is = fetch(urlString); 


     Bitmap drawable = BitmapFactory.decodeStream(is); 


     if (drawable != null) { 
      bitMap.put(urlString, drawable); 
      //Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", " + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", " + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth()); 
     } else { 
      //wrong.setImageResource(stub_id); 
      Log.w(this.getClass().getSimpleName(), "could not get thumbnail"); 

     } 

     return drawable; 
    } catch (MalformedURLException e) { 
     Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e); 
     //imageView.setImageResource(stub_id); 
     return null; 
    } catch (IOException e) { 

     Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e); 
    // imageView.setImageResource(stub_id); 
     return null; 
    } 
} 
private InputStream fetch(String urlString) throws MalformedURLException, IOException { 
    DefaultHttpClient httpClient = new DefaultHttpClient(); 
    HttpGet request = new HttpGet(urlString); 
    HttpResponse response = httpClient.execute(request); 
    return response.getEntity().getContent(); 
} 

}

OnScrollListener提供四个参数,其是如下所示:

    public void onScroll(AbsListView view, int firstVisibleItem, 
     int visibleItemCount, int totalItemCount) 

顾名思义,第一个是你在屏幕上可见的第一个项目,第二个是你可见的项目总数和最后一个告诉你总数否的itema列表有你的情况是500+。因此请发送请求以下载用户可见的那些itema /行的图像。通过这第一个线程将创建更少的第二个加点是图像将被下载得更快。使用占位符或图像视图的progreess栏。

准备一个映射位图图像,并在关键的URL值。在获取视图方法检查位图为特定的图像视图位置下载或不使用哈希映射,并根据它设置进度栏的可见性。

对于上面的操作,你不能简单地创建一个线程(在匿名内部类中),通过创建一个类来扩展线程并实现一个通知ui线程和后台下载或不下载图像的监听器。这将像创建一个线程异步的管道。

我希望上面的方法可以帮助你在装500+行没有考虑时间做别的所有图像异步下载可能会导致为位图内存不足的例外,它的时间不多,服用过多的。

+0

感谢您的建议。 – Srikanth

使用异步任务按照此链接

http://android-er.blogspot.in/2010/07/load-listview-in-background-asynctask.html

完整示例加载从服务器的图像和在ListView显示它们

请点击此链接加载从服务器图像,自定义列表视图。 http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134

下载这一点,并尝试实施它在你的应用程序。