gpt4 book ai didi

c# - 获取安卓屏幕分辨率

转载 作者:太空宇宙 更新时间:2023-11-03 10:36:57 24 4
gpt4 key购买 nike

我需要代码在 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();
}

最佳答案

[已编辑] 尝试使用它。

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;
}
}

在你的 Activity 课上

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;

}
}

关于c# - 获取安卓屏幕分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27401863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com