private ImageScale scaleImage ( int imageX , int imageY , int containerX , int containerY ) { ImageScale ret = new ImageScale ( ) ; // Compare aspects faster by multiplying out the divisors. if ( imageX * containerY > imageY * containerX ) { // Image aspect wider than container ret . scale = ( float ) containerX / imageX ; ret . deltaY = ( ( float ) containerY - ( ret . scale * imageY ) ) / 2.0f ; } else { // Image aspect taller than container ret . scale = ( float ) containerY / imageY ; ret . deltaX = ( ( float ) containerX - ( ret . scale * imageX ) ) / 2.0f ; } return ret ; }
This scales the image so that it fits within the container . The container may have empty space at the ends but the entire image will be displayed .