loopj / android-smart-image-view

Android ImageView replacement which allows image loading from URLs or contact address book, with caching
http://loopj.com/android-smart-image-view/
1.29k stars 502 forks source link

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

Open muayyad-alsadi opened 11 years ago

muayyad-alsadi commented 11 years ago

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 commented 10 years ago

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 commented 10 years ago

@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 commented 10 years ago

yes

daisygabi commented 10 years ago

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