How to get screen performance beyond the scope of the action?

I have a shared pool that I am doing here:

public class FruitPool extends GenericPool<Sprite> { // =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== private final TextureRegion mTextureRegion; private Scene mScene; // =========================================================== // Constructors // =========================================================== public FruitPool(final TextureRegion pFruitTextureRegion, Scene mScene2) { this.mTextureRegion = pFruitTextureRegion; this.mScene = mScene2; } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override protected Sprite onAllocatePoolItem() { Random rand = new Random(); //I want to get the Screens Display metrics here... Sprite fruit = new Sprite(0, 0, this.mTextureRegion); mScene.attachChild(fruit); return fruit; } 

I am trying to get screens to display such indicators.

  final Display display = getWindowManager().getDefaultDisplay(); CAMERA_WIDTH = display.getWidth(); CAMERA_HEIGHT = display.getHeight(); 

The only problem is that I cannot figure out how to do this outside of Activity.

Is it possible? Or will I have to use SharedPreference or something else?

+16
android
Dec 26 2018-11-12T00:
source share
4 answers

The simplest would be to pass the Context to the FruitPool constructor. Then it can get display metrics by calling Context.getWindowManager() . (If you want to run this code outside the constructor, save context.getApplicationContext() if an Activity context has been passed to it.)

EDIT: if you take this approach and pass the Activity to the FruitPool object, and the lifetime of the FruitPool object can exceed the lifetime of the activity, then you should not store the link to the Events. Instead, you should keep a reference to context.getApplicationContext() . Since getWindowManager() is defined only for Activity , you can use this expression instead to get WindowManager :

 (WindowManager) context.getSystemService(Context.WINDOW_SERVICE) 
+29
Dec 26 2018-11-12T00:
source share

Although there is an acceptable answer here, I am posting another answer. The reason for this is as follows. Passing contexts is not always a good idea for imho, because in some cases (for example, in application libraries, for example), contexts should not create additional and unnecessary dependencies from the application. The code is simple:

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

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

getSystem() returns a global resource share that provides access only to system resources (without application resources) and is not configured for the current screen (cannot use units, does not change depending on orientation, etc.).

In any case, all DisplayMetrics fields are populated with meaningful information. In my case, it was the DPI that I was. And the method provides me with DPI without context.

+80
May 18 '13 at 21:38
source share

Here's another way to get display metrics that configured for the current screen.

final DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();

+2
Jan 24 '17 at 8:16
source share

Here is the api I wrote to get the width of the screen, you need the context to get the window system service. In the same way, you can get the height.

 int getWindowWidth() { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point size = new Point(); display.getSize(size); return size.x; } 
+1
Jun 27 '17 at 11:25
source share



All Articles