안드로이드에서는 Dimension 단위가 종류별로 있습니다.
총 7가지가 있으며, 그 종류는 다음과 같습니다.

 1.px(pixel) : 화면 위의 점
 2.in(inches) : 실제 스크린에 기반한 크기
 3.mm(millimeters) : 실제 스크린에 기반한 크기
 4.pt(points) : 인치의 1/72 사이즈
 5.dp(density-independent pixels, 밀도에 독립적인 화소): 화면의 밀도에 기초한 추상 
         단위, 인치당 160개의 점이 있는 디스플레이에서 1dp는 1px과 같다.
 6.dip : 구글 예제에서 자주 쓰이는 dp의 동의어
 7.sp(scale-independent pixels, 스케일에 독립적인 화소) : dp와 유사하지만 사용자의
         글꼴 크기 설정에 따라 확대/축소 될수 있다.

위와 같이 7가지의 단위가 존재하며, 각각의 단위를 소스상에서 다른 단위로 변환을 할 수 있습니다. 변환 방법은 TypedValue api를 사용하는 방법이 있습니다.
계산 방법은 다음과 같습니다.

 private static final float GESTURE_THRESHOLD_DIP = 16.0f; //상수 정의
 mGestureThreshold = TypedValue.applyDimension(
                                                       TypedValue.COMPLEX_UNIT_DIP,
                                                       GESTURE_THRESHOLD_DIP,
                                                       getResources().getDisplayMetrics());

applyDimension함수의 첫번째 인자는 TypedValue.COMPLEX_UNIT_DIP가 뜻하는 
값으로 바꾸겠다는 것입니다. 아래의 표를 보시면 아시겟지만, 
COMPLEX_UNIT_DIP 는 DIP값을 px로 값을 바꿔 주는 역할을 하고 있습니다.

Constants
int COMPLEX_MANTISSA_MASK Complex data: mask to extract mantissa information (after shifting byCOMPLEX_MANTISSA_SHIFT).
int COMPLEX_MANTISSA_SHIFT Complex data: bit location of mantissa information.
int COMPLEX_RADIX_0p23 Complex data: the mantissa magnitude is 0 bits -- i.e, 0x0.nnnnnn
int COMPLEX_RADIX_16p7 Complex data: the mantissa magnitude is 16 bits -- i.e, 0xnnnn.nn
int COMPLEX_RADIX_23p0 Complex data: the mantissa is an integral number -- i.e., 0xnnnnnn.0
int COMPLEX_RADIX_8p15 Complex data: the mantissa magnitude is 8 bits -- i.e, 0xnn.nnnn
int COMPLEX_RADIX_MASK Complex data: mask to extract radix information (after shifting byCOMPLEX_RADIX_SHIFT).
int COMPLEX_RADIX_SHIFT Complex data: where the radix information is, telling where the decimal place appears in the mantissa.
int COMPLEX_UNIT_DIP TYPE_DIMENSION complex unit: Value is Device Independent Pixels.
int COMPLEX_UNIT_FRACTION TYPE_FRACTION complex unit: A basic fraction of the overall size.
int COMPLEX_UNIT_FRACTION_PARENT TYPE_FRACTION complex unit: A fraction of the parent size.
int COMPLEX_UNIT_IN TYPE_DIMENSION complex unit: Value is in inches.
int COMPLEX_UNIT_MASK Complex data: mask to extract unit information (after shifting byCOMPLEX_UNIT_SHIFT).
int COMPLEX_UNIT_MM TYPE_DIMENSION complex unit: Value is in millimeters.
int COMPLEX_UNIT_PT TYPE_DIMENSION complex unit: Value is in points.
int COMPLEX_UNIT_PX TYPE_DIMENSION complex unit: Value is raw pixels.
int COMPLEX_UNIT_SHIFT Complex data: bit location of unit information.
int COMPLEX_UNIT_SP TYPE_DIMENSION complex unit: Value is a scaled pixel.
int DENSITY_DEFAULT If density is equal to this value, then the density should be treated as the system's default density value: DENSITY_DEFAULT.
int DENSITY_NONE If density is equal to this value, then there is no density associated with the resource and it should not be scaled.
int TYPE_ATTRIBUTE The data field holds an attribute resource identifier (referencing an attribute in the current theme style, not a resource entry).
int TYPE_DIMENSION The data field holds a complex number encoding a dimension value.
int TYPE_FIRST_COLOR_INT Identifies the start of integer values that were specified as color constants (starting with '#').
int TYPE_FIRST_INT Identifies the start of plain integer values.
int TYPE_FLOAT The data field holds an IEEE 754 floating point number.
int TYPE_FRACTION The data field holds a complex number encoding a fraction of a container.
int TYPE_INT_BOOLEAN The data field holds 0 or 1 that was originally specified as "false" or "true".
int TYPE_INT_COLOR_ARGB4 The data field holds a color that was originally specified as #argb.
int TYPE_INT_COLOR_ARGB8 The data field holds a color that was originally specified as #aarrggbb.
int TYPE_INT_COLOR_RGB4 The data field holds a color that was originally specified as #rgb.
int TYPE_INT_COLOR_RGB8 The data field holds a color that was originally specified as #rrggbb.
int TYPE_INT_DEC The data field holds a number that was originally specified in decimal.
int TYPE_INT_HEX The data field holds a number that was originally specified in hexadecimal (0xn).
int TYPE_LAST_COLOR_INT Identifies the end of integer values that were specified as color constants.
int TYPE_LAST_INT Identifies the end of plain integer values.
int TYPE_NULL The value contains no data.
int TYPE_REFERENCE The data field holds a resource identifier.
int TYPE_STRING The string field holds string data.

'안드로이드' 카테고리의 다른 글

Android Manifest Element  (0) 2011.01.16
애니메이션 기능  (0) 2011.01.16
AlarmManager의 Alarm Types  (0) 2011.01.14
Alarms  (0) 2011.01.14
자주 이용하는 블로그  (0) 2011.01.13

+ Recent posts