1

While we're at it, what is the Android equivalent of [UIScreen mainScreen].scale?

Are the following equations correct?

[UIScreen mainScreen].bounds.size.width = displayMetrics.widthPixels
[UIScreen mainScreen].scale = displayMetrics.density

where you get displayMetrics like so:

DisplayMetrics displayMetrics = new DisplayMetrics();
Display display = ((WindowManager) someContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
display.getMetrics(displayMetrics);

The thing is that I'm confused by the wording for iOS versus Android.

The definitions for those UIScreen values are these:

bounds

Contains the bounding rectangle of the screen, measured in points. (read-only)

scale

The natural scale factor associated with the screen. (read-only)

@property(nonatomic, readonly) CGFloat scale

Discussion

This value reflects the scale factor needed to convert from the default logical coordinate space into the device coordinate space of this screen. The default logical coordinate space is measured using points, where one point is approximately equal to 1/160th of an inch. If a device’s screen has a reasonably similar pixel density, the scale factor is typically set to 1.0 so that one point maps to one pixel. However, a screen with a significantly different pixel density may set this property to a higher value.

I am wondering whether the width of the bounds is equivalent to the widthPixels of the DisplayMetrics and whether the scale value is equivalent to the density value on Android.

boo-urns
  • 10,136
  • 26
  • 71
  • 107
  • I've never programmed on iOS but read [Android: How to get screen dimensions](http://stackoverflow.com/q/1016896/1267661) to get the screen's width. Can't help you with `[UIScreen mainScreen].scale` I have no idea what you're after or what that is. – Sam Nov 29 '12 at 00:10
  • The thing is that I'm getting tripped up on the terminology -- along with the fact that I don't trust whoever wrote the docs to be using the terms correctly. I'm going to update the question with some more detail. – boo-urns Nov 29 '12 at 00:17

2 Answers2

6
[UIScreen mainScreen].bounds.size.width = displayMetrics.widthPixels

Correct, width of the screen (display) in pixels, nothing complicated here.

[UIScreen mainScreen].scale = displayMetrics.density

Not really. They are similar values but definitely not equal.

I'll try to explain what scale is. We have iPad 1 with screen resolution 1024x768 and we have iPad 3 with double resolution (apple marketing calls it Retina) and we want applications to work on both devices. So, the application works on resolution 1024x768 (logical points) but the OS translates to physical pixels on every device using the scale (scale=1.0 on iPad 1, scale=2.0 on iPad 3). For example, when you draw a rectangle in logical coordinates (1, 1, 20, 40), on iPad 3 it will be drawn on pixels (2, 2, 40, 80).

There are currently only two values defined: 1.0 and 2.0.

The density is a similar scaling factor but calculated differently

This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen Note that again it converts logical points (called DIP on Android) into screen pixels.

The difference between iOS scale and Android density is that the logical unit is defined differently.

raidfive
  • 6,603
  • 1
  • 35
  • 32
Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Interesting! So, there is absolutely no way to get the same scale value on Android? – boo-urns Nov 29 '12 at 00:56
  • 1
    @DarrenGreen The real question is - why would somebody wanted to do it? There is now way how to use `scale` on Android an no way ho to use `density` on iOS. In most situations you use only the logical points and don't care about pixels. – Sulthan Nov 29 '12 at 08:20
-3

I use this:

    private void SetScreenSizes() {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        intScreenWidth = displaymetrics.widthPixels;
}
daniel
  • 1,435
  • 9
  • 16
  • You're just repeating what I wrote in my question. The main question is: is that equivalent to `[UIScreen mainScreen].bounds.size.width`? – boo-urns Nov 29 '12 at 00:28
  • Why dont you log both and see? – daniel Nov 29 '12 at 00:30
  • I don't understand how that would work. I don't have an Android device that will tell me the bounds of its UIScreen and I don't have an iOS device that will me the widthPixels for its DisplayMetrics. Do you see what I'm getting at? I also don't have an Android device that exactly matches the screen size and resolution of an iOS device. – boo-urns Nov 29 '12 at 00:36