在GridView中显示位图图像

时间:2014-06-19 05:36:24

标签: android xamarin android-gridview android-adapter android-bitmap

在Xamarin中,我有Activity,其中包含GridView和图片网址列表。检索Bitmap个对象列表,然后从这些网址中GridView显示。

这是我的MainActivity代码:

public class MainActivity : Activity
{
    GridView gridView;
    List<String> bitmapUris;
    List<Bitmap> gridViewItems;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView (Resource.Layout.Main);
        gridView = FindViewById<GridView>(Resource.Id.gridView);

        bitmapUris = new List<string> ();
        bitmapUris.Add ("http://images4.fanpop.com/image/photos/15000000/My-Sample-Pictures-lexi-and-sophie-15061826-1024-768.jpg");
        bitmapUris.Add ("http://www.cameraegg.org/wp-content/uploads/2013/08/AF-S-DX-NIKKOR-18-140mm-f-3.5-5.6G-ED-VR-sample-images-1.jpg");
        bitmapUris.Add ("http://upload.wikimedia.org/wikipedia/en/5/5d/The_Samples-Outpost.jpg");
        bitmapUris.Add ("http://www.1800postcards.com/images/samples/sample_kits2.jpg");

        gridViewItems = GetBitmapImages (bitmapUris);
        gridView.Adapter = new GridViewAdapter (this, gridViewItems);
    }

    List<Bitmap> GetBitmapImages(List<String> bitmapUris)
    {
        List<Bitmap> images = new List<Bitmap> ();
        Bitmap image;
        foreach (var uri in bitmapUris) 
        {
            image = GetImageBitmapFromUri (uri);
            images.Add (image);
        }
        return images;
    }

    private Bitmap GetImageBitmapFromUri(string uri)
    {
        Bitmap imageBitmap = null;

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

        return imageBitmap;
    }
}

这是我的GridViewAdapter代码:

public class GridViewAdapter : BaseAdapter
{
    Activity context;
    List<Bitmap> gridViewtems;

    public GridViewAdapter (Activity context, List<Bitmap> gridViewtems)
    {
        this.context = context;
        this.gridViewtems = gridViewtems;
    }

    public override int Count {
        get { return gridViewtems.Count; }
    }

    public override Java.Lang.Object GetItem (int position)
    {
        return null;
    }

    public override long GetItemId (int position)
    {
        return 0;
    }

    public override View GetView (int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if (view == null)
            view = context.LayoutInflater.Inflate(Resource.Layout.GridViewItemWithImageAndText, null);
        view.FindViewById<ImageView> (Resource.Id.image).SetImageBitmap (gridViewtems[position]);
        view.FindViewById<TextView> (Resource.Id.text).Text = position.ToString();
        return view;

    }
}

目前,当应用程序加载时,屏幕将变为空白,直到从网址中检索到Bitmaps为止。

我的问题是:如何在Activity项目中显示GridView占位符,并在检索到每个Bitmap时显示相关的GridView项目图片人口稠密? 这是在MainActivityGridViewAdapter

中完成的

提前致谢

1 个答案:

答案 0 :(得分:2)

尝试使用Universal Image Loader将图像从url显示到GridView。

参考: https://github.com/nostra13/Android-Universal-Image-Loader