How to get screen display metrics in application c

2020-01-27 06:48发布

If I put this in some activity class it works perfectly but, when I put it in my App class the method getWindowManager() can not be found. Is there some way to get the WindowManager in app class?

My app class is defined like this:

public class myApp extends Application {

and in on create method I have this:

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

5条回答
爷的心禁止访问
2楼-- · 2020-01-27 07:28

Here, Context.getResource()

DisplayMetrics dm = getResources().getDisplayMetrics(); 
int densityDpi = dm.densityDpi;
查看更多
贼婆χ
3楼-- · 2020-01-27 07:28

hope this will work for you

DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int pxWidth = displayMetrics.widthPixels;
int pxHeight = displayMetrics.heightPixels;
查看更多
家丑人穷心不美
4楼-- · 2020-01-27 07:40

Try this:

Display display = getWindowManager().getDefaultDisplay();
Log.e("", "" + display.getHeight() + " " + display.getWidth());
查看更多
欢心
5楼-- · 2020-01-27 07:52

You can also try this:

WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
final DisplayMetrics displayMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
查看更多
仙女界的扛把子
6楼-- · 2020-01-27 07:53
final DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

int width = dm.widthPixels;
int height = dm.heightPixels;

getWindow().setLayout((int) (width * .8), (int) (height * .5));
查看更多
登录 后发表回答