我有一个NetworkImageView,但我想使用ImageView来缓存图像,而不是一直从网络上获取它。
任何想法如何改变它?
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
//Views
public NetworkImageView imageView;
public TextView textViewName;
public TextView textViewPublisher;
//Initializing Views
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
textViewName.setClickable(true);
textViewName.setOnClickListener(this);
}
答案 0 :(得分:3)
您可以使用fresco lib加载图片。 它有很多功能。 https://github.com/facebook/fresco
答案 1 :(得分:1)
结帐Picasso。易于使用,并提供缓存机制。
答案 2 :(得分:1)
更改布局来自<NetworkImageView
<ImageView
,然后将public NetworkImageView imageView;
的{{1}}和public ImageView imageView;
的{{1}}更改为imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
希望这可以帮助你
您无法直接在ImageView中设置图像。你必须在Bitmap中转换该图像,然后设置
imageView = (ImageView) itemView.findViewById(R.id.imageViewHero);
然后设置为imageview:
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}