【问题标题】:Android - Glide in the recyclerViewAndroid - 在 recyclerView 中滑动
【发布时间】:2018-02-06 21:19:47
【问题描述】:

我是 android 开发的新手,我有一个问题。我尝试在 RecyclerView 中加载一些图像。传入数据的类型为 byte[]。

我的第一个解决方案是将 byte[] 转换为位图,它工作正常,但位图卡在内存中,我得到了 OutOfMemory 异常。

第二种解决方案是使用 Glide。

Glide.with(mContext)                 
                .load(field.getImage())
                .into(holder.mImageView);

但是通过这个解决方案,我在每个项目中都得到了相同的图像。 我认为问题出在 Glide 的缓存中,因为传入的数组不同。我尝试用这段代码解决这个问题:

requestOptions.skipMemoryCache(true);
requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(mContext)
                .applyDefaultRequestOptions(requestOptions)
                .load(field.getImage())
                .into(holder.mImageView);

 }

但这无济于事。这样做的正确方法是什么?

【问题讨论】:

    标签: android caching android-recyclerview android-glide


    【解决方案1】:

    试试这个用法setDefaultRequestOptions(requestOptions)

    Glide.with(context)
         .setDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
         .load(url)
         .into(imageView);
    

    【讨论】:

    • 是的。有用。我使用了 applyDefaultRequestOptions(requestOptions) 而不是 setDefaultRequestOptions(requestOptions)
    • ".setDefaultRequestOptions" 我不能使用它。我正在使用 kolin..这是这里的问题吗?
    • @charithaamarasinghe 使用 .apply(requestOptions)
    【解决方案2】:

    你可以像这样调整图片大小

     Glide.with(PhotoGalleryActivity.this)
                                        .using(new FirebaseImageLoader())
                                        .load(storageReference)
                                        .asBitmap()
                                        .into(new SimpleTarget<Bitmap>(520, 520) {
                                            @Override
                                            public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                                                holder.img.setImageBitmap(resource);
    
                                            }
                                        });
    

    【讨论】:

      【解决方案3】:

      要调整图像大小并添加(占位符和错误图像),请使用以下代码:

          RequestOptions requestOptions = new RequestOptions();
              requestOptions.placeholder(R.drawable.app_icon);
              requestOptions.error(R.drawable.ic_image_error);
              Glide.with(context)
                      .setDefaultRequestOptions(requestOptions)
                      .asBitmap()
                      .load(newsContentModel.getImgNewsUrl())
                      .into(new SimpleTarget<Bitmap>() {
                          @Override
                          public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
      //                        requestLayout()
      //                        Call this when something has changed which has
      //                        invalidated the layout of this view.
                              myViewHolder.imgImage.requestLayout();
                              myViewHolder.imgImage.getLayoutParams().height = newHeight;
                              myViewHolder.imgImage.getLayoutParams().width = newWidth;
      
                              // Set the scale type for ImageView image scaling
                              myViewHolder.imgNewsImage.setScaleType(ImageView.ScaleType.FIT_XY);
                              myViewHolder.imgImage.setImageBitmap(bitmap);
                          }
                      });
      

      【讨论】:

        猜你喜欢
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 2020-10-17
        • 1970-01-01
        • 2020-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多