views:

64

answers:

3

I am currently working on an android app that is causing me some trouble. My app is heavily dependent on images that are fitted correctly to the screen they are being displayed on. Unfortunately things like the FWVGA/WVGA resolution difference 854 vs 800 is causing me some real problems, and that's aside from the other sizes/densieites and resolutions.

I don't know if I'm just missing the point here but how do I go about having my app display correctly on both the resolutions without either "squashing" my images or cutting bits off of giving me black bars etc?

There doesn't seem an immediately present solution where I can just have it display a different image automatically from the drawable folder for the right screen.

I have tried using displaymetrics but unless i have my manifest claim that it supports all screen sizes, the android OS will in fact "lie" to my app about hte current resolution.

Someone please tell me there is an easy solution.

+1  A: 

how do I go about having my app display correctly on both the resolutions without either "squashing" my images or cutting bits off of giving me black bars etc?

IMHO, you start by revisiting your GUI design:

My app is heavily dependent on images that are fitted correctly to the screen they are being displayed on.

That does not work particularly well anywhere, IMHO. Windows (including browsers) can be sized to any desired size by the user, for example.

There doesn't seem an immediately present solution where I can just have it display a different image automatically from the drawable folder for the right screen.

Because Android devices can have any number of screen dimensions, just like windows and Web browsers can have any number of screen dimensions. Android does let you choose images based on density (e.g., -hdpi) or screen size (e.g., -large), but these do not map to resolutions, nor should they.

CommonsWare
We had this same discussion amongst the team today, that on a PC (Mac, etc) windows are resizable, however, on a phone they are not, and it is essential to the function of our app that images are displayed the correct size to maximise the screen realestate without cropping or destroying the aspect of the image. SO considering your argument, how do I solve this?
Hamid
@Hamid: If your images are stretchable, make them nine-patch PNG files.
CommonsWare
Unfortunately not, they are essentially "photographs" with nothing else on screen. I can't 9 patch them because, well, as I said. These "photgraphs" are rendered by us however, beforehand (not in runtime), how would it be possible to seperate each "subject" of the image in such a way that we can position them in runtime to look correct regardless of screen size or dimensions?
Hamid
@Hamid: What you want is mathematically impossible.
CommonsWare
Thanks for the answer. I think we have concluded on a solution.
Hamid
A: 

When it comes to displays and resolutions, it is not possible to have your images resized to any aspect ratio without either squishing / stretching them, or using letterboxes. I know this doesn't really answer you question, but if you can't get reliable screen size information from the OS, then try picking images for the largest resolution and cropping them for the smaller resolutions. Just make sure there is nothing interesting around the borders.

A.R.
This causes a major problem for us beause our content requires all of the image to be iewable and in the correct aspect. I am coming around to understanding your point, but I still don't see a solution there yet.
Hamid
A: 

As CommonsWare pointed out, your best bet for this situation really is to rethink your layout structure. It's probably possible to accomplish similar, if not identical results to what you currently have, using RelativeLayouts and widgets. If you're willing to post a sample, we could advise you on ways to do so.

That said, an answer to your question would be to query the device at runtime for its screen resolution (keeping in mind space taken up by the notification bar, as well as the title bar - unless you theme it without a title bar). You would need a different image for every possible screen resolution, and as more devices (notably tablets) begin to release, you're going to have to continue to provide more images and customized code for each different image as they release. You can see why this is not an ideal situation.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels();
int screenHeight = metrics.heightPixels();

Then you can place that into an if-then-else block, and determine the resolution, and set the image accordingly. I don't recommend this.

kcoppock
I implemented something using this method today but as you say, the tablets and future (unknown) screen resolutions are a concern for us. The app is fullscreen so I guess seperating every element of our images so they are resolution independant may be the only solution.
Hamid