I want to load a gif image from my raw resources( or my device storage), and also scale it to desired width and height with Glide. I successfully in loading gif image but can't scaled it down.
Here is my loading code:
Glide.with(getContext()).load(R.raw.abc).override(
getResources().getDimensionPixelSize(R.dimen.note_icon_width),
getResources().getDimensionPixelSize(R.dimen.note_icon_height)).into(this);
And here is the declaration view in my xml:
<ImageView
android:id="@+id/btn_open_warning"
android:layout_width="48dp"
android:layout_height="match_parent"
android:src="@drawable/ic_whatnew_toolbox"
app:srcSecondState="@drawable/ic_note_menu_disable" />
Glide is an Image Loader Library for Android developed by bumptech and is a library that is recommended by Google. It has been used in many Google open source projects including Google I/O 2014 official application. It provides animated GIF support and handles image loading/caching.
Image Resizing with override(x, y) Glide automatically limits the size of the image it holds in cache and memory to the ImageView dimensions.
Placeholders are Drawables that are shown while a request is in progress.
Many of us use glide for image loading but only some of us know its real power. If we dive into the features of glide, the article will go into TL;DR category.
If you only want to display the first frame of the Gif, you can call asBitmap () to guarantee the display as a regular image, even if the URL is pointing to a Gif. This should give you all the knowledge to display Gifs with Glide. It's easy, try it! Another step up from Gifs are videos.
Glide offers the general scaling options to manipulate the image display. It ships with two standard options centerCrop and fitCenter. CenterCrop () is a cropping technique that scales the image so that it fills the requested bounds of the ImageView and then crops the extra.
Before getting into Glide example, we should know what is glide, Glide is an image processing library developed by muyangmin. Using glide library we can show image, decode images,cache images, animated gifs and many more. This example demonstrate about how to integrate glide in android.
I'm not familiar with Glide
but maybe if you override onResourceReady()
method you can scale it down.
Use a .listener()
as follows
Glide.with(getContext())
.load(R.raw.abc)
.listener(new RequestListener<Uri, GlideDrawable>() {
@Override
public boolean onException(Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) {
//PENG
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
GlideDrawableImageViewTarget glideTarget = (GlideDrawableImageViewTarget) target;
ImageView iv = glideTarget.getView();
int width = iv.getMeasuredWidth();
int targetHeight = width * resource.getIntrinsicHeight() / resource.getIntrinsicWidth();
if(iv.getLayoutParams().height != targetHeight) {
iv.getLayoutParams().height = targetHeight;
iv.requestLayout();
}
return false;
}
})
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With