簡體   English   中英

使用滑行加載圖像很慢

[英]Loading image with glide is slow

我有應用程序與服務器連接,當圖片加載看起來很慢並且上下滾動時似乎滑動想要再次讀取圖像! 這是我的滑翔適配器:

數據適配器

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

        }
    }

}

如果有人可以提供幫助,請這樣做!

我使用了dontTransform()方法,它幾乎為我節省了一秒鍾。 圖像首先真正加載到列表中。

只需在從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());

檢查這個例子

使用diskCacheStrategy(DiskCacheStrategy.ALL)

更多訪問請點擊這里

您可以使用以下部件進行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 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM