Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading image with glide is slow

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!

like image 364
Erfan Avatar asked Sep 24 '16 08:09

Erfan


People also ask

How do you optimize memory consumption when using Glide?

Loading images with the unknown size If a loaded image happens to be smaller than the ImageView, Glide will unnecessarily expand original bitmap to match the size of the target view. One way to prevent it is to set scaleType=”centerInside” in the ImageView. That way loaded image won't expand beyond the original size.

How do I turn off glide cache?

To skip the disk cache only, use DiskCacheStrategy. NONE : Glide. with(fragment) .

What is Glide cache?

What is Glide? Glide is a fast and efficient open-source media management and image loading library for Android applications. It offers an extensible resource decoding pipeline, memory and disk caching, automatic resource pooling, and an easy-to-use API to improve performance.


2 Answers

I used dontTransform() method and it almost saved me a second. Images are loaded to the list really first.

like image 103
Prabhat Avatar answered Oct 17 '22 16:10

Prabhat


Just add this when you get image from URL, this code reduces the size of that image

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

like image 44
Amit Vaghela Avatar answered Oct 17 '22 14:10

Amit Vaghela