huy nguyen
huy nguyen

Reputation: 1371

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" />

Upvotes: 6

Views: 1848

Answers (1)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20616

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

Upvotes: 9

Related Questions