我的项目遇到了一些麻烦。我的工作一切正常但是当我在我的活动或片段中加载单个gridview中的图像或多个图像时,在我的活动加载之前需要3-5分钟。我的意思是Android在从活动中的gridview加载图像之前冻结超过3分钟。
我研究了这个问题并尝试了我搜索过的代码。尝试异步任务。但我得到错误。我不熟悉异步或异步任务。我仍然是Xamarin和C#语言的新手。
这是我的代码:
CustomGridViewAdapter.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Lang;
namespace testing_code
{
public class CustomGridViewAdapter : BaseAdapter
{
private Context context;
private string[] gridViewString;
//private int[] gridViewImage;
private string[] gridViewImage;
public CustomGridViewAdapter(Context context, string[] gridViewstr, string[] gridViewImage /*int[] gridViewImage*/)
{
this.context = context;
gridViewString = gridViewstr;
this.gridViewImage = gridViewImage;
}
public override int Count
{
get
{
return gridViewString.Length;
}
}
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;
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
if(convertView == null)
{
view = new View(context);
view = inflater.Inflate(Resource.Layout.gridview_layout, null);
TextView txtview = view.FindViewById<TextView>(Resource.Id.textViewGrid);
ImageView imgview = view.FindViewById<ImageView>(Resource.Id.imageViewGrid);
txtview.Text = gridViewString[position];
//imgview.SetImageResource(gridViewImage[position]);
imgview.SetImageBitmap(GetImageBitmapFromUrl(gridViewImage[position]));
}
else
{
view = (View)convertView;
}
return view;
}
private Android.Graphics.Bitmap GetImageBitmapFromUrl(string url)
{
Android.Graphics.Bitmap imageBitmap = null;
using (var webClient = new System.Net.WebClient())
{
var imageBytes = webClient.DownloadData(url);
if (imageBytes != null && imageBytes.Length > 0)
{
imageBitmap = Android.Graphics.BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}
}
return imageBitmap;
}
}
}
Fragment1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using SupportFragment = Android.Support.V4.App.Fragment;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Support.V4.Widget;
using Android.Support.V4.App;
using MySql.Data.MySqlClient;
using System.Data;
namespace EFCAndroid.Fragment_Category
{
public class Fragment_chicken : SupportFragment
{
GridView gridview;
List<string> gridviewstring = new List<string>();
List<string> imgview = new List<string>();
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
View view = inflater.Inflate(Resource.Layout.fragment_layout, container, false);
string[] get_id_name = new string[] { "CHICKEN GRATIN WITH MIX VEGETABLES", "CHICKEN TIKKA WITH PAN GRILL BANANA WITH MIX GREENS", "CHICKEN WITH CARAMELIZED APPLE WITH POTATO WEDGES",
"ROSEMARY CHICKEN BREAST WITH SIMPLE PASTA OR MIX", "ROASTED CHICKEN WITH FUNKY SALAD" };
string[] get_img = new string[] { "http://soleimer.com/EFC_IMAGES_MOBILE/CATEGORY/chicken/CHICKEN_GRATIN_WITH_MIX_VEGETABLES.png", "http://soleimer.com/EFC_IMAGES_MOBILE/CATEGORY/chicken/CHICKEN_TIKKA_WITH_PAN_GRILL_BANANA_WITH_MIX_GREENS.png",
"http://soleimer.com/EFC_IMAGES_MOBILE/CATEGORY/chicken/CHICKEN_WITH_CARAMELIZED_APPLE_WITH_POTATO_WEDGES.png", "http://soleimer.com/EFC_IMAGES_MOBILE/CATEGORY/chicken/ROASTED_CHICKEN_WITH_FUNKY_SALAD.png",
"http://soleimer.com/EFC_IMAGES_MOBILE/CATEGORY/chicken/ROSEMARY_CHICKEN_BREAST_WITH_SIMPLE_PASTA_OR_MIX.png" };
// ASC
Array.Sort(get_id_name, get_img, Comparer<string>.Create((x, y) => x.CompareTo(y)));
// DESC --> change x.compareTo(y) into y.compareTo(x)
//Array.Sort(get_id_name, get_img, Comparer<string>.Create((x, y) => y.CompareTo(x)));
gridviewstring.AddRange(get_id_name);
imgview.AddRange(get_img);
string[] GridViewStringArray = gridviewstring.ToArray();
string[] GridImgViewArray = imgview.ToArray();
CustomGridViewAdapter adapter = new CustomGridViewAdapter(Activity, GridViewStringArray, GridImgViewArray);
gridview = view.FindViewById<GridView>(Resource.Id.grid_view_image_text);
gridview.Adapter = adapter;
return view;
}
}
}
答案 0 :(得分:0)
为了高效的图像加载,我建议。 可以从nuget添加的FFimageloading或square.picasso。
Picasso可以在您的适配器中使用:
Picasso.With(context)
.Load((gridViewImage[position]))
.Into(imgview);
而不是:
imgview.SetImageBitmap(GetImageBitmapFromUrl(gridViewImage[position]))
如果您正在使用FFimageloading,请执行以下操作:
ImageService.Instance.LoadUrl(gridViewImage[position]).Into(imgview);