Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load and scale gif image with Glide

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" />
like image 534
huy nguyen Avatar asked Apr 09 '18 07:04

huy nguyen


People also ask

Does glide support gifs?

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.

What is Glide override?

Image Resizing with override(x, y) Glide automatically limits the size of the image it holds in cache and memory to the ImageView dimensions.

What is placeholder Glide?

Placeholders are Drawables that are shown while a request is in progress.

Do you use glide for image loading?

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.

How to display GIFs with glide?

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.

How do I scale the image display in OpenGlide?

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.

What is Glide in Android?

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.


1 Answers

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);
like image 118
Skizo-ozᴉʞS Avatar answered Sep 22 '22 10:09

Skizo-ozᴉʞS