Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

away to fill width and keep aspect ratio in height #30

Open
muayyad-alsadi opened this issue Nov 17, 2013 · 4 comments
Open

away to fill width and keep aspect ratio in height #30

muayyad-alsadi opened this issue Nov 17, 2013 · 4 comments

Comments

@muayyad-alsadi
Copy link

hi,

this way suggest a simple class that extends from imageview but this does not work here since we don't know width until we fetch/download the image

http://stackoverflow.com/questions/18077325/scale-image-to-fill-imageview-width-and-keep-aspect-ratio

any suggestion ?

@alex-oleshkevich
Copy link

I've solved that via extending SmartImageView with this class:

public class AspectRatioImageView extends SmartImageView {

public AspectRatioImageView(Context context) {
    super(context);
}

public AspectRatioImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (getDrawable() != null) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
        setMeasuredDimension(width, height);
    } else {
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }
}

}

@muayyad-alsadi
Copy link
Author

@alex-oleshkevich does it work even when the size of the image is not know because the image is still to come. and will it get fixed when downloading is finished.

@alex-oleshkevich
Copy link

yes

@daisygabi
Copy link

Hello, trying this class, but the remote images that have different sizes are not scalling(for instance there are portrait images and landscape ones). Is this just for internal drawables or it should work for remote images? Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants