使用滑行加载图像很慢 [英] Loading image with glide is slow

查看:23
本文介绍了使用滑行加载图像很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应用程序与服务器连接,当图片加载看起来很慢并且向上和向下滚动时似乎滑动时想要再次读取图像!这是我的滑翔适配器:

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:

数据适配器:

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!

推荐答案

当你从 URL 获取图像时添加此代码,此代码减小该图像的大小

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

你也可以这样做,

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());

检查这个例子

这篇关于使用滑行加载图像很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆