获取android屏幕分辨率

时间:2014-12-10 13:09:25

标签: c# android monogame

我需要代码才能在API 10 +中获得Android屏幕分辨率

我正在使用monogame android

using Android.App;
_ScreenWidth = Resources.DisplayMetrics.WidthPixels;

此代码仅适用于Actvity1.cs,而不适用于Game1.cs,为什么会这样?我是using Android.App;陈述......

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        ScreenWidth = Resources.DisplayMetrics.WidthPixels;
        Game1.Activity = this;
        var g = new Game1();
        SetContentView(g.Window);
        g.Run();
    }

2 个答案:

答案 0 :(得分:1)

[EDITED]尝试使用此功能。

import android.util.DisplayMetrics;

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

int screen_width    = metrics.widthPixels;
int screen_height   = metrics.heightPixels;
float density       = metrics.density;

或者喜欢这个

import android.util.DisplayMetrics;
public class Resolution()
{
int screen_width;
int screen_height;
float density;

public Resolution(Activity activity)
{
    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

    screen_width    = metrics.widthPixels;
    screen_height   = metrics.heightPixels;
    density         = metrics.density;
}
}

并在您的活动类

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        Resolution res = new Resolution(this); //your activity in param
        int width = res.screen_width;
        int height = res.screen_height;

    }
}

答案 1 :(得分:0)

你可以试试这个

int measuredWidth = 0;
int measuredHeight = 0;
Point size = new Point();
WindowManager w = getWindowManager();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
{
    w.getDefaultDisplay().getSize(size);

    measuredWidth = size.x;
    measuredHeight = size.y;
}
else
{
    Display d = w.getDefaultDisplay();
    measuredWidth = d.getWidth();
    measuredHeight = d.getHeight();
}