Android的加载图像到ListView控件的AsyncTask [英] Android loading images to ListView AsyncTask

查看:125
本文介绍了Android的加载图像到ListView控件的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是更多的应用程序逻辑和性能的问题。

This is more a question of application logic and performance.

项目设置:我有一个Android的ListView其膨胀的布局更美观不仅仅是简单的文字赏心悦目。在每一行(7要准确),我有一个通过URL获取图像。

Project Setup: I have an Android ListView which inflates a layout that is more aesthetically pleasing than just simple text. In each row (7 to be exact), I have an Image that is fetched by URL.

问题:这个ListView和它的数据是由作为临时数据模型持有对象的ArrayList设置。当被添加的行中的模型对象然后用于在特定行的UI设置数据。那么问题出现时,当用户向下滚动列表中的一行,曾经上可以观看的,现在是出了用户的看法。当用户滚动备份到该行,获取图像的全过程,然后再次发生。

The problem: This ListView and its data are set by an ArrayList of model objects that serve as temporary data holders. When the rows are added the model objects are then used to set the data in the specific row's UI. The issue then comes along, that when a user scrolls down on the list a row that was once viewable is now out of the user's view. When the user scrolls back up to that row, the whole process of fetching the Image then happens again.

这似乎是它可以通过而不是传递的网址,通过模型,然后提取图像是可以避免的,我应该先获取相关的图片,然后通过位图到模型中,因此,当行可见,再次给用户,它不必重新加载图像

This seems like it can be avoided by instead of passing URLs through the models then fetching the images, I should fetch the images first, then pass the bitmaps to the model, therefore when the row is visible again to the user, it does not have to re-load the image.

有没有编写能够成功加载7张图片,或我必须为每个图像的AsyncTask的对象的AsyncTask的一种方式?

Is there a way to write an AsyncTask that can load 7 images successfully, or do I have to create an AsyncTask object for each image?

每次我走这条路的应用程序崩溃...

Everytime I go this route the application crashes...

推荐答案

您可以使用毕加索库完成这个任务。
访问 http://square.github.io/picasso/

You can use Picasso library for this task. Visit http://square.github.io/picasso/

在你的ListView的适配器,在方法getView,你可以把这个:

In the adapter of your ListView, in the method getView, you can put this:

     Picasso.with(context) 
     .load(url)
     .placeholder(R.drawable.ic_launcher)
     .error(R.drawable.ic_error) 
     .resizeDimen(R.dimen.list_detail_image_size,R.dimen.list_detail_image_size)                              .centerInside()
    .into(imgView);

您不需要一个AsyncTask的,因为这个库已经实现了自己。

You dont need a AsyncTask because this library already implements itself.

这篇关于Android的加载图像到ListView控件的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆