Glide: fit image based on height indluding aspect ratio

Created on 21 Sep 2015  路  5Comments  路  Source: bumptech/glide

Hello.
Is it possible to resize image based only on height including aspect ratio ? In picasso i was using .resize(0,height)
but here a can't pass 0 to .override method

question

Most helpful comment

I would like to say it's as simple as:

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        />
</HorizontalScrollView>

but it's not yet implemented: #135.

Try something like the above layout AND .fitCenter().override(Target.SIZE_ORIGINAL, imageView.getHeight()). But be careful because getHeight() is not available in onCreate/onStart/onResume, so you'll need to listen for layout. Notice I didn't say getWindowManager().getDefaultDisplay().getHeight(), because of the action bar or activity margins; it's kind of pointless to load a roughly fitting image with Glide and then let ImageView resample the image again, because it's a few pixels too tall. I would try with something like this to determine the precise height:

void load(final ImageView imageView) {
    if (imageView.getHeight() == 0) {
        // wait for layout, same as Glide's SizeDeterminer does
        imageView.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
            @Override public boolean onPreDraw() {
                // re-query viewTreeObserver, because the one used to add the listener may be dead: http://stackoverflow.com/a/29172475/253468
                imageView.getViewTreeObserver().removeOnPreDrawListener(this);
                // call the same method, but now we can be sure getHeight() has a value
                load(imageView);
                return true; // == allow drawing
            }
        });
    } else {
        Glide
                .with(imageView.getContext())
                .load("whatever")
                .fitCenter()
                .override(Target.SIZE_ORIGINAL, imageView.getHeight())
                .into(imageView);
    }
}

All 5 comments

You can try Target.SIZE_ORIGINAL instead of 0, but I think a better approach is to use the layout system to figure out the sizing: height: 100dp, width: wrap_content/match_parent, scaleType: fitCenter(default) in xml.

Yes, but my imageView is in horizontalScrollView, and I want the image resize to screen height and width calculated with ratio

I would like to say it's as simple as:

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        />
</HorizontalScrollView>

but it's not yet implemented: #135.

Try something like the above layout AND .fitCenter().override(Target.SIZE_ORIGINAL, imageView.getHeight()). But be careful because getHeight() is not available in onCreate/onStart/onResume, so you'll need to listen for layout. Notice I didn't say getWindowManager().getDefaultDisplay().getHeight(), because of the action bar or activity margins; it's kind of pointless to load a roughly fitting image with Glide and then let ImageView resample the image again, because it's a few pixels too tall. I would try with something like this to determine the precise height:

void load(final ImageView imageView) {
    if (imageView.getHeight() == 0) {
        // wait for layout, same as Glide's SizeDeterminer does
        imageView.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
            @Override public boolean onPreDraw() {
                // re-query viewTreeObserver, because the one used to add the listener may be dead: http://stackoverflow.com/a/29172475/253468
                imageView.getViewTreeObserver().removeOnPreDrawListener(this);
                // call the same method, but now we can be sure getHeight() has a value
                load(imageView);
                return true; // == allow drawing
            }
        });
    } else {
        Glide
                .with(imageView.getContext())
                .load("whatever")
                .fitCenter()
                .override(Target.SIZE_ORIGINAL, imageView.getHeight())
                .into(imageView);
    }
}

I have solved the problem by creating custom BitmapTransformation.
I have passed measuredHeigth of screen and calculated ratio =measuredHeigth/toTransform.getHeigth()
And then return Bitmap.createScaledBitmap(toTransform, toTransform.getHeigth()*ratio, toTransform.getWidth()*ratio,true);
I Thing this class should be included to Glide lib;)

Feel free to send us a pull request. Thanks for following up!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kenneth2008 picture kenneth2008  路  3Comments

kooeasy picture kooeasy  路  3Comments

MrFuFuFu picture MrFuFuFu  路  3Comments

Morteza-Rastgoo picture Morteza-Rastgoo  路  3Comments

technoir42 picture technoir42  路  3Comments