问题描述:

I make a gridview which load images from url. Its working good but it is very slow.I think its make user irritated.

In my code i am using Bitmap in GridView adapter . Here is my code

using System;

using Android.Widget;

using Android.App;

using System.Collections.Generic;

using Android.Views;

using Android.Graphics;

using System.Net;

namespace AMUSEAndroid

{

public class GridViewAdapter : BaseAdapter<ImageItem>

{

Activity context;

List<ImageItem> items;

public GridViewAdapter (Activity context, List<ImageItem> gridViewtems)

{

this.context = context;

this.items = gridViewtems;

}

public override int Count {

get { return items.Count; }

}

public override ImageItem this[int position]

{

get { return items[position]; }

}

public override long GetItemId (int position)

{

return position;

}

public override View GetView (int position, View convertView, ViewGroup parent)

{

View view = convertView;

var item = items[position];

if (view == null)

view = context.LayoutInflater.Inflate(Resource.Layout.player_grid_item, null);

ImageView imgIcon = view.FindViewById<ImageView> (Resource.Id.img_play_bg);//.SetImageBitmap (gridViewtems[position]);

view.FindViewById<TextView> (Resource.Id.music_text).Text = item.Heading;

var imageBitmap = GetImageBitmapFromUrl(item.ImageResourceId);

imgIcon.SetImageBitmap(imageBitmap);

return view;

}

public Bitmap GetImageBitmapFromUrl(string url)

{

Bitmap imageBitmap = null;

using (var webClient = new WebClient())

{

var imageBytes = webClient.DownloadData(url);

if (imageBytes != null && imageBytes.Length > 0)

{

imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);

}

}

return imageBitmap;

}

}

}

Here ImageItem is a Model class.

Is there any other way to load image from url in Gridview. Which load images very quickly and make application faster.

Thanks in advance.

网友答案:

Yup, there is.

You need asynchronous image loading. The current state-of-the-art in this task is implmented by Square's Picasso.

I have never coded in Xamarin, but a quick search shows me there is a module for Xamarin Picasso.

网友答案:

I use this:

private async Task<Bitmap> GetImageBitmapFromUrl(string url)
    {
        Bitmap imageBitmap = null;

        using (var webClient = new WebClient())
        {
            var imageBytes = await webClient.DownloadDataTaskAsync(url);
            if (imageBytes != null && imageBytes.Length > 0)
            {
                imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
            }
        }

        return imageBitmap;
    }

But I cache all images using a dictionary:

private static Dictionary<string, Bitmap> autoCompleteImages = new Dictionary<string, Bitmap>();

And on GetView method I use "holderView" and this piece of code to get cached images from dictionary:

if (autoCompleteImages.ContainsKey(this.autocompleteSearches[position].PicUrl))
            {
                holder.imageView.SetImageBitmap(autoCompleteImages[this.autocompleteSearches[position].PicUrl]);
            }
            else
            {
                this.UpdateImage(holder, position);
            }

Here UpdateImage method:

private async void UpdateImage(ViewHolderAutoComplete holder, int position)
    {
        autoCompleteImages[this.autocompleteSearches[position].PicUrl] = await GetImageBitmapFromUrl(this.autocompleteSearches[position].PicUrl);
        holder.imageView.SetImageBitmap(autoCompleteImages[this.autocompleteSearches[position].PicUrl]);
    }
相关阅读:
Top