在Android中获取屏幕尺寸

13 浏览
0 Comments

在Android中获取屏幕尺寸

这个问题已经有答案了

如何在Android中获取屏幕尺寸(以像素为单位)

有两种方法可以获取屏幕尺寸。对于API级别大于13,这种方法对我起作用:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

对于版本在14以下的,这种方法对我也起作用:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

我能获取所有API级别的屏幕尺寸吗?请帮帮我。

admin 更改状态以发布 2023年5月23日
0
0 Comments
public void ScreenSize()
{
 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();
    }
}

(这是一段HTML代码,显示为粗体的数字123)

0
0 Comments

试试这个家伙。

int density;
    private DisplayMetrics metrics;
    private int widthPixels;
    private float scaleFactor;
    private float widthDp;
private DisplayMetrics metrics;
          metrics = new DisplayMetrics();
     getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
     density= getResources().getDisplayMetrics().densityDpi;
     widthPixels = metrics.widthPixels;
     scaleFactor = metrics.density;
     widthDp = widthPixels / scaleFactor;

0