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

问题描述 投票:5回答:5

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

怎么做到这一点?

当我使用sizeReadyCallback时,它返回ImageView的大小而不是image。当我使用请求监听器时,它返回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);
        }
    });
android android-glide
5个回答
4
投票

更新:

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


对于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);
         }
     });

重点是在设置为ImageView之前获取位图。


2
投票

这个问题很老,但我遇到了类似的情况,我需要检查原始图像大小。经过一番挖掘后,我在Github上找到了this线程,它有解决方案。

我将复制pandasys写的最新(滑翔v4)solution

这段代码是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)
   }
 })

可重复使用的选项是:

private val sizeOptions by lazy {
RequestOptions()
    .skipMemoryCache(true)
    .diskCacheStrategy(DiskCacheStrategy.DATA)}

我的大小等级大约是:

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"

}

这是我的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)
  }
}

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


-1
投票

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

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

-1
投票
Layout code:
    <ImageView
                android:adjustViewBounds="true"
                android:id="@+id/imageView"
                android:scaleType="fitCenter"
                android:src="@android:drawable/ic_menu_camera"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="visible" />


     Glide.with(context).load(imgpth).placeholder(R.drawable.bg_loading)
                                .error(R.drawable.bg_loading).into(imageProdctBanner).getSize(new SizeReadyCallback() {
                            @Override
                            public void onSizeReady(int width, int height) {
                                Log.e("width","wdthheight "+width+"  :  "+height);
                                //before you load image LOG height and width that u actually got?
                               // mEditDeskLayout.setImageSize(width,height);
                            }
                        });
working perfect its getting image original size.
Happy coding...

-2
投票

onResource中尝试这个以获得高度和宽度。

 @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            //This returns 0,0
          int width = glideDrawable.getIntrinsicWidth();
    int height = glideDrawable.getIntrinsicHeight();
            return false;
        }
© www.soinside.com 2019 - 2024. All rights reserved.