开发技巧安卓基础知识

获取屏幕或组件的各种大小尺寸等

2016-01-01  本文已影响2384人  JupiterY

一、获取屏幕分辨率、像素密度、尺寸:

1.获取屏幕分辨率:

//方式一, 通过WindowManager获取 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); System.out.println("heigth : " + dm.heightPixels); System.out.println("width : " + dm.widthPixels); // 方式二,通过Resources获取 DisplayMetrics dm2 = getResources().getDisplayMetrics(); System.out.println("heigth2 : " + dm2.heightPixels); System.out.println("width2 : " + dm2.widthPixels); // 方式三,获取屏幕的默认分辨率 Display display = getWindowManager().getDefaultDisplay(); System.out.println("width-display :" + display.getWidth()); System.out.println("heigth-display :" + display.getHeight()); //方式四,通过getSize获取分辨率 Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y;
说明:方式一与方式二必须在Activity中进行调用,因为他们是依靠getWindowManager获取的。方式三适用于非Activity中获取屏幕分辨率。
由于方法方法三只适用于API13之前使用,而之后则推荐使用方法四,可以用以下方法适配:
if (android.os.Build.VERSION.SDK_INT >= 13) { display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); width = size.x; height = size.y; }else { display = getWindowManager().getDefaultDisplay(); width = display.getWidth(); height = display.getHeight(); }

2.获取屏幕的像素密度:

//方法一 DisplayMetrics dm = new DisplayMetrics(); dm = getResources().getDisplayMetrics(); float density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0) int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320) //方法二 dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0) densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)

3.获取屏幕的尺寸:

DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); double x = Math.pow(dm.widthPixels/dm.xdpi,2); double y = Math.pow(dm.heightPixels/dm.ydpi,2); double screenInches = Math.sqrt(x+y); //屏幕尺寸(英寸)

4.获取文字的大小:

TextPaint paint = textView.getPaint(); float len = paint.measureText(string);

5.获取状态栏的高度:

//方式一 public static int getStatusBarHeight(Context context){ Class<?> c = null; Object obj = null; Field field = null; int x = 0, statusBarHeight = 0; try { c = Class.forName("com.android.internal.R$dimen"); obj = c.newInstance(); field = c.getField("status_bar_height"); x = Integer.parseInt(field.get(obj).toString()); statusBarHeight = context.getResources().getDimensionPixelSize(x); } catch (Exception e1) { e1.printStackTrace(); } return statusBarHeight; } //方式二: DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels; //屏幕宽 int height = dm.heightPixels; //屏幕高 Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; //状态栏高 int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop(); int titleBarHeight = contentTop - statusBarHeight; //标题栏高

6.获得导航栏的高度:

public int getNavigationBarHeight(Activity activity) { Resources resources = activity.getResources(); int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android"); //获取NavigationBar的高度 int height = resources.getDimensionPixelSize(resourceId); return height; }

7.获取组件大小:

上一篇下一篇

猜你喜欢

热点阅读