안드로이드 개발 질문/답변
(글 수 45,052)
레이아웃 작업을 하면서 pixel로 된 값을을
여러 해상도의 화면에 맞추기 위하여 dip 값으로 바꾸어서 xml 파일을 만들때
dip값으로 값을 정해주는데요
제가 하는 작업이 보통 해상도 480 x 800을 기준으로 pixel값들이 나오기때문에
이 pixel값에 2/3를 곱해서 값으로 사용하고 있습니다.
하지만 이렇게 할수 있는 경우는 xml에 값을 정해줄때의 얘기인데요
문제는 소스에서 bitmap이나 canvas를 사용하면 pixel값으로 값을 지정해줘야 하는데요
소스에서 bitmap이나 canvas를 사용할때 dip값을 사용할수 없나요?
아니면 pixel값을 dip값으로 바꿔주는 함수같은게 없나요?
여러 해상도의 화면에 맞추기 위하여 dip 값으로 바꾸어서 xml 파일을 만들때
dip값으로 값을 정해주는데요
제가 하는 작업이 보통 해상도 480 x 800을 기준으로 pixel값들이 나오기때문에
이 pixel값에 2/3를 곱해서 값으로 사용하고 있습니다.
하지만 이렇게 할수 있는 경우는 xml에 값을 정해줄때의 얘기인데요
문제는 소스에서 bitmap이나 canvas를 사용하면 pixel값으로 값을 지정해줘야 하는데요
소스에서 bitmap이나 canvas를 사용할때 dip값을 사용할수 없나요?
아니면 pixel값을 dip값으로 바꿔주는 함수같은게 없나요?
2010.04.02 22:57:18
px 은 멀티 스크린을 지원할때 문제가 되서 지양해야됩니다.
Converting from dips to pixels
In some cases, you will need to express dimensions in dip
and then convert them to pixels. Imagine an application in which a scroll gesture is recognized after the user's finger has moved by at least 16 pixels. On a baseline HVGA screen, the user will have to move his finger by 16 pixels / 160 dpi = 1/10th of an inch (or 2.5 mm) before the gesture is recognized. On a device with a high (240) density display, the user will move his finger by only 16 pixels / 240 dpi = 1/15th of an inch (or 1.7 mm.) The distance is much shorter and the application thus appears more sensitive to the user. To fix this issue, the gesture threshold must be expressed in the code in dip
and then converted to actual pixels.
// The gesture threshold expressed in dip
private static final float GESTURE_THRESHOLD_DIP = 16.0f;
// Convert the dips to pixels
final float scale = getContext().getResources().getDisplayMetrics().density;
mGestureThreshold = (int) (GESTURE_THRESHOLD_DIP * scale + 0.5f);
// Use mGestureThreshold as a distance in pixels
Calendar에서도 canvas를 사용하여 직접 그리는데 이때 다음과 같이 사용하여 계산하고 있습니다.
316 if (mScale == 0) {
317 mScale = getContext().getResources().getDisplayMetrics().density;
318 if (mScale != 1) {
319 SINGLE_ALLDAY_HEIGHT *= mScale;
320 MAX_ALLDAY_HEIGHT *= mScale;
321 ALLDAY_TOP_MARGIN *= mScale;
322 MAX_ALLDAY_EVENT_HEIGHT *= mScale;
324 NORMAL_FONT_SIZE *= mScale;
325 EVENT_TEXT_FONT_SIZE *= mScale;
326 HOURS_FONT_SIZE *= mScale;
327 AMPM_FONT_SIZE *= mScale;
328 MIN_CELL_WIDTH_FOR_TEXT *= mScale;
329 MIN_EVENT_HEIGHT *= mScale;
331 HORIZONTAL_SCROLL_THRESHOLD *= mScale;
333 SMALL_ROUND_RADIUS *= mScale;
334 }
335 }
참고소스