DisplayMetrics returning all zero
I am trying to access some DisplayMetrics data and everything is returning zero. This is how I am initializing开发者_如何学C display metrics object:
DisplayMetrics display = new DisplayMetrics();
and when I call display.heightPixels
it's just zero. Do I need a permission or something to access DisplayMetrics
?
This is not how to use DisplayMetrics
.
This is how:
final WindowManager w = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
final Display d = w.getDefaultDisplay();
final DisplayMetrics m = new DisplayMetrics();
d.getMetrics(m);
// m now contains reliable data
I think you have to use getWindowManager().getDefaultDisplay().getMetrics(metrics);
, before call the method display.heightPixels
You should call: getWindowManager().getDefaultDisplay().getMetrics(display);
before calling display.heightPixels
精彩评论