如何在Android上使用Glide获得原始图像大小?

Swi*_*ift 8 android android-glide

我正在从动态来源加载图像并将其加载到我的应用程序中。但是有时图像太小,在我的应用中看起来很糟糕。我要做的是获取图像大小,如果小于5x5,则完全不显示ImageView。

如何实现呢?

当我使用sizeReadyCallback时,它返回ImageView的大小而不是图像。当我使用请求侦听器时,它返回0,0。

Glide.with(getContext()).load(imageUrl).listener(new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            return false;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            //This returns 0,0
            Log.e("TAG","_width: " + resource.getBounds().width() + " _height:" +resource.getBounds().height());
            return false;
        }
    }).into(ivImage).getSize(new SizeReadyCallback() {
        @Override
        public void onSizeReady(int width, int height) {
            //This returns size of imageview.
            Log.e("TAG","width: " + width + " height: " + height);
        }
    });
Run Code Online (Sandbox Code Playgroud)

小智 6

这个问题很老,但我偶然发现了类似的场景,我需要检查原始图像大小。经过一番挖掘,我在 Github 上找到了这个线程,它有解决方案。

我将复制由pandasys 编写的最新(glide v4)解决方案

这段代码是 Kotlin,但 Java 人应该没有问题。执行加载的代码如下所示:

Glide.with(activity)
 .`as`(Size2::class.java)
 .apply(sizeOptions)
 .load(uri)
 .into(object : SimpleTarget<Size2>() {
   override fun onResourceReady(size: Size2, glideAnimation: Transition<in Size2>) {
     imageToSizeMap.put(image, size)
     holder.albumArtDescription.text = size.toString()
   }

   override fun onLoadFailed(errorDrawable: Drawable?) {
     imageToSizeMap.put(image, Size2(-1, -1))
     holder.albumArtDescription.setText(R.string.Unknown)
   }
 })
Run Code Online (Sandbox Code Playgroud)

可重用的选项是:

private val sizeOptions by lazy {
RequestOptions()
    .skipMemoryCache(true)
    .diskCacheStrategy(DiskCacheStrategy.DATA)}
Run Code Online (Sandbox Code Playgroud)

我的尺码等级大约是:

data class Size2(val width: Int, val height: Int) : Parcelable {
  companion object {
    @JvmField val CREATOR = createParcel { Size2(it) }
  }

  private constructor(parcelIn: Parcel) : this(parcelIn.readInt(), parcelIn.readInt())

  override fun writeToParcel(dest: Parcel, flags: Int) {
    dest.writeInt(width)
    dest.writeInt(height)
  }

  override fun describeContents() = 0

  override fun toString(): String = "$width x $height"

}
Run Code Online (Sandbox Code Playgroud)

这是我的 AppGlideModule 的相关部分

 class BitmapSizeDecoder : ResourceDecoder<File, BitmapFactory.Options> {
  @Throws(IOException::class)
  override fun handles(file: File, options: Options): Boolean {
    return true
  }

  override fun decode(file: File, width: Int, height: Int, options: Options): Resource<BitmapFactory.Options>? {
    val bmOptions: BitmapFactory.Options = BitmapFactory.Options()
    bmOptions.inJustDecodeBounds = true
    BitmapFactory.decodeFile(file.absolutePath, bmOptions)
    return SimpleResource(bmOptions)
  }
}:




override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
            registry.prepend(File::class.java, BitmapFactory.Options::class.java, BitmapSizeDecoder())
            registry.register(BitmapFactory.Options::class.java, Size2::class.java, OptionsSizeResourceTranscoder())


class OptionsSizeResourceTranscoder : ResourceTranscoder<BitmapFactory.Options, Size2> {
  override fun transcode(resource: Resource<BitmapFactory.Options>, options: Options): Resource<Size2> {
    val bmOptions = resource.get()
    val size = Size2(bmOptions.outWidth, bmOptions.outHeight)
    return SimpleResource(size)
  }
}
Run Code Online (Sandbox Code Playgroud)

所以回到最初的问题,onResourceReady回调你可以检查宽度和高度并决定是否显示图像


thu*_*ick 5

更新:

@TWiStErRob在评论中提供了一个更好的解决方案: 更好的解决方案


对于Glide v4:

Glide.with(getContext().getApplicationContext())
     .asBitmap()
     .load(path)
     .into(new SimpleTarget<Bitmap>() {
         @Override
         public void onResourceReady(Bitmap bitmap,
                                     Transition<? super Bitmap> transition) {
             int w = bitmap.getWidth();
             int h = bitmap.getHeight()
             mImageView.setImageBitmap(bitmap);
         }
     });
Run Code Online (Sandbox Code Playgroud)

关键是要先将位图设置为ImageView

  • 我在这里写了一个广泛的解决方案:https://github.com/bumptech/glide/issues/781#issuecomment-160953996也描述了如何有效地实现这一点。我明确提到此解决方案不好,因为它会解码整个图像,而不是前100个字节。 (3认同)
  • 不好,因为您将位图的原始分辨率分配到ImageView中,浪费了内存,并且由于覆盖了Glide的质量分辨率缩小而获得了更丑陋的结果。而且由于您不利用Glide的较小分辨率图像的缓存,因此速度也较慢。 (2认同)

ND1*_*10_ -2

试试这个:我不确定这一点,但你会这样做:

Glide.with(this).load(uri).into(imageView).getSize(new SizeReadyCallback() {
    @Override
    public void onSizeReady(int width, int height) {
        //before you load image LOG height and width that u actually got?
        mEditDeskLayout.setImageSize(width,height);
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 这将返回图像视图的大小。 (3认同)