Loading and scaling a gif image with Glide

I want to download a gif image from my source resources (or my device), and also scale it to the required width and height using Glide. I have successfully uploaded a gif image but cannot scale it.

Here is my download 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 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" />
+4
source share
1 answer

I am not familiar with Glide, but perhaps if you override the method onResourceReady(), you can reduce it.

Use .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);
+7
source

Source: https://habr.com/ru/post/1695959/


All Articles