Develop
2016.10.21 11:16

[android] dp, px 서로 변환

Views 3379 Votes 0 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

# Context가 있는 Activity 내부용 함수

//dp를 px로 변환 (dp를 입력받아 px을 리턴)
public float convertDpToPixel(float dp){
    Resources resources = this.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return px;
}

//px을 dp로 변환 (px을 입력받아 dp를 리턴)
public float convertPixelsToDp(float px){
    Resources resources = this.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return dp;
}


# Context를 변수로 받아서 쓰는 유틸 라이브러리용 Static 함수

//dp를 px로 변환 (dp를 입력받아 px을 리턴)
public static float convertDpToPixel(float dp, Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return px;
}

//px을 dp로 변환 (px을 입력받아 dp를 리턴)
public static float convertPixelsToDp(float px, Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return dp;
}


?

List of Articles
No. Category Subject Author Date Views
745 Develop [js] 자바스크립트(Javascript) 코드를 동적으로 삽입하는 방법.. file hooni 2013.04.23 22879
744 Develop [ios] UILabel top alignㅎㅎ hooni 2013.04.23 22842
743 Develop 밸런싱 로봇.. 최종.. (관련 논문도 첨부) ㅋㅋ file hooni 2013.04.23 22822
742 Develop [vb] 문자열에서 태그 제거함수 (Visual Basic) file hooni 2013.04.23 22626
741 Develop [iphone] 화면 전환 Portrait & Landscape Mode hooni 2013.04.23 22311
740 Develop [doc] Json Framework 설치와 사용 file hooni 2013.04.23 21923
739 Develop [c] 재미있는 코딩.. file hooni 2003.04.23 21316
738 Develop 프로그래밍에서 foo, bar 함수의 유래 file hooni 2013.06.25 21264
737 Develop [c] 포인터 학습용 예제 소스 코드 file hooni 2013.04.23 21034
736 Develop [c] 이진트리/트리 순회법 코드(전위/중위/후위) 5 file hooni 2015.07.02 20926
735 Develop [c++] 디렉토리 내의 파일 찾기 FindFirstFile() 함수 6 hooni 2013.04.23 20587
734 Develop git 브런치 배우기 (링크) hooni 2013.07.09 20571
733 Develop [opengl] 컴퓨터 그래픽스 강의 자료(수업자료) file hooni 2003.04.23 20432
732 Develop [android] [번역] 안드로이드 Android Cloud to Device Messaging(C2DM) hooni 2013.04.23 20425
731 Develop [php] substr() 한글 자를 때 깨짐 방지 hooni 2014.01.09 20367
730 Develop [ios] UIView에서 상위 UIViewController 가져오기 hooni 2013.09.27 20161
Board Pagination Prev 1 5 6 7 8 9 ... 53 Next
/ 53