Xamarin.Android
ListViewで、サムネ付きのリストアイテムを表示しています。
サムネと、テキストは、URLから、取得していて、無事に表示は出来ているのですが、
スクロールをすると、重くて、カクカクしながら表示される感じになります。
何か良い方法はありませんでしょうか?
処理の順番としては次の順番になります。
(1)APIで、記事データを取得 : AsyncTaskで処理
↓
(2)List<T>にデータを格納
↓
(3)Adapterにリストデータをセット
↓
(4)AdapterClass内で、
GetView関数が処理開始。
↓
(5)GetViewメソッドで、テキスト、画像をセットする。
まだまだ、初心者で申し訳ありませんが、ご教授をお願い致します。
以下に、Adapterのソースコードを記載させて頂きます。
namespace SampleArticleMemberApp.Droid { public class ArticleListItemAdapter : BaseAdapter<ArticleListItem> { public List<ArticleListItem> items; public Activity context; /** * コンストラクタ */ public ArticleListItemAdapter(Activity context, List<ArticleListItem> items) { Debug.Log(Debug.DEBUG_LEVEL_VERBOSE, "ArticleListItemAdapter", "-----START-----"); this.context = context; this.items = items; Debug.Log(Debug.DEBUG_LEVEL_VERBOSE, "ArticleListItemAdapter", "-----END-----"); } /** * クリック処理 */ public override long GetItemId(int position) { Debug.Log(Debug.DEBUG_LEVEL_VERBOSE, "GetItemId", "-----START-----"); Debug.Log(Debug.DEBUG_LEVEL_VERBOSE, "GetItemId", "-----END-----"); return position; } public override ArticleListItem this[int position] { get { return items[position]; } } public override int Count { get { return items.Count; } } public override View GetView(int position, View convertView, ViewGroup parent) { Debug.Log(Debug.DEBUG_LEVEL_VERBOSE, "GetView", "-----START-----"); var item = items[position]; View view = convertView; if (view == null){ view = context.LayoutInflater.Inflate(Resource.Layout.ArticleListViewItem, null); } // BaseAdapter<T>の対応するプロパティを割り当て view.FindViewById<TextView>(Resource.Id.articleItemDate).Text = item.date; view.FindViewById<TextView>(Resource.Id.articleItemTitle).Text = item.title; Debug.Log(Debug.DEBUG_LEVEL_INFO, "GetView", item.title); Debug.Log(Debug.DEBUG_LEVEL_INFO, "GetView", item.url); Debug.Log(Debug.DEBUG_LEVEL_INFO, "GetView", item.thumbUrl); Debug.Log(Debug.DEBUG_LEVEL_INFO, "GetView", "readable: " +item.readable.ToString()); if(item.thumbUrl != ""){ Debug.Log(Debug.DEBUG_LEVEL_INFO, "GetView", "画像を読み込みます"); view.FindViewById<ImageView>(Resource.Id.articleItemThumb).SetImageResource(Resource.Mipmap.def_thumb); SetThumb(view, position); //var image = GetImageBitmapFromUrl(item.thumbUrl); //view.FindViewById<ImageView>(Resource.Id.articleItemThumb).SetImageBitmap(image); } else{ view.FindViewById<ImageView>(Resource.Id.articleItemThumb).SetImageResource(Resource.Mipmap.def_thumb); } //読み込み不可能ならば、背景色を変える if(!item.readable){ view.SetBackgroundColor(Color.DarkGray); view.FindViewById<TextView>(Resource.Id.articleItemDate).SetTextColor(Color.GhostWhite); view.FindViewById<TextView>(Resource.Id.articleItemDate).Alpha = 0.4f; view.FindViewById<TextView>(Resource.Id.articleItemTitle).SetTextColor(Color.GhostWhite); view.FindViewById<TextView>(Resource.Id.articleItemTitle).Alpha = 0.4f; view.FindViewById<ImageView>(Resource.Id.articleItemThumb).Alpha = 0.4f; } else{ view.SetBackgroundColor(Color.GhostWhite); view.FindViewById<TextView>(Resource.Id.articleItemDate).SetTextColor(Color.Black); view.FindViewById<TextView>(Resource.Id.articleItemDate).Alpha = 1.0f; view.FindViewById<TextView>(Resource.Id.articleItemTitle).SetTextColor(Color.Black); view.FindViewById<TextView>(Resource.Id.articleItemTitle).Alpha = 1.0f; view.FindViewById<ImageView>(Resource.Id.articleItemThumb).Alpha = 1.0f; } Debug.Log(Debug.DEBUG_LEVEL_VERBOSE, "GetView", "-----END-----"); return view; } /** * URLから、Bitmap生成 */ private async Task<Bitmap> GetImageBitmapFromUrl(string url) { Bitmap imageBitmap = null; try{ using (var webClient = new WebClient()) { var imageBytes = webClient.DownloadData(url); if (imageBytes != null && imageBytes.Length > 0) { imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length); } } } catch(Exception e) { Debug.Log(Debug.DEBUG_LEVEL_ERROR, "GetImageBitmapFromUrl", "e: " +e); } return imageBitmap; } private async void SetThumb(View view, int position) { var item = items[position]; if(items[position].bitmap == null) { ImageView imageView = view.FindViewById<ImageView>(Resource.Id.articleItemThumb); Debug.Log(Debug.DEBUG_LEVEL_ERROR, "GetImageBitmapFromUrl", "imgvW: " + imageView.Width); Debug.Log(Debug.DEBUG_LEVEL_ERROR, "GetImageBitmapFromUrl", "imgvH: " + imageView.Height); if(0 < imageView.Width && 0 < imageView.Height){ Bitmap bmp = await GetImageBitmapFromUrl(item.thumbUrl); int bmpH = (int)((float)imageView.Width / (float)bmp.Width *(float)bmp.Height); int scale = Math.Max(imageView.Width, bmpH); bmp = Bitmap.CreateScaledBitmap(bmp, imageView.Width, bmpH, false); items[position].bitmap = await GetImageBitmapFromUrl(item.thumbUrl); view.FindViewById<ImageView>(Resource.Id.articleItemThumb).SetImageBitmap(items[position].bitmap); Debug.Log(Debug.DEBUG_LEVEL_ERROR, "GetImageBitmapFromUrl", "bmpW: " + bmp.Width); Debug.Log(Debug.DEBUG_LEVEL_ERROR, "GetImageBitmapFromUrl", "bmpH: " + bmp.Height); bmp.Recycle(); bmp = null; } } else { view.FindViewById<ImageView>(Resource.Id.articleItemThumb).SetImageBitmap(items[position].bitmap); } } } }
以上です。
よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー