简体   繁体   English

使用滑行加载图像很慢

[英]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!如果有人可以提供帮助,请这样做!

I used dontTransform() method and it almost saved me a second.我使用了dontTransform()方法,它几乎为我节省了一秒钟。 Images are loaded to the list really first.图像首先真正加载到列表中。

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检查这个例子

use diskCacheStrategy(DiskCacheStrategy.ALL) .使用diskCacheStrategy(DiskCacheStrategy.ALL)

for more visit this Click Here更多访问请点击这里

You can use below parts for your glide .您可以使用以下部件进行glide This works for me这对我有用

 .thumbnail( 0.5f )
 .diskCacheStrategy( DiskCacheStrategy.ALL )

Like this像这样

Glide.with( context )
            .load( arrayList.get().getImage_url())
            .thumbnail( 0.5f )
            .override( 200, 200 )
            .placeholder(R.drawable.placeholder_image)
            .diskCacheStrategy( DiskCacheStrategy.ALL )
            .into( imageView );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM