如何获取活动之外的屏幕指标? [英] How to get Screen metrics outside an Activity?

查看:76
本文介绍了如何获取活动之外的屏幕指标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个通用池:

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();

唯一的问题是,我无法弄清楚如何在Activity之外执行此操作.

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

这甚至有可能吗?还是我必须使用SharedPreference或其他东西?

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

推荐答案

最简单的方法是将Context传递给FruitPool构造函数.然后,它可以通过调用 Context.getWindowManager()来检索显示指标.(如果要在构造函数之外运行该代码,请保存 context.getApplicationContext(),以防它传递了Activity上下文.)

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.)

如果您采用这种方法并将 Activity 传递给 FruitPool 对象,则 FruitPool 对象的生存期可能会超过活动的生存期,那么您就不要保留对活动的引用.相反,您应该保留对 context.getApplicationContext()的引用.由于 getWindowManager()仅是为 Activity 定义的,因此您可以改用此表达式来获取 WindowManager :

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)

这篇关于如何获取活动之外的屏幕指标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆