用图像永久填充 ListView?

Permanently Populate ListView With images?

我目前正在开发一个有列表视图的应用程序。此列表视图填充了从 XML 在线文件中获取的文本和图像。但是,它正在正确填充,无论何时滚动,当前不在屏幕上的任何内容都会重新加载并重新计算位图的大小以进入列表视图。这导致滚动不那么流畅和烦人。我怎样才能让我的 activity 用图像填充列表视图并将它们加载一次以保留在那里?

ImageLoader:

public class ImageLoader {

MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;

int size;
int placeholderpic;

public ImageLoader(Context context, int size, int placeholderpic) {
    fileCache = new FileCache(context);
    executorService = Executors.newFixedThreadPool(5);
    this.size = size;
    this.placeholderpic = placeholderpic;
}

public void displayImage(String url, ImageView imageView) {
    imageViews.put(imageView, url);
    Bitmap bitmap = memoryCache.get(url);
    if(bitmap != null)
        imageView.setImageBitmap(bitmap);
    else {
        queuePhoto(url, imageView);
        imageView.setImageResource(placeholderpic);
    }
}

private void queuePhoto(String url, ImageView imageView) {
    PhotoToLoad p = new PhotoToLoad(url, imageView);
    executorService.submit(new PhotosLoader(p));
}

private Bitmap getBitmap(String url) {
    File f = fileCache.getFile(url);

    //from SD cache
    Bitmap b = decodeFile(f);
    if(b != null)
        return b;

    //from web
    try {
        Bitmap bitmap = null;
        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is=conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Throwable ex) {
        ex.printStackTrace();
        if(ex instanceof OutOfMemoryError)
            memoryCache.clear();
        return null;
    }
}

//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
    try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = size;
        int width_tmp = o.outWidth, height_tmp=o.outHeight;
        int scale = 1;
        while(true){
            if(width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}

//Task for the queue
private class PhotoToLoad {
    public String url;
    public ImageView imageView;
    public PhotoToLoad(String u, ImageView i) {
        url = u;
        imageView = i;
    }
}

class PhotosLoader implements Runnable {
    PhotoToLoad photoToLoad;
    PhotosLoader(PhotoToLoad photoToLoad){
        this.photoToLoad = photoToLoad;
    }

    @Override
    public void run() {
        if(imageViewReused(photoToLoad))
            return;
        Bitmap bmp = getBitmap(photoToLoad.url);
        memoryCache.put(photoToLoad.url, bmp);
        if(imageViewReused(photoToLoad))
            return;
        BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
        Activity a = (Activity)photoToLoad.imageView.getContext();
        a.runOnUiThread(bd);
    }
}

boolean imageViewReused(PhotoToLoad photoToLoad) {
    String tag=imageViews.get(photoToLoad.imageView);
    if(tag == null || !tag.equals(photoToLoad.url))
        return true;
    return false;
}

//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
    Bitmap bitmap;
    PhotoToLoad photoToLoad;

    public BitmapDisplayer(Bitmap b, PhotoToLoad p){
        bitmap = b; photoToLoad = p;
    }

    public void run() {
        if(imageViewReused(photoToLoad))
            return;
        if(bitmap != null)
            photoToLoad.imageView.setImageBitmap(bitmap);
        else
            photoToLoad.imageView.setImageResource(placeholderpic);
    }
}

public void clearCache() {
    memoryCache.clear();
    fileCache.clear();
}

列表视图适配器:

public class LazyNewsAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<News> listData;
private LayoutInflater inflater = null;

public LazyNewsAdapter(Activity activity, ArrayList<News> listData) {
    this.activity = activity;
    this.listData = listData;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return listData.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    News newsItem = listData.get(position);

    View view = convertView;
    if(convertView == null)
        view = inflater.inflate(R.layout.news_cell, null);

    TextView newsTitle = (TextView) view.findViewById(R.id.newsTitle);
    TextView newsDate = (TextView) view.findViewById(R.id.newsDate);
    ImageView image = (ImageView) view.findViewById(R.id.newsImage);

    newsTitle.setText(newsItem.getNewsTitle());
    newsDate.setText(newsItem.getNewsDate());
    String url = newsItem.getNewsImageUrl();

    ImageLoader imageLoader = new ImageLoader(activity, 600, R.drawable.placeholder);
    imageLoader.displayImage(url, image);

    return view;
}

尝试将在线数据保存到Array List 变量中。然后使用 notifydatasetChange

将其加载到列表视图

你必须为那种事情使用缓存。

请参阅 Universal Image loader 以高效方式加载图像。

您必须在 activity 中只有一个 imageLoader 实例,不要在每个 getView 中实例化它:

在 onCreate 而不是 getView 中执行该行:

    ImageLoader imageLoader = new ImageLoader(activity, 600, R.drawable.placeholder);

在 ListAdapter 中使用 listview 优化设置图像这里的视频教程可能会对您有所帮助,还请务必使用 Glide 设置图像,因为它们在回收您的位图方面向前迈进了一步

https://www.youtube.com/watch?v=mKGoKnhN_Ys&index=91&list=PL1q3ROAofjeOUwh7lPBnGbg__DUodwLN7