2016-10-16 3 views
1

に画像をダウンロードして使用する現在、私は私のrecyclerviewで画像をダウンロードして表示するために、これを使用しています:効率的recyclerview

private class RetrieveImageTask extends AsyncTask<URL,Void,Drawable>{ 
    URL url; 
    InputStream inputStream; 
    protected Drawable doInBackground(URL... urls){ 
     url = urls[0]; 
     try { 
      inputStream = (InputStream)url.getContent(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return Drawable.createFromStream(inputStream, null); 
    } 
    protected void onPostExecute(Drawable drawable) { 

    } 
} 

私はその後、私のRecyclerViewAdapterにonBindViewHolderでこのクラスを使用します。

@Override 
public void onBindViewHolder(ItemViewHolder item, int i) { 
    try { 
     item.title.setText(aushangdata.getJSONObject(i).getString("title")); 
     item.desc.setText(aushangdata.getJSONObject(i).getString("text")); 
     item.tag.setText(aushangdata.getJSONObject(i).getString("category")); 
     item.link.setText(aushangdata.getJSONObject(i).getString("link")); 
     URL myUrl = new URL(aushangdata.getJSONObject(i).getString("image")); 
     RetrieveImageTask task = new RetrieveImageTask(); 
     Drawable drawable = task.execute(myUrl).get(); 
     item.thumb.setImageDrawable(drawable); 
    } catch (JSONException | IOException | InterruptedException | ExecutionException e) { 
     e.printStackTrace(); 
    } 
} 

これは明らかに非常におしゃれな解決策であり、非常に目立っています。すなわち、recyclerviewを含むフラグメントを開くと、最大0.5秒の遅延があります。また、スクロールしていくと、画像が大きくカードのスペースの

私の疑問は:RecyclerViewで画像をダウンロードして表示する最適な方法は何ですか?

答えて

2

あなたはグライドを使用することができます。https://github.com/bumptech/glide

はまた、使用するのは簡単です:

Glide.with(CONTEXT).load(URL or DRAWABLE).asBitmap().into(IMAGEVIEW); 

より多くのオプションのためのwikiをチェックしてください:あなたのコードでhttps://github.com/bumptech/glide/wiki

を:

Glide.with(context).load(myUrl).asBitmap().into(item.thumb); 

希望します。

1

私はピカソと同様の問題がありました。私の上の答えは正しい。私がロード時間を改善するために行ったことは、方法resizecenterCropを使用していました。

resize(xxx,xxx). 

centerCrop(). 
into(IMAGEVIEW); 

おそらく同様の方法があります。

+0

グライドをお試しください。パフォーマンスが向上しました。 – elmontoya7