Contents

Develop
2016.10.21 11:16

[android] dp, px 서로 변환

조회 수 3382 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

# 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
번호 분류 제목 글쓴이 날짜 조회 수
969 Develop Laravel 5 Failed opening required bootstrap/../vendor/autoload.php hooni 2018.01.24 1665
968 Develop [ios] @property의 속성 (strong, weak, copy) 사용 경우 hooni 2014.08.08 1666
967 Develop [c] 한글 문자열 출력 hooni 2015.11.10 1684
966 Develop [python][django] request.cookie 읽어오기 ㅋㅋㅋ (쓰기) hooni 2019.12.06 1696
965 System/OS [linux] CentOS 에 APM 설치하기 hooni 2015.01.02 1698
964 Develop [php] mysql_ 과 mysqli_ 의 차이 hooni 2017.12.01 1700
963 System/OS CentOS 에서 Cacti 설치하기 hooni 2015.01.02 1772
962 Develop [ios] Xcode cannot run using the selected device hooni 2014.08.14 1799
961 Develop [swift] UIView에서 subview 찾기 hooni 2022.12.09 1804
960 Etc 인증서 *.p12 파일을 *.pem 파일로 변환 hooni 2015.04.30 1809
959 Develop [iOS] Xcode 불필요한 캐시 삭제하기 hooni 2021.10.12 1810
958 System/OS [mac] 맥에서 기본 실행 앱 변경하기 file hooni 2018.03.02 1817
Board Pagination Prev 1 ... 13 14 15 16 17 18 19 20 21 22 ... 98 Next
/ 98