我创建了一些自定义元素,并希望以编程方式将它们放置在右上角(距离上边缘n个像素,距离右边缘m个像素)。因此,我需要获得屏幕宽度和屏幕高度,然后设置位置:
int px = screenWidth - m;
int py = screenHeight - n;
如何在主活动中获取screenWidth和screenHeight?
我创建了一些自定义元素,并希望以编程方式将它们放置在右上角(距离上边缘n个像素,距离右边缘m个像素)。因此,我需要获得屏幕宽度和屏幕高度,然后设置位置:
int px = screenWidth - m;
int py = screenHeight - n;
如何在主活动中获取screenWidth和screenHeight?
如果您想要以像素为单位的显示尺寸,可以使用getSize:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
如果您不在“活动”中,则可以通过WINDOW_SERVICE获取默认显示:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
如果您在一个片段中,并且想要实现这一点,只需使用Activity.WindowManager(在Xamarin.Android中)或getActivity().getWindowManager()(在java中)。
在引入getSize之前(在API级别13中),您可以使用现在已弃用的getWidth和getHeight方法:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
然而,对于用例,您正在描述布局中的边距/填充似乎更合适。
另一种方法是:DisplayMetrics
一种结构,描述有关显示器的一般信息,如大小、密度和字体比例。要访问DisplayMetrics成员,请初始化如下对象:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
我们可以使用widthPixels获取以下信息:
“显示的绝对宽度(以像素为单位)。”
例子:
Log.d("ApplicationTagName", "Display width in px is " + metrics.widthPixels);
API等级30更新
final WindowMetrics metrics = windowManager.getCurrentWindowMetrics();
// Gets all excluding insets
final WindowInsets windowInsets = metrics.getWindowInsets();
Insets insets = windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars()
| WindowInsets.Type.displayCutout());
int insetsWidth = insets.right + insets.left;
int insetsHeight = insets.top + insets.bottom;
// Legacy size that Display#getSize reports
final Rect bounds = metrics.getBounds();
final Size legacySize = new Size(bounds.width() - insetsWidth,
bounds.height() - insetsHeight);
一种方法是:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
它已被弃用,您应该尝试以下代码。前两行代码提供DisplayMetrics对象。此对象包含高度像素、宽度像素等字段。
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Api级别30更新
final WindowMetrics metrics = windowManager.getCurrentWindowMetrics();
// Gets all excluding insets
final WindowInsets windowInsets = metrics.getWindowInsets();
Insets insets = windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars()
| WindowInsets.Type.displayCutout());
int insetsWidth = insets.right + insets.left;
int insetsHeight = insets.top + insets.bottom;
// Legacy size that Display#getSize reports
final Rect bounds = metrics.getBounds();
final Size legacySize = new Size(bounds.width() - insetsWidth,
bounds.height() - insetsHeight);
它可能无法回答您的问题,但如果你需要一个视图的维度,但你的代码在尚未布局时(例如在onCreate()中)正在执行,你可以使用View.getViewTreeObserver().addOnGlobalLayoutListener()设置一个ViewTreeObserver.OnGlobalLayout侦听器,并将需要该视图的相关代码放入尺寸。布局布局完成后,将调用侦听器的回调。
对于使用XML进行动态缩放,有一个名为“android:layout_weight”的属性
下面的示例是根据synic在这个线程上的响应修改的,显示了一个占屏幕75%的按钮(权重=.25)和一个占剩余25%的文本视图(权重=.75)。
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="somebutton">
<TextView android:layout_width="fill_parent"
android:layout_height="Wrap_content"
android:layout_weight=".75">
</LinearLayout>
只是补充弗朗西斯科的答案。如果你想找出窗口中的位置或屏幕中的位置,另一个更合适的观察者是ViewTreeObserver.OnPreDrawListener()
这也可用于查找在onCreate()时大部分未知的视图的其他属性,例如滚动位置、缩放位置。
在“活动”中使用以下代码。
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int wwidth = metrics.widthPixels;
查找屏幕的宽度和高度:
width = getWindowManager().getDefaultDisplay().getWidth();
height = getWindowManager().getDefaultDisplay().getHeight();
使用这个,我们可以获得最新的和更高版本的SDK 13。
// New width and height
int version = android.os.Build.VERSION.SDK_INT;
Log.i("", " name == "+ version);
Display display = getWindowManager().getDefaultDisplay();
int width;
if (version >= 13) {
Point size = new Point();
display.getSize(size);
width = size.x;
Log.i("width", "if =>" +width);
}
else {
width = display.getWidth();
Log.i("width", "else =>" +width);
}
public class AndroidScreenActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
String str_ScreenSize = "The Android Screen is: "
+ dm.widthPixels
+ " x "
+ dm.heightPixels;
TextView mScreenSize = (TextView) findViewById(R.id.strScreenSize);
mScreenSize.setText(str_ScreenSize);
}
}
(2012年的答案,可能已经过时了)如果你想支持前蜂窝,你需要在API 13之前引入向后兼容性。类似于:
int measuredWidth = 0;
int measuredHeight = 0;
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Point size = new Point();
w.getDefaultDisplay().getSize(size);
measuredWidth = size.x;
measuredHeight = size.y;
} else {
Display d = w.getDefaultDisplay();
measuredWidth = d.getWidth();
measuredHeight = d.getHeight();
}
当然,被弃用的方法最终会从最新的SDK中删除,但尽管我们仍然依赖大多数拥有Android 2.1、2.2和2.3的用户,但这就是我们剩下的。
我有两个函数,一个用于发送上下文,另一个用于获取像素的高度和宽度:
public static int getWidth(Context mContext){
int width=0;
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
if(Build.VERSION.SDK_INT>12){
Point size = new Point();
display.getSize(size);
width = size.x;
}
else{
width = display.getWidth(); // Deprecated
}
return width;
}
and
public static int getHeight(Context mContext){
int height=0;
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
if(Build.VERSION.SDK_INT>12){
Point size = new Point();
display.getSize(size);
height = size.y;
}
else{
height = display.getHeight(); // Deprecated
}
return height;
}
首先加载XML文件,然后编写以下代码:
setContentView(R.layout.main);
Display display = getWindowManager().getDefaultDisplay();
final int width = (display.getWidth());
final int height = (display.getHeight());
根据屏幕分辨率显示宽度和高度。
有一种使用DisplayMetrics(API 1)来实现这一点的方法并不过时,它可以避免try/catch混乱:
// initialize the DisplayMetrics object
DisplayMetrics deviceDisplayMetrics = new DisplayMetrics();
// populate the DisplayMetrics object with the display characteristics
getWindowManager().getDefaultDisplay().getMetrics(deviceDisplayMetrics);
// get the width and height
screenWidth = deviceDisplayMetrics.widthPixels;
screenHeight = deviceDisplayMetrics.heightPixels;
这是我用于任务的代码:
// `activity` is an instance of Activity class.
Display display = activity.getWindowManager().getDefaultDisplay();
Point screen = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(screen);
} else {
screen.x = display.getWidth();
screen.y = display.getHeight();
}
看起来很干净,但要注意贬低。
需要说明的是,如果您不在“活动”中,而是在“视图”中(或在范围中具有视图类型的变量),则不需要使用WINDOW_SERVICE。那么你至少可以使用两种方法。
第一:
DisplayMetrics dm = yourView.getContext().getResources().getDisplayMetrics();
第二:
DisplayMetrics dm = new DisplayMetrics();
yourView.getDisplay().getMetrics(dm);
我们在这里调用的所有这些方法都没有被弃用。
我尝试了所有可能的“解决方案”,但都失败了,我注意到Elliott Hughes的“Dalvik Explorer”应用程序在任何Android设备/操作系统版本上都始终显示正确的维度。我最后看了他的开源项目,可以在这里找到:https://code.google.com/p/enh/
以下是所有相关代码:
WindowManager w = activity.getWindowManager();
Display d = w.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
d.getMetrics(metrics);
// since SDK_INT = 1;
widthPixels = metrics.widthPixels;
heightPixels = metrics.heightPixels;
try {
// used when 17 > SDK_INT >= 14; includes window decorations (statusbar bar/menu bar)
widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
} catch (Exception ignored) {
}
try {
// used when SDK_INT >= 17; includes window decorations (statusbar bar/menu bar)
Point realSize = new Point();
Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
widthPixels = realSize.x;
heightPixels = realSize.y;
} catch (Exception ignored) {
}
EDIT:略有改进的版本(避免在不受支持的OS版本上触发异常):
WindowManager w = activity.getWindowManager();
Display d = w.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
d.getMetrics(metrics);
// since SDK_INT = 1;
widthPixels = metrics.widthPixels;
heightPixels = metrics.heightPixels;
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
try {
widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
} catch (Exception ignored) {
}
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 17)
try {
Point realSize = new Point();
Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
widthPixels = realSize.x;
heightPixels = realSize.y;
} catch (Exception ignored) {
}
我会这样包装getSize代码:
@SuppressLint("NewApi")
public static Point getScreenSize(Activity a) {
Point size = new Point();
Display d = a.getWindowManager().getDefaultDisplay();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
d.getSize(size);
} else {
size.x = d.getWidth();
size.y = d.getHeight();
}
return size;
}
这不是更好的解决方案吗?DisplayMetrics提供了您所需的一切,并从API 1开始工作。
public void getScreenInfo(){
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
heightPixels = metrics.heightPixels;
widthPixels = metrics.widthPixels;
density = metrics.density;
densityDpi = metrics.densityDpi;
}
您也可以使用getRealMetrics获得实际显示(包括屏幕装饰,如状态栏或软件导航栏),但这仅适用于17+。
我错过了什么吗?
在活动的onCreate中,有时需要知道布局可用空间的精确尺寸。经过一番思考,我想出了这种方法。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivityForResult(new Intent(this, Measure.class), 1);
// Return without setting the layout, that will be done in onActivityResult.
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
// Probably can never happen, but just in case.
if (resultCode == RESULT_CANCELED) {
finish();
return;
}
int width = data.getIntExtra("Width", -1);
// Width is now set to the precise available width, and a layout can now be created. ...
}
}
public final class Measure extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Create a LinearLayout with a MeasureFrameLayout in it.
// Just putting a subclass of LinearLayout in works fine, but to future proof things, I do it this way.
LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams matchParent = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
MeasureFrameLayout measureFrameLayout = new MeasureFrameLayout(this);
measureFrameLayout.setLayoutParams(matchParent);
linearLayout.addView(measureFrameLayout);
this.addContentView(linearLayout, matchParent);
// measureFrameLayout will now request this second activity to finish, sending back the width.
}
class MeasureFrameLayout extends FrameLayout {
boolean finished = false;
public MeasureFrameLayout(Context context) {
super(context);
}
@SuppressLint("DrawAllocation")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (finished) {
return;
}
finished = true;
// Send the width back as the result.
Intent data = new Intent().putExtra("Width", MeasureSpec.getSize(widthMeasureSpec));
Measure.this.setResult(Activity.RESULT_OK, data);
// Tell this activity to finish, so the result is passed back.
Measure.this.finish();
}
}
}
如果出于某种原因,您不想将另一个活动添加到Android清单中,可以这样做:
public class MainActivity extends Activity {
static Activity measuringActivity;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras == null) {
extras = new Bundle();
}
int width = extras.getInt("Width", -2);
if (width == -2) {
// First time in, just start another copy of this activity.
extras.putInt("Width", -1);
startActivityForResult(new Intent(this, MainActivity.class).putExtras(extras), 1);
// Return without setting the layout, that will be done in onActivityResult.
return;
}
if (width == -1) {
// Second time in, here is where the measurement takes place.
// Create a LinearLayout with a MeasureFrameLayout in it.
// Just putting a subclass of LinearLayout in works fine, but to future proof things, I do it this way.
LinearLayout linearLayout = new LinearLayout(measuringActivity = this);
LinearLayout.LayoutParams matchParent = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
MeasureFrameLayout measureFrameLayout = new MeasureFrameLayout(this);
measureFrameLayout.setLayoutParams(matchParent);
linearLayout.addView(measureFrameLayout);
this.addContentView(linearLayout, matchParent);
// measureFrameLayout will now request this second activity to finish, sending back the width.
}
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
// Probably can never happen, but just in case.
if (resultCode == RESULT_CANCELED) {
finish();
return;
}
int width = data.getIntExtra("Width", -3);
// Width is now set to the precise available width, and a layout can now be created.
...
}
class MeasureFrameLayout extends FrameLayout {
boolean finished = false;
public MeasureFrameLayout(Context context) {
super(context);
}
@SuppressLint("DrawAllocation")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (finished) {
return;
}
finished = true;
// Send the width back as the result.
Intent data = new Intent().putExtra("Width", MeasureSpec.getSize(widthMeasureSpec));
MainActivity.measuringActivity.setResult(Activity.RESULT_OK, data);
// Tell the (second) activity to finish.
MainActivity.measuringActivity.finish();
}
}
如果您不想增加WindowManagers、Points或Displays的开销,可以获取XML中最顶层View项的高度和宽度属性,前提是其高度和宽度设置为match_parent。(只要您的布局占据整个屏幕,这是正确的。)
例如,如果您的XML以以下内容开头:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/entireLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
然后findViewById(R.id.entireLayout).getWidth()将返回屏幕的宽度,findViewById(R.id.entireLayout).getHeight()将显示屏幕的高度。
这不是OP的答案,因为他希望以真实像素显示尺寸。我想要“设备无关像素”的尺寸,并从这里汇总答案https://stackoverflow.com/a/17880012/253938在这里https://stackoverflow.com/a/6656774/253938我想到了这个:
DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
int dpHeight = (int)(displayMetrics.heightPixels / displayMetrics.density + 0.5);
int dpWidth = (int)(displayMetrics.widthPixels / displayMetrics.density + 0.5);
为了访问Android设备的状态栏高度,我们更喜欢一种编程方式:
示例代码
int resId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resId > 0) {
result = getResources().getDimensionPixelSize(resId);
}
变量结果给出像素的高度。
用于快速访问
有关标题栏、导航栏和内容视图高度的更多信息,请查看Android设备屏幕大小。
对于正在搜索没有状态栏和操作栏的可用屏幕尺寸的用户(也感谢Swapnil的回答):
DisplayMetrics dm = getResources().getDisplayMetrics();
float screen_w = dm.widthPixels;
float screen_h = dm.heightPixels;
int resId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resId > 0) {
screen_h -= getResources().getDimensionPixelSize(resId);
}
TypedValue typedValue = new TypedValue();
if(getTheme().resolveAttribute(android.R.attr.actionBarSize, typedValue, true)){
screen_h -= getResources().getDimensionPixelSize(typedValue.resourceId);
}
我有一个启动屏幕活动,LinearLayout作为根视图,其宽度和高度为match_parent。这是该活动的onCreate()方法中的代码。我在应用程序的所有其他活动中使用这些度量。
int displayWidth = getRawDisplayWidthPreHoneycomb();
int rawDisplayHeight = getRawDisplayHeightPreHoneycomb();
int usableDisplayHeight = rawDisplayHeight - getStatusBarHeight();
pf.setScreenParameters(displayWidth, usableDisplayHeight);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
LinearLayout myView = (LinearLayout) findViewById(R.id.splash_view);
myView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (left == 0 && top == 0 && right == 0 && bottom == 0) {
return;
}
int displayWidth = Math.min(right, bottom);
int usableDisplayHeight = Math.max(right, bottom);
pf.setScreenParameters(displayWidth, usableDisplayHeight);
}
});
}
下面是上面调用的方法的实现:
private int getRawDisplayWidthPreHoneycomb() {
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);
int widthPixels = displayMetrics.widthPixels;
int heightPixels = displayMetrics.heightPixels;
return Math.min(widthPixels, heightPixels);
}
private int getRawDisplayHeightPreHoneycomb() {
WindowManager w = getWindowManager();
Display d = w.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
d.getMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
return Math.max(widthPixels, heightPixels);
}
public int getStatusBarHeight() {
int statusBarHeight = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
这将导致所有API版本和不同类型的设备(手机和平板电脑)的可用显示的高度和宽度,不包括任何类型的栏(状态栏、导航栏)。
遵循以下方法:
public static int getWidthScreen(Context context) {
return getDisplayMetrics(context).widthPixels;
}
public static int getHeightScreen(Context context) {
return getDisplayMetrics(context).heightPixels;
}
private static DisplayMetrics getDisplayMetrics(Context context) {
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics;
}
DisplayMetrics dimension = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dimension);
int w = dimension.widthPixels;
int h = dimension.heightPixels;
简单的功能与较低版本兼容。
/**
* @return screen size int[width, height]
*
* */
public int[] getScreenSize(){
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){
w.getDefaultDisplay().getSize(size);
return new int[]{size.x, size.y};
}else{
Display d = w.getDefaultDisplay();
//noinspection deprecation
return new int[]{d.getWidth(), d.getHeight()};
}
}
要使用:
int width = getScreenSize()[0];
int height = getScreenSize()[1];
DisplayMetrics dm = getResources().getDisplayMetrics();
float fwidth = dm.density * dm.widthPixels;
float fheight = dm.density * dm.heightPixels;
如果getSize由于您的minSDKVersion而给您带来错误,并且您不想使用不推荐的方法(getWidth&getHeight),那么getMetrics解决方案最初是由Balaji.K.于2011年发布的。Nik添加了一条注释,解释getDisplayMetrics也考虑了状态栏大小。
其他一些注释指的是乘以比例(密度),以获得尺寸的精确浮动值。在Android v2.2(API 8)和v4.0中测试,结果良好,无错误/警告。
最简单的方法:
int screenHeight = getResources().getDisplayMetrics().heightPixels;
int screenWidth = getResources().getDisplayMetrics().widthPixels;
此函数以英寸为单位返回近似屏幕大小。
public double getScreenSize()
{
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)width/(double)dens;
double hi=(double)height/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);
return screenInches;
}
如果Display类不工作,则上面的答案将不起作用,那么您可以通过下面的方法获得宽度和高度。
private static final int WIDTH_INDEX = 0;
private static final int HEIGHT_INDEX = 1;
public static int[] getScreenSize(Context context) {
int[] widthHeight = new int[2];
widthHeight[WIDTH_INDEX] = 0;
widthHeight[HEIGHT_INDEX] = 0;
try {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
widthHeight[WIDTH_INDEX] = size.x;
widthHeight[HEIGHT_INDEX] = size.y;
if (!isScreenSizeRetrieved(widthHeight))
{
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
widthHeight[0] = metrics.widthPixels;
widthHeight[1] = metrics.heightPixels;
}
// Last defense. Use deprecated API that was introduced in lower than API 13
if (!isScreenSizeRetrieved(widthHeight)) {
widthHeight[0] = display.getWidth(); // deprecated
widthHeight[1] = display.getHeight(); // deprecated
}
} catch (Exception e) {
e.printStackTrace();
}
return widthHeight;
}
private static boolean isScreenSizeRetrieved(int[] widthHeight) {
return widthHeight[WIDTH_INDEX] != 0 && widthHeight[HEIGHT_INDEX] != 0;
}
要获得屏幕尺寸,请使用显示度量
DisplayMetrics displayMetrics = new DisplayMetrics();
if (context != null)
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display defaultDisplay = windowManager.getDefaultDisplay();
defaultDisplay.getRealMetrics(displayMetrics);
}
以像素为单位获取高度和宽度
int width =displayMetrics.widthPixels;
int height =displayMetrics.heightPixels;
您可以使用以下方法获得高度大小:
getResources().getDisplayMetrics().heightPixels;
宽度大小使用
getResources().getDisplayMetrics().widthPixels;
这里是Kotlin实现中对上面一些答案的简单改编。它需要清单中如上所述的windowsSoftInput=“adjustResize”:
class KeyboardWatcher(private val layoutRooView: View) {
companion object {
private const val MIN_KEYBOARD_HEIGHT = 200f
}
private val displayMetrics: DisplayMetrics = layoutRooView.resources.displayMetrics
private var stateVisible = false
var observer: ((Boolean) -> Unit)? = null
init {
layoutRooView.viewTreeObserver.addOnGlobalLayoutListener {
val heightDiff = layoutRooView.rootView.height - layoutRooView.height
if (!stateVisible && heightDiff > dpToPx(MIN_KEYBOARD_HEIGHT)) {
stateVisible = true
observer?.invoke(stateVisible)
} else if(stateVisible) {
stateVisible = false
observer?.invoke(stateVisible)
}
}
}
private fun dpToPx(valueInDp: Float): Float {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, displayMetrics)
}
}
并使用:
val activityRootView = findViewById<ViewGroup>(R.id.activityRoot)
KeyboardWatcher(activityRootView).observer = { visible ->
if (visible) do something here ...
}
我使用了上述建议,并为我们的问题创建了一个kotlin版本。希望这为使用科特林的人提供一些额外的帮助:
private val screenDimensions: Int by lazy {
val display = (context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
Point()
.also { size ->
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 -> display.getRealSize(size)
else -> display.getSize(size)
}
}
}
screenDimensions.x // width
screenDimensions.y // height
科特林
fun getScreenHeight(activity: Activity): Int {
val metrics = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(metrics)
return metrics.heightPixels
}
fun getScreenWidth(activity: Activity): Int {
val metrics = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(metrics)
return metrics.widthPixels
}
我认为这是最简单的
private fun checkDisplayResolution() {
val displayMetrics = DisplayMetrics().also {
windowManager.defaultDisplay.getMetrics(it)
}
Log.i(TAG, "display width: ${displayMetrics.widthPixels}")
Log.i(TAG, "display height: ${displayMetrics.heightPixels}")
Log.i(TAG, "display width dpi: ${displayMetrics.xdpi}")
Log.i(TAG, "display height dpi: ${displayMetrics.ydpi}")
Log.i(TAG, "display density: ${displayMetrics.density}")
Log.i(TAG, "display scaled density: ${displayMetrics.scaledDensity}")
}
创建Kotlin扩展函数以获取屏幕宽度和高度-
fun Context?.screenWidthInPx(): Int {
if (this == null) return 0
val dm = DisplayMetrics()
val wm = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
wm.defaultDisplay.getMetrics(dm)
return dm.widthPixels
}
//comment
fun Context?.screenHeightInPx(): Int {
if (this == null) return 0
val dm = DisplayMetrics()
val wm = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
wm.defaultDisplay.getMetrics(dm)
return dm.heightPixels
}
以上代码已在API级别30中弃用。现在您可以使用以下代码
val width = windowManager.currentWindowMetrics.bounds.width()
val height = windowManager.currentWindowMetrics.bounds.height()
此方法报告包括所有系统栏区域的窗口大小,而Display#getSize(Point)报告不包括导航栏和显示剪切区域的区域。Display#getSize(Point)报告的值可通过以下方式获得:
val metrics = windowManager.currentWindowMetrics
// Gets all excluding insets
val windowInsets = metrics.windowInsets
var insets: Insets = windowInsets.getInsets(WindowInsets.Type.navigationBars())
val cutout = windowInsets.displayCutout
if (cutout != null) {
val cutoutSafeInsets = Insets.of(cutout.safeInsetLeft, cutout.safeInsetTop, cutout.safeInsetRight, cutout.safeInsetBottom)
insets = Insets.max(insets, cutoutSafeInsets)
}
val insetsWidth = insets.right + insets.left
val insetsHeight = insets.top + insets.bottom
// Legacy size that Display#getSize reports
val legacySize = Size(metrics.bounds.width() - insetsWidth, metrics.bounds.height() - insetsHeight)
来源:WindowManager#getCurrentWindowMetrics()
现在在Api 30级别上,应该这样做
final WindowMetrics metrics = windowManager.getCurrentWindowMetrics();
// Gets all excluding insets
final WindowInsets windowInsets = metrics.getWindowInsets();
Insets insets = windowInsets.getInsetsIgnoreVisibility(WindowInsets.Type.navigationBars()
| WindowInsets.Type.displayCutout());
int insetsWidth = insets.right + insets.left;
int insetsHeight = insets.top + insets.bottom;
// Legacy size that Display#getSize reports
final Rect bounds = metrics.getBounds();
final Size legacySize = new Size(bounds.width() - insetsWidth,
bounds.height() - insetsHeight);
•Kotlin版本通过扩展属性
在android中有多种实现屏幕尺寸的方法,但我认为最好的解决方案可以独立于Context实例,因此您可以在代码中的任何地方使用它。在这里,我通过kotlin扩展属性提供了一个解决方案,它可以很容易地知道以像素为单位的屏幕大小以及dp:
尺寸Utils.kt
import android.content.res.Resources
import android.graphics.Rect
import android.graphics.RectF
import android.util.DisplayMetrics
import kotlin.math.roundToInt
/**
* @author aminography
*/
private val displayMetrics: DisplayMetrics by lazy { Resources.getSystem().displayMetrics }
val screenRectPx: Rect
get() = displayMetrics.run { Rect(0, 0, widthPixels, heightPixels) }
val screenRectDp: RectF
get() = displayMetrics.run { RectF(0f, 0f, widthPixels.px2dp, heightPixels.px2dp) }
val Number.px2dp: Float
get() = this.toFloat() / displayMetrics.density
val Number.dp2px: Int
get() = (this.toFloat() * displayMetrics.density).roundToInt()
用法:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val widthPx = screenRectPx.width()
val heightPx = screenRectPx.height()
println("[PX] screen width: $widthPx , height: $heightPx")
val widthDp = screenRectDp.width()
val heightDp = screenRectDp.height()
println("[DP] screen width: $widthDp , height: $heightDp")
}
}
结果:
当设备处于纵向时:
[PX] screen width: 1440 , height: 2392
[DP] screen width: 360.0 , height: 598.0
当设备处于横向时:
[PX] screen width: 2392 , height: 1440
[DP] screen width: 598.0 , height: 360.0
如果您不是kotlin的粉丝,请使用java版本:
import android.content.res.Resources;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.DisplayMetrics;
/**
* @author aminography
*/
public class DimensionUtils {
private static DisplayMetrics displayMetrics;
private static DisplayMetrics getDisplayMetrics() {
if (displayMetrics == null) {
displayMetrics = Resources.getSystem().getDisplayMetrics();
}
return displayMetrics;
}
public static Rect screenRectPx() {
return new Rect(0, 0, getDisplayMetrics().widthPixels, getDisplayMetrics().heightPixels);
}
public static RectF screenRectDp() {
return new RectF(0f, 0f, px2dp(getDisplayMetrics().widthPixels), px2dp(getDisplayMetrics().heightPixels));
}
public static float px2dp(int value) {
return value / getDisplayMetrics().density;
}
public static int dp2px(float value) {
return (int) (value * getDisplayMetrics().density);
}
}
科特林牌手表
fun getScreenHeight(activity: Activity): Int {
val metrics = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(metrics)
return metrics.heightPixels
}
fun getScreenWidth(activity: Activity): Int {
val metrics = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(metrics)
return metrics.widthPixels
}
JAVA
DisplayMetrics dimension = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dimension);
int w = dimension.widthPixels;
int h = dimension.heightPixels;
以下是低于/高于API 30代码的Kotlin扩展函数:
fun Activity.getScreenWidth(): Int {
return if (Build.VERSION.SDK_INT < 30) {
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
displayMetrics.widthPixels
} else {
val metrics = windowManager.currentWindowMetrics
val insets = metrics.windowInsets
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
metrics.bounds.width() - insets.left - insets.right
}
}
fun Activity.getScreenHeight(): Int {
return if (Build.VERSION.SDK_INT < 30) {
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
displayMetrics.heightPixels
} else {
val metrics = windowManager.currentWindowMetrics
val insets = metrics.windowInsets
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
metrics.bounds.height() - insets.top - insets.bottom
}
}
对应的Java助手方法:
public int getScreenWidth(Activity activity) {
if (Build.VERSION.SDK_INT < 30) {
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.widthPixels;
} else {
WindowMetrics metrics = activity.getWindowManager().getCurrentWindowMetrics();
Insets insets = metrics.getWindowInsets()
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
return metrics.getBounds().width() - insets.left - insets.right;
}
}
public int getScreenHeight(Activity activity) {
if (Build.VERSION.SDK_INT < 30) {
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
} else {
WindowMetrics metrics = activity.getWindowManager().getCurrentWindowMetrics();
Insets insets = metrics.getWindowInsets()
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
return metrics.getBounds().height() - insets.bottom - insets.top;
}
}
创建一个类和方法,如下所示:
public MyPoint getScreenDimensionsAsPixel(Context context){
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
return new MyPoint(point.x, point.y);
}
public class MyPoint{
private int width;
private int height;
public MyPoint(int width, int height) {
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
然后在代码中使用它们:
MyPoint myPoint = getScreenDimensionsAsPixel(MainActivity.this);
Toast.makeText(MainActivity.this, "width: " + String.valueOf(myPoint.getWidth()) + "|" +
"height: " + String.valueOf(myPoint.getHeight()), Toast.LENGTH_LONG).show();
在Kotlin要简单得多。
val displayMetrics = Resources.getSystem().displayMetrics
displayMetrics.heightPixels
displayMetrics.widthPixels