Android: How to stretch an image to the screen width while maintaining aspect ratio?
        Posted  
        
            by fredley
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by fredley
        
        
        
        Published on 2010-06-07T16:05:53Z
        Indexed on 
            2010/06/07
            17:52 UTC
        
        
        Read the original article
        Hit count: 1161
        
I want to download an image (of unknown size, but which is always roughly square) and display it so that it fills the screen horizontally, and stretches vertically to maintain the aspect ratio of the image, on any screen size. Here is my (non-working) code. It stretches the image horizontally, but not vertically, so it is squashed...
ImageView mainImageView = new ImageView(context);
    mainImageView.setImageBitmap(mainImage); //downloaded from server
    mainImageView.setScaleType(ScaleType.FIT_XY);
    //mainImageView.setAdjustViewBounds(true); 
    //with this line enabled, just scales image down
    addView(mainImageView,new LinearLayout.LayoutParams( 
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        © Stack Overflow or respective owner