gpt4 book ai didi

android - 在 GridView 中显示位图图像

转载 作者:行者123 更新时间:2023-11-29 21:06:47 25 4
gpt4 key购买 nike

在 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

我的问题是:如何显示带有 GridView 项目占位符的 Activity,以及在检索每个 Bitmap 时,填充了相关的 GridView 项目图像?这是在 MainActivity 中完成的,还是在 GridViewAdapter 中完成的?

提前致谢

最佳答案

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

引用这个: https://github.com/nostra13/Android-Universal-Image-Loader

关于android - 在 GridView 中显示位图图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24299436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com