Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Screen metrics outside an Activity?

Tags:

I have a Generic Pool that i am making 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 the screens display metrics like this..

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

The only problem is, is that i cant figure out how to do this outside of an Activity..

Is this even possible? Or will i have to use SharedPreference or something?

like image 221
coder_For_Life22 Avatar asked Dec 26 '11 04:12

coder_For_Life22


People also ask

How do you get Screenwidth on Android?

1. Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size.


2 Answers

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.

like image 96
Stan Avatar answered Sep 28 '22 19:09

Stan


The simplest thing would be to pass a Context to the FruitPool constructor. It could then retrieve the display metrics by calling Context.getWindowManager(). (If you want to run that code outside the constructor, save context.getApplicationContext(), in case it was passed an Activity context.)

EDIT: If you adopt this approach and are passing an Activity to the FruitPool object, and the lifetime of the FruitPool object might exceed the lifetime of the activity, then you must not keep a reference to the activity. You should instead keep a reference to context.getApplicationContext(). Since getWindowManager() is only defined for an Activity, you can instead use this expression to obtain the WindowManager:

(WindowManager) context.getSystemService(Context.WINDOW_SERVICE) 
like image 45
Ted Hopp Avatar answered Sep 28 '22 19:09

Ted Hopp