java 使用滑行加载图像很慢
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39674227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading image with glide is slow
提问by erfan
I have app connect with server and when pics load seems so slow and when scroll that up and down seems glide want read image again! This is my adapter of glide:
我有应用程序与服务器连接,当图片加载看起来很慢并且向上和向下滚动时似乎滑动想要再次读取图像!这是我的滑翔适配器:
DataAdapter:
数据适配器:
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private Context context;
private ArrayList<AndroidVersion> android;
public DataAdapter(Context context,ArrayList<AndroidVersion> android) {
this.context = context;
this.android = android;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.tv_name.setText(android.get(i).getName());
viewHolder.tv_version.setText(android.get(i).getVer());
viewHolder.tv_api_level.setText(android.get(i).getApi());
// load image into imageview using glide
Glide.with(context).load("http://memaraneha.ir/Erfan/images/"+android.get(i).getPic())
.placeholder(R.drawable.truiton)
.error(R.drawable.truiton)
.into(viewHolder.tv_image);
}
@Override
public int getItemCount() {
return android.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_name,tv_version,tv_api_level;
public ImageView tv_image;
public ViewHolder(View view) {
super(view);
tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_version = (TextView)view.findViewById(R.id.tv_version);
tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);
tv_image= (ImageView) view.findViewById(R.id.img);
}
}
}
If any one can help please do that!
如果有人可以提供帮助,请这样做!
采纳答案by Amit Vaghela
Just add this when you get image from URL
, this code reduces the size of that image
只需在从中获取图像时添加此URL
代码,此代码会减小该图像的大小
you can also do this,
你也可以这样做,
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
//save scaled down image to cache dir
newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File imageFile = new File(filePath);
// write the bytes in file
FileOutputStream fo = new FileOutputStream(imageFile);
fo.write(bytes.toByteArray());
check this example
检查这个例子
回答by Prabhat
I used dontTransform()
method and it almost saved me a second. Images are loaded to the list really first.
我使用了dontTransform()
方法,它几乎为我节省了一秒钟。图像首先真正加载到列表中。
回答by Rjz Satvara
use diskCacheStrategy(DiskCacheStrategy.ALL).
使用diskCacheStrategy(DiskCacheStrategy.ALL)。
for more visit this Click Here
更多访问请点击这里