Search code examples
androidandroid-layoutscale

Scale images or let it do Android?


I have several images in my application. I think all buttons i can offer in 9patch, but there is one central image, that has to be resized, if the display size changes. So I wondered whats the best way to have the right image size (and aspect ratio) with every display size.

A simple solution to this would be this:

LayoutParams lp = indicator.getLayoutParams();
lp.width = (int) (disW-(disW*0.04));
lp.height = (int) (disW-(disW*0.04));
indicator.setLayoutParams(lp);

If the displayWidth would be 320px, the width and height of my image would be 307px. This is working very well except of the quality of the scaled image. But the image quality could be bad, because of the stretched emulator device...i will test it on a real device soon.

The other possibility is to scale the image programmatically. For this I used this tutorial.

But i could work out how to keep the aspect ratio. Whats the best solution to this problem?


Solution

  • You have to change the layout params manually as you are doing so in your question.

    For Bitmaps, you can get their width and height attributes, and can therefore maintain the aspect ratio manually.