wufoo
wufoo

Reputation: 14571

Android mdpi drawables for use on both Droid phone and Xoom?

I have an app developed for a Droid (phone). In the app, I use:

Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.bg);

To set the background bitmap in the application. The bitmap is sized 480x800 which is the WVGA screen size of the Droid (without status or title bar).

Two questions: When I rotate the phone, the Droid goes into landscape mode and the background no longer fits correctly. How do I tell the App it needs to use landscape mode when I'm not using a layout?

Second, when I run this app on the Xoom, it picks the mdpi drawable which is nowhere near the size of the Xoom screen. How do I define a second mdpi drawable that has a 1280x800 size? I think if I were using layouts, I would create a layout-xlarge directory and place a 1280x800 background in the imageview of that layout, correct?

Thanks!

Update *: Thanks for the great answers! I upvoted everyone. I updated the code to use drawable-xlarge-port/drawable-xlarge-land for the Xoom and drawable-port-mdpi/drawable-land-mdpi for the Droid.

I'm not using layouts as I needed to draw directly onto the activity canvas and not sure how to use setContentView() from within the class where I extend View and hook into onDraw() in order to do the animation (would need to be a separate posting).

If I can figure out how to rotate the image myself I could indeed do away with the drawable-*-land and *-port directories.

Upvotes: 3

Views: 1787

Answers (3)

lilbyrdie
lilbyrdie

Reputation: 1907

That's just about right. You can mix in portrait, landscape, and other modifiers, too. Don't forget to use them with your drawables as well as your layouts (and any other resource directory, for that matter). If it's the same layout and different drawables, then you only need to apply it to the drawable, for instance.

You'll want to look at the documentation for resource directories and make use of the size and orientation directory modifiers.

For example:

drawable-land-mdpi
drawable-xlarge-land
layout-port-mdpi

and such

For more information, see:

http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources

http://developer.android.com/guide/practices/screens_support.html#qualifiers

Upvotes: 1

Yoni Samlan
Yoni Samlan

Reputation: 38075

You can target drawables, not just layouts. You want drawable-hdpi-large-long-land for the first situation (assuming you're letting the system handle rotation events and recreate your activity), and drawable-mdpi-xlarge for the second.

Of course it's futile to try to ship with a separate image for every conceivable phone resolution; partially because there's a lot you'd need, and partially because some targets aren't differentiable (the 854x480 and 800x480 phones out there are all hdpi-large-long, for example, despite the 54px difference). Try to use a scaleable image, a stretchable 9patch, or an image you can just fix at the top-right corner (you can define stretch behaviors using an XML-defined bitmapdrawable).

Upvotes: 3

Laurent Etiemble
Laurent Etiemble

Reputation: 27889

When I rotate the phone, the Droid goes into landscape mode and the background no longer fits correctly. How do I tell the App it needs to use landscape mode when I'm not using a layout?

If you alreay detect the rotation, then the Device class is the way to go.

Second, when I run this app on the Xoom, it picks the mdpi drawable which is nowhere near the size of the Xoom screen. How do I define a second mdpi drawable that has a 1200x800 size?

You can use various image folders dependending on the resolution of the device: ldpi, mdpi, hdpi or xhdpi. You should take a look at the following page for proper support of various resolutions and screen sizes.

Upvotes: 1

Related Questions