/** * 단말기 density 구함 * @param con * 사용법 : if(getDensity(context) == 2f && (float으로 형변환해서 사용 해야함.) */public static float getDensity(Context con) { float density = 0.0f; density = con.getResources().getDisplayMetrics().density; Log.d(TAG, "density = " + density); return density;}/** * px을 dp로 변환 * @param con * @param px * @return dp */public static int getPxToDp(Context con, int px) { float density = 0.0f; density = con.getResources().getDisplayMetrics().density; Log.d(TAG, "density = " + density); return (int)(px / density);}/** * dp를 px로 변환 * @param con * @param dp * @return px */public static int getDpToPix(Context con, double dp) { float density = 0.0f; density = con.getResources().getDisplayMetrics().density; Log.d(TAG, "density = " + density); return (int)(dp * density + 0.5);}/** * 단말기 가로 해상도 구하기 * @param activity * @return width */public static int getScreenWidth(Activity activity) { int width = 0; width = activity.getWindowManager().getDefaultDisplay().getWidth(); Log.i(TAG, "Screen width = " + width); return width;}/** * 단말기 세로 해상도 구하기 * @param activity * @return hight */public static int getScreenHeight(Activity activity) { int height = 0; height = activity.getWindowManager().getDefaultDisplay().getHeight(); Log.i(TAG, "Screen height = " + height); return height;}/** * 단말기 가로 해상도 구하기 * @param context */public static int getScreenWidth(Context context) { Display dis = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int width = dis.getWidth(); Log.i(TAG, "Screen Width = " + width); return width;}/** * 단말기 세로 해상도 구하기 * @param context */public static int getScreenHeight(Context context) { Display dis = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int height = dis.getHeight(); Log.i(TAG, "Screen height = " + height); return height;}
참조 : http://arabiannight.tistory.com/entry/328
'Android' 카테고리의 다른 글
| 안드로이드 구글맵 (0) | 2013.08.01 |
|---|---|
| 안드로이드/Android 픽셀(pixel), 디피(dp) 계산법 ~! (0) | 2013.03.28 |
| [안드로이드] LayoutInflater inflate 사용하기 (2) | 2011.12.09 |
| Android: Intent 활용 사례 (0) | 2011.11.16 |
| 안드로이드 표준코딩스타일 가이드 (0) | 2011.07.14 |