Expected Result
I have a some UI widgets whose dimensions are design for iPhone 4 Retina device. The unit is in pixels, e.g. a button with 30 pixels wide by 30 pixels high. I wanna copy the design style into Android devices, say the previous 30 by 30 button, takes 30/640 = 4.6875% of the screen width in iPhone 4 Retina and 30/960 = 9.375% of the screen height, then I expect it also takes 4.6875% of the Android device screen width, 9.375% of the screen height.
Problem
Don't know the size scale factor of iPhone 4 Retina device which is used in the following code.
Code
/**
* Change dip value to pixel value using density of current device
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
Log.d("ch", "density of current device : " + scale);
return (int) (dpValue * scale + 0.5f);
}
/**
* Change pixel value to dip value using density of current device
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
Log.d("congliu", "density of current device : " + scale);
return (int) (pxValue / scale + 0.5f);
}
Reference
iPhone 4 Retina
ppi : 326
resolution : 640 by 960 pixels
size scale factor : Unknown
Samsung Galaxy S
ppi : 233
resolution : 480 by 800 pixels
size scale factor : 1.5
Samsung Galaxy Note
ppi : 285
resolution : 800 by 1280 pixels
size scale factor : 2.0
xhdpi
, or the same2.0
scale factor as for the Note (withmdpi
as baseline). Also read: Supporting Multiple Screens. – MH.