I'm using Glide in my Android project, and I have a RecyclerView with a list of CardViews in it. What I wanna do is to load an image for each CardView while showing a GIF loading image until the real image gets loaded. Now if it try to load the GIF as the main image, no problem:
Glide.with(context)
.load(R.raw.gif_loading_bar)
.into(imageView);
The code above works fine and the GIF gets loaded. But when I try to load another image and make the GIF a placeholder Android Studio shows an error "Expected resource of type drawable":
Glide.with(mContext)
.setDefaultRequestOptions(new RequestOptions()
.placeholder(R.raw.gif_loading_bar))
.load(imageUrl)
.into(imageView);
What should I do to fix this?
Glide does support GIF as placeholder. I recently faced this situation and solved it like below. Hope it helps someone like me who is facing this situtaion.
var builder = Glide.with(mContext).load(path).apply(options).thumbnail(Glide.with(mContext).load(R.raw.loader2))
if (listener == null) {
builder = builder.addListener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
//TODO; your code here
return false
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
//TODO; your code here
return false
}
})
} else {
builder = builder.addListener(listener)
}
builder.into(myImageView)
What it does
It load your gif as thumbnail until the image is downloaded from the source.
Inside the onResourceReady method it will give you the callback when the resource is ready. Make sure to put gif file under R.raw package and not in drawable package. Let me know if you face any issue.
Unfortunately, Glide does not support GIF placeholders, as mentioned in this Github issue, and this one.
This should do the job:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
GlideDrawableImageViewTarget imageViewTarget = new
GlideDrawableImageViewTarget(imageView);
Glide.with(context)
.asGif()
.setDefaultRequestOptions(new RequestOptions()
.placeholder(R.raw.gif_loading_bar))
.load(imageUrl)
.into(imageView);
More info here and here
Just make sure to change the getContext() as per your codes. For example in my case I am passing it as holder.img.getContext()
Glide.with(getContext()).load(item[position])
.thumbnail(Glide.with(getContext()).load(R.drawable.gif_image))
.fitCenter()
.crossFade()
.into(imageView);
Related
I'm trying to display a low-res image as a full tv background while loading the high quality image. With this solution I could avoid the Black-To-image blink effect.
My low-res image is loaded in a previous fragment and is stored in the cache.
I try to achieve this with Coil. I've seen there is a memoryCachePlaceholder, but I couldn't make it work with my solution. Any hint ?
requireContext().imageLoader.enqueue(ImageRequest.Builder(requireContext())
.data(myLowResUrl)
.crossfade(0)
.target(object : TransitionTarget {
override val drawable get() = showImageView!!.drawable
override val view get() = showImageView!!
override fun onSuccess(result: Drawable) {
showImageView?.setImageDrawable(result)
showImageView?.load(myHighResUrl) {
placeholderMemoryCacheKey(showImageView?.result?.request?.memoryCacheKey)
}
}
})
.build())
I want to use palette to get a color and fill it before glide successfully loads the picture.
Just like bing.com or twitter.com, a color will cover the image before it is successfully loaded.
I use the following methods to achieve it:
Glide
.with(imageView.context)
.asBitmap()
.load(imageUrl)
.listener(object : RequestListener<Bitmap> {
override fun onLoadFailed(e: GlideException?, model: Any, target: Target<Bitmap>, isFirstResource: Boolean) = false
override fun onResourceReady(resource: Bitmap, model: Any, target: Target<Bitmap>, dataSource: DataSource, isFirstResource: Boolean): Boolean {
Palette
.from(resource)
.generate(PaletteAsyncListener { palette ->
val darkVibrantSwatch = palette!!.darkVibrantSwatch
val dominantSwatch = palette.dominantSwatch
val lightVibrantSwatch = palette.lightVibrantSwatch
if (darkVibrantSwatch != null) {
imageView.setBackgroundColor(darkVibrantSwatch.rgb)
} else if (dominantSwatch != null) {
imageView.setBackgroundColor(dominantSwatch.rgb)
} else {
imageView.setBackgroundColor(lightVibrantSwatch!!.rgb)
}
})
return false
}
})
.into(imageView)
However, through the above code, I will load the image directly (imagview will display white before the image is loaded successfully), without filling it with solid color first.
I think maybe glide is loading too fast?
Is there any way to ensure that my picture is filled with solid color for a period of time, such as 500ms?
use Palette.from(resource!!.toBitmap) this will force and rap the resources to bitmap. but firstly you will need to use let on the resources variable so that resources doe's not give you issues
I have to successfully load an image to this imageview and I am using glide. My biggest challenged however is I want this rectangle drawable to be in the image and the image to fit in the white rectangle drawable.
Here is my glide function
private fun loadUrl(url: String) {
Glide.with(this)
.asBitmap()
.load(url)
.fitCenter()
.centerInside()
.into(object : CustomTarget<Bitmap?>() {
override fun onResourceReady(
resource: Bitmap,
transition: Transition<in Bitmap?>?
) {
binding.imageview.setImageBitmap(resource)
binding.imageview.setBackgroundResource(R.drawable.white_rectangle)
}
override fun onLoadCleared(placeholder: Drawable?) {
}
})
}
My image view
<ImageView
android:id="#+id/imageview"
android:layout_width="48dp"
android:layout_height="48dp"
I want to achieve something like this.
I saw this one how can I advance this?
How to make border for circle cropped image in glide
I am loading image stored in firebase firestore using Glide. Initially I was loading the image in the same activity where the url link is being fetched from Firebase. Things were running smooth till this point. Now I am trying to load this image in a popUp Activity (basically a different activity). The url is being fetched in the original activity and then being passed on to the popUp Activity.
I have checked that the URL passed to the new activity is correct, however, the same is not loading. Can some one help. The code in the new activity is as follows:
val catch = intent.getStringExtra("imgSrc")
popUpText.visibility = View.GONE
if(catch!=null){
val presImage = PhotoView(this#PopUp)
val param2: LinearLayout.LayoutParams = LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT, 1.0F)
param2.setMargins(0,64,0,0)
presImage.layoutParams = param2
presImage.scaleType = ImageView.ScaleType.FIT_CENTER
val popProgress = ProgressBar(this#PopUp)
popProgress.layoutParams = param2
popProgress.setBackgroundColor(Color.parseColor("#beb2bb"))
popLayout.addView(popProgress)
Glide.with(this#PopUp)
.load(Uri.parse(catch))
.listener(object: RequestListener<Drawable>{
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: com.bumptech.glide.request.target.Target<Drawable>?,
isFirstResource: Boolean
): Boolean {
return false
}
override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: com.bumptech.glide.request.target.Target<Drawable>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
popProgress.visibility=View.GONE
return false
}
})
.into(presImage)
}
The value of catch is correct. I have checked that by logging the same.
I understand that the error I may be making is likely very trivial, but have spent a lot of time unable to decipher.
Pls add your child view into parent view to show up on screen.
in that part of code above. you just loaded image into imageView but forgot add imageView into parent View.
Hope this help.
I am using glide 4.6.1 to load a Resource (a vector drawable). It's always the same resource but sometimes does not load properly. Looks Emily Blunt image.
The code:
fun AppCompatImageView.loadCircleImage(resId: Int, width: Int, height: Int) {
val options = RequestOptions()
.fitCenter()
.override(width, height)
.circleCrop()
Glide.with(context)
.load(AppCompatResources.getDrawable(context, resId))
.apply(options)
.into(this)
}