45

I have a lot of doubts about the treatment of the images in android, and I was hoping to see if you could solve them.

At this point I have an image that occupies 320 dp high, and match_parent width, which is around 60% of the screen. This image of the load with Glide, of some images in 1080 that I have personally.

I tried to make centroCrop and fitXY, but always deform the images. The first type I know cuts the image, but the second one fits a size, but it does deform the image high or wide.

Is there any way, to insert it with Glide and see it as it is? What properties have I to touch on ImageView and which of Glide?

<ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="false"
        app:layout_collapseMode="parallax"
        android:scaleType="fitXY"
        app:layout_scrollFlags="scroll|enterAlways" />
Zoe
  • 27,060
  • 21
  • 118
  • 148
Traif
  • 671
  • 2
  • 6
  • 12

6 Answers6

112

Override must be accessed via RequestOptions in the most recent version of Glide 4.x. You can add RequestOptions through apply()

Glide.with(context)
    .load(path)
    .apply(new RequestOptions().override(600, 200))
    .into(imageViewResizeCenterCrop);
Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66
  • 1
    You're correct. I think developers should read and not only copy-paste, specially what comes in newer versions. – blueware Apr 19 '20 at 09:20
  • 2
    Is it possible to resize only if the original image bigger than size you want? I need to set max side for my image 1000px but if I use override(1000) Glide gives me 1000x1000 image even if it was 10x10. – Den Jul 20 '20 at 08:28
  • @Den. This is the best approach for u. https://stackoverflow.com/a/8471294/4140069 – amlwin Jul 21 '20 at 07:41
  • @AungMyoLwin no, not the best. You need the original bitmap to create scaled bitmap - it takes a lot of RAM. I'd like to get pre-scaled bitmap. – Den Jul 21 '20 at 08:07
  • also you can directly use `.override(w, h)` or `.override(size = )` ( without `.apply(new RequestOptions()` ). Moreover, if you set size at 0, the Glide will load full-sized image – Skarlet Jun 04 '21 at 13:11
15

in glide 4.x 'override' is in 'apply' :

Glide.with(getBaseContext())
 .load(path)
 .apply(RequestOptions.placeholderOf(R.mipmap.no_wifi)
 .error(R.mipmap.no_wifi)
 .override(500,500))
 .into(mImageView);
abbasalim
  • 3,118
  • 2
  • 23
  • 45
10
Glide
    .with(context)
    .load(path)     
    .apply(new RequestOptions().override(600, 200))
    .centerCrop() 
    .into(imageViewResizeCenterCrop);

@Raghunandan is right.you should try transformation your own way.

Radhey
  • 2,139
  • 2
  • 24
  • 42
  • 5
    Note: Override must be accessed via RequestOptions in the most recent version of glide: "apply(new RequestOptions().override(600, 200))" – Elletlar Apr 24 '18 at 16:19
5

If you want to load image without using glide then you can use scaleType "fitCenter" or "centerInside" as follows -

<ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="false"
        app:layout_collapseMode="parallax"
        android:scaleType="centerInside"
        app:layout_scrollFlags="scroll|enterAlways" />

With Glide you can use override(w,h). When you use override(w,h), glide generates a new bitmap with width and height mentioned in override(w,h) and then load the image into ImageView. You can use fitCenter() to align the image. You can also use diskCacheStrategy(). If you don't use it, Glide will catch only newly generated bitmap. If you want to catch original image also then use diskCacheStrategy(DiskCacheStrategy.ALL).

Glide.with(context)
            .load(image_path)
            .override(800, 400)
            .fitCenter()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(imageView)

I hope this helps.

Anand Soni
  • 186
  • 1
  • 5
  • 6
    Note: Override must be accessed via RequestOptions in the most recent version of Glide: "apply(new RequestOptions().override(800, 400))" – Elletlar Apr 24 '18 at 16:18
4

you have to do the following with your imageview :

        <ImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="fitCenter" />

and this with your glide :

    Glide.with(getBaseContext())
            .load(path)
            //.placeholder(R.drawable.drawable)
            .error(R.drawable.noimg)
            .animate(R.anim.animation_fade_in)
            .into(mImageView);

This works perfectly with me .

4

Glide 4.x - If you want get bitmap, try:

Glide.with(mContext).asBitmap().
                        load(pictureUri)
                        .apply(new RequestOptions().override(50, 50))
                        .listener(new RequestListener<Bitmap>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                                return false;
                            }
                        @Override
                        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                            // resource is your loaded Bitmap
                            imgView.setImageBitmap(resource);
                            return true;
                        }
                    }).submit();
Lam Nguyen
  • 565
  • 1
  • 5
  • 7