[android] How to scale an Image in ImageView to keep the aspect ratio

Programatically apply aspect ratio to Imageview:

aspectRatio = imageWidth/imageHeight   
ratioOfWidth = imageWidth/maxWidth  
ratioOfHeight = imageHeight/maxHeight  


if(ratioOfWidth > ratioOfHeight){???????
    imageWidth = maxWidth 
    imageHeight = imageWidth/aspectRatio
}??????? else if(ratioOfHeight > ratioOfWidth){???????
    imageHeight = maxHeight
    imageWidth = imageHeight * aspectRatio
}???????

After that you can use scaled bitmap to image view

Bitmap scaledBitmap= Bitmap.createScaledBitmap(bitmap, (int) imageWidth , (int) imageHeight , true);