使用滑行加载图像很慢

时间:2016-09-24 08:28:19

标签: java android android-studio android-recyclerview android-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);

        }
    }

}

如果有人可以提供帮助,请这样做!

4 个答案:

答案 0 :(得分:2)

只需在从URL获取图片时添加此图片,此代码就会缩小该图片的大小

你也可以这样做,

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

检查this example

答案 1 :(得分:2)

我使用dontTransform()方法,它几乎救了我一秒钟。图像首先被加载到列表中。

答案 2 :(得分:0)

使用 diskCacheStrategy(DiskCacheStrategy.ALL)

更多访问此Click Here

答案 3 :(得分:0)

您可以将以下部分用于您的 glide。这对我有用

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

像这样

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