Android: 如何找到屏幕的宽度和高度?
Android: How to find width and height of screen?
我正在尝试查找屏幕的宽度和高度,但我尝试过的 none 方法将在我创建的 class 中起作用,我的代码如下。
有人知道怎么找吗?我不能使用我在下面尝试的方式,因为 .getWidth() 已被弃用?
public class Crate {
public int acrossCrate;
public int upDownCrate;
Context theContext;
WindowManager wm = (WindowManager)theContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();
public Crate() {
acrossCrate = 650;
upDownCrate = 650;
//int width = ;
//int height = ;
}
}
使用下面的代码
private int getScreenHeight(Context context) {
int height;
if (android.os.Build.VERSION.SDK_INT >= 13) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
height = size.y;
} else {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
height = display.getHeight(); // deprecated
}
return height;
}
我正在尝试查找屏幕的宽度和高度,但我尝试过的 none 方法将在我创建的 class 中起作用,我的代码如下。
有人知道怎么找吗?我不能使用我在下面尝试的方式,因为 .getWidth() 已被弃用?
public class Crate {
public int acrossCrate;
public int upDownCrate;
Context theContext;
WindowManager wm = (WindowManager)theContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();
public Crate() {
acrossCrate = 650;
upDownCrate = 650;
//int width = ;
//int height = ;
}
}
使用下面的代码
private int getScreenHeight(Context context) {
int height;
if (android.os.Build.VERSION.SDK_INT >= 13) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
height = size.y;
} else {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
height = display.getHeight(); // deprecated
}
return height;
}