に画像をダウンロードして使用する現在、私は私の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
で画像をダウンロードして表示する最適な方法は何ですか?
グライドをお試しください。パフォーマンスが向上しました。 – elmontoya7