Contents

Develop
2016.10.21 11:16

[android] dp, px 서로 변환

조회 수 7618 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
663 Develop [Android Error] The number of method references in a .dex file cannot exceed 64K hooni 2016.11.10 5493
662 Develop [js] Javascript로 만든 포트리스 (2010) 5 file hooni 2017.03.03 5495
661 Develop [php][laravel] 라라벨 개발환경 세팅하기 (Mac, Window) 2 file hooni 2017.12.15 5556
660 Develop [js] show/hide 이벤트 감시 (Observing show/hide event) hooni 2021.02.03 5570
659 Develop [ios] 웹뷰 history.back() ㅋㄷ file hooni 2016.06.27 5674
658 Develop [php] Connect to Firebase Console in Laravel file hooni 2018.05.09 5736
657 Develop [ios] UITableView 특정 Row만 Update hooni 2014.04.08 5799
656 Develop [ios] Swift 4 Singleton inheritance hooni 2018.10.31 5938
655 Develop [android] 버전 별 앱 알림 설정으로 이동하는 방법 file hooni 2016.11.28 6043
654 Develop Laravel 5 Failed opening required bootstrap/../vendor/autoload.php hooni 2018.01.24 6126
653 Develop [ios] NSString, RegularExpression Find/Replace hooni 2017.04.14 6129
652 Develop XML, JSON, BSON, MSGPACK 장,단점 비교 file hooni 2017.01.11 6216
Board Pagination Prev 1 ... 11 12 13 14 15 16 17 18 19 20 ... 71 Next
/ 71