How to get Screen metrics outside an Activity?

后端 未结 4 1771
名媛妹妹
名媛妹妹 2021-02-04 01:59

I have a Generic Pool that i am making here:

public class FruitPool extends GenericPool {
// ======================================================         


        
4条回答
  •  半阙折子戏
    2021-02-04 02:30

    Despite the fact there is an accepted answer here, I'm posting another answer. The reasoning for this is the following. Passing contexts is not always a good idea imho, because in some cases (such as applied libraries, for example) contexts should not build additional and unnecessary dependencies from an application. The code is simple:

    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    

    It provides a solution for cases when known limitations of this method are not important for a developer. According to Android documentation:

    getSystem() returns a global shared Resources object that provides access to only system resources (no application resources), and is not configured for the current screen (can not use dimension units, does not change based on orientation, etc).

    Anyway, all fields of DisplayMetrics are filled in with meaningful information. In my case, it was DPI which I was after. And the method provides me with DPI without a context.

提交回复
热议问题