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?
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, savecontext.getApplicationContext(), in case it was passed an Activity context.)EDIT: If you adopt this approach and are passing an
Activityto theFruitPoolobject, and the lifetime of theFruitPoolobject might exceed the lifetime of the activity, then you must not keep a reference to the activity. You should instead keep a reference tocontext.getApplicationContext(). SincegetWindowManager()is only defined for anActivity, you can instead use this expression to obtain theWindowManager: