Wednesday, November 16, 2011

Pre-scaling, Auto-Scaling and Screen Density

Designing android layout for multiple screens is really confusing.

Pre-scaling

Pre-scaling resource is an important concept.
If resources are not available in the correct density, the system loads the default resources and scales them up or down as needed to match the current screen's density.

Examples:
for an hdpi device, when there is only mdpi image 100x100, then the image will be pre-scaled to 150x150;
for an mdpi device, when there is only hdpi image 150x150, then the image will be pre-scaled to 100x100;



Auto-Scaling

When pre-scaling is disabled, system will scale image at draw time.
Auto-scaling is CPU expensive, but use less memory.
"In this case, the system auto-scales any absolute pixel coordinates and pixel dimension values at draw time. It does this to ensure that pixel-defined screen elements are still displayed at approximately the same physical size as they would be at the baseline screen density (mdpi)."
Bitmap created at runtime will auto-scaled

The system assumes that the bitmap is designed for the baseline mdpi screen. The system applies "auto-scaling" to a Bitmap when the bitmap has unspecified density properties, so that it look like the same(same dp). For example, 100*100px bitmap, will auto-scaled to 150*150px on hdpi screen.

It's Recommented to use "dp" or  "wrap_content"

The system then scales bitmap drawables as appropriate in order to display at the appropriate size, based on the appropriate scaling factor for the current screen's density.

dip or dp

dip or dp : density-independent pixel
Examples:
100dp means 150px on hdpi device
100dp means 100px on mdpi device


wrap_content

if image is 100*100px, wrap_content turns out to be
-- 100*100px on mdpi,
-- 150*150px on hdpi,
because the image is scaled to appropriate size by the system.


Reference:
Supporting Multiple Screens 
http://developer.android.com/guide/practices/screens_support.html

Providing Resources
http://developer.android.com/guide/topics/resources/providing-resources.htm

Screen Geometry Fun 
http://android-developers.blogspot.com/2010/09/screen-geometry-fun.html

 


No comments:

Post a Comment