获取设备的屏幕分辨率 [英] Get screen resolution of device

查看:139
本文介绍了获取设备的屏幕分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方法来获取屏幕尺寸:

I used following method to get the screen size:

public static Point getScreenSize(Context context)
{
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    int w = wm.getDefaultDisplay().getWidth();
    int h = wm.getDefaultDisplay().getHeight();
    return new Point(w, h);
}

在我的三星银河s6上,此方法返回1080x1920 ...尽管我的设备的分辨率为1440x2560.

On my samsung galaxy s6, this method returns 1080x1920... Although my device has a resolution of 1440x2560.

为什么?有没有更好的方法来使屏幕分辨率在新手机上也能可靠使用?

Why? Is there a better method to get the screen resolution that works on newer phones reliable as well?

编辑

我在服务中需要此方法!我没有视图/活动来寻求帮助,只有应用程序上下文!我需要真实像素

I need this method in a service! I don't have a view/activity for help, only the application context! And I need the REAL pixels

推荐答案

获得屏幕分辨率的最佳方法是来自DisplayMetrics:

The best way to get your screen resolution is from DisplayMetrics :

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;

这篇关于获取设备的屏幕分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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