4

我有應用程序連接服務器,當圖片加載看起來非常緩慢,當滾動上下滑動似乎想再次閱讀圖像! 這是我的下滑適配器:加載圖像滑行速度很慢

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

     } 
    } 

} 

如果任何人能幫助請做到這一點!

回答

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

使用diskCacheStrategy(DiskCacheStrategy .ALL)

的詳情,請訪問該Click Here

1

我用dontTransform()方法,它幾乎救了我一秒鐘。圖像首先被加載到列表中。