2012년 5월 24일 목요일

dp px 변환

// Converts 14 dip into its equivalent px
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, r.getDisplayMetrics());

// Converts dp to px
public static float convertDpToPixel( float dp,Context context){
         Resources resources = context.getResources();
         DisplayMetrics metrics = resources.getDisplayMetrics();
         float px = dp * (metrics.densityDpi/160f);
         return px;
}

// Converts px to dp
public static float convertPixelsToDp( float px,Context context){
         Resources resources = context.getResources();
         DisplayMetrics metrics = resources.getDisplayMetrics();
         float dp = px / (metrics.densityDpi / 160f);
         return dp;
}

댓글 없음:

댓글 쓰기