获取屏幕宽高

来源:互联网 发布:裁剪软件 编辑:程序博客网 时间:2024/04/30 12:11

我的程序初始化是设置全屏显示,即(不显示状态栏与标题栏)

nCreat或onResume获得屏幕宽高:

Rect mRect = new Rect();

Window win = DisplayColor.this.getWindow();

win.getDecorView().getWindowVisibleDisplayFrame(mRect);

int height = mRect.getBottom();
int width =mRect.getRight(); 

宽高是不包含标题栏、状态栏的高度(w:1280,h:752)

由于程序是画图显示界面。

在绘图OnDraw函数里获取屏幕宽高就是屏幕物理真正的宽高了(w:1280,h:800)。


同样功能:

View v = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
int height = v.getBottom();

int width = v.getRight(); (w:1280,h:800)。

在绘图OnDraw函数里获取宽高:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;

height = dm.heightPixels

(w:1280,h:752)想来DisplayMetrics它是只获取不包含标题栏、状态栏的屏幕宽度高度。

原创粉丝点击