我正在从动态来源加载图片并将其加载到我的应用中。但是有时候我的应用程序中的图像太小而且看起来很糟糕。我想要做的是获取图像大小,如果小于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);
}
});
答案 0 :(得分:3)
更新
@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
之前获取位图。
答案 1 :(得分:2)
这个问题很旧,但是我偶然发现了类似的情况,我需要检查原始图像的大小。经过一番挖掘之后,我在Github上找到了this线程,它具有解决方案。
我将复制pandasys编写的最新版本(glide v4)solution。
这段代码是Kotlin,但是Java专家应该不会遇到麻烦。进行加载的代码如下所示:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<form name=f1 method=post>
<div class="parentDiv">
<input type="checkbox" class='chkbox' name=others>Others
<input type=text name=other_text class='txtbox' disabled='disabled'>
</div>
<div class="parentDiv">
<input type="checkbox" class='chkbox' name=others>Others
<input type=text name=other_text class='txtbox' disabled='disabled'>
</div>
<div class="parentDiv">
<input type="checkbox" class='chkbox' name=others>Others
<input type=text name=other_text class='txtbox' disabled='disabled'>
</div>
</form>
可重用的选项是:
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)}
这是我的AppGlideModule的相关部分
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"
}
所以回到最初的问题, 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)
}
}
回调您可以检查宽度和高度并决定是否显示图像
答案 2 :(得分:0)
Glide.with(this)
.asFile()
.load("your path url image")
.into(new SimpleTarget<File>() {
@Override
public void onResourceReady(@NonNull File resource, @Nullable
Transition<? super File> transition) {
fab_raw.setLabelText(""+resource.length());
}
});
答案 3 :(得分:0)
这个hacky(实际上不是)变体按预期工作。由于此序列中未指定 targetView
(.into()
的参数)并且调用了 .preload()
,因此图像不会调整到 ImageView 尺寸或额外的 {{1} }}。您将获得原始尺寸的原始图像,然后将其设置为 scaleType
的来源。
ImageView
答案 4 :(得分:-1)
试试这个:我不确定这个,但你会这样做:
connection string:
答案 5 :(得分:-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...
答案 6 :(得分:-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;
}