Proper way to get DisplayMetrics: getResources() or getWindowManager()

后端 未结 1 643
有刺的猬
有刺的猬 2021-02-01 06:28

What\'s the proper way to get DisplayMetrics, i.e. get screen/display info such as density or xdpi?

I ask because I\'ve seen two ways of going

相关标签:
1条回答
  • 2021-02-01 06:55
    public DisplayMetrics getDisplayMetrics ()
    

    Return the current display metrics that are in effect for this resource object. The returned object should be treated as read-only.

    public void getMetrics (DisplayMetrics outMetrics)
    

    Initialize a DisplayMetrics object from this display's data.

    The difference is that the metrics returned by the Resources method is the metrics for that particular Resources object. You can always create a new Resources instance with the constructor

    Resources(AssetManager assets, DisplayMetrics metrics, Configuration config)
    

    in which you can set any metrics you want, not necessarily the metrics you can get from the Display instance returned by the method getDefaultDisplay().

    That's the difference. It may not make a difference though in the values if you're using just the default resources and the default display.

    0 讨论(0)
提交回复
热议问题