Android将屏幕尺寸定义为Normal Large XLarge等。
它会自动在适当文件夹中的静态资源之间进行选择。我的Java代码中需要有关当前设备的数据。DisplayMetrics仅提供有关当前设备密度的信息。屏幕大小无可用。
我确实在这里的grep代码中找到了ScreenSize枚举, 但是对于4.0 SDK来说,这似乎不可用。有没有办法获取此信息?
Answers:
将此代码复制并粘贴到您的代码中Activity
,执行后将Toast
显示设备的屏幕尺寸类别。
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
String toastMsg;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
toastMsg = "Large screen";
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
toastMsg = "Normal screen";
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
toastMsg = "Small screen";
break;
default:
toastMsg = "Screen size is neither large, normal or small";
}
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
private static String getScreenResolution(Context context)
{
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
return "{" + width + "," + height + "}";
}
确定屏幕尺寸:
int screenSize = getResources().getConfiguration().screenLayout &Configuration.SCREENLAYOUT_SIZE_MASK;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
Toast.makeText(this, "Normal screen",Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
Toast.makeText(this, "Small screen",Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
}
确定密度:
int density= getResources().getDisplayMetrics().densityDpi;
switch(density)
{
case DisplayMetrics.DENSITY_LOW:
Toast.makeText(context, "LDPI", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_MEDIUM:
Toast.makeText(context, "MDPI", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_HIGH:
Toast.makeText(context, "HDPI", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_XHIGH:
Toast.makeText(context, "XHDPI", Toast.LENGTH_SHORT).show();
break;
}
供参考: http //devl-android.blogspot.in/2013/10/wifi-connectivity-and-hotspot-in-android.html
我认为这是非常简单的代码!
public Map<String, Integer> deriveMetrics(Activity activity) {
try {
DisplayMetrics metrics = new DisplayMetrics();
if (activity != null) {
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
}
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("screenWidth", Integer.valueOf(metrics.widthPixels));
map.put("screenHeight", Integer.valueOf(metrics.heightPixels));
map.put("screenDensity", Integer.valueOf(metrics.densityDpi));
return map;
} catch (Exception err) {
; // just use zero values
return null;
}
}
现在,该方法可以在任何地方独立使用。无论您要在何处获取有关设备屏幕的信息,请按照以下步骤操作:
Map<String, Integer> map = deriveMetrics2(this);
map.get("screenWidth");
map.get("screenHeight");
map.get("screenDensity");
希望这对外面的人可能有所帮助,并且会发现它更易于使用。如果我需要重新更正或改进,请随时让我知道!:-)
干杯!!!
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
您可以使用此代码获得以像素为单位的显示尺寸。
Display display = getWindowManager().getDefaultDisplay();
SizeUtils.SCREEN_WIDTH = display.getWidth();
SizeUtils.SCREEN_HEIGHT = display.getHeight();
getHeight
并getWidth
从API 16开始弃用:Android 4.1(Jelly Bean)
获取DisplayMetrics的方法:
1。
val dm = activity.resources.displayMetrics
val dm = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(dm)
然后得到
屏幕密度表示为每英寸点数。可以是DENSITY_LOW,DENSITY_MEDIUM或DENSITY_HIGH
dm.densityDpi
可用显示尺寸的绝对高度(以像素为单位)。
dm.heightPixels
可用显示尺寸的绝对宽度(以像素为单位)。
dm.widthPixels
X尺寸屏幕每英寸的确切物理像素。
dm.xdpi
屏幕上每英寸在Y维度上的确切物理像素。
dm.ydpi
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int ht = displaymetrics.heightPixels;
int wt = displaymetrics.widthPixels;
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG)
.show();
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(this,
"Screen size is neither large, normal or small",
Toast.LENGTH_LONG).show();
}
// Determine density
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi;
if (density == DisplayMetrics.DENSITY_HIGH) {
Toast.makeText(this,
"DENSITY_HIGH... Density is " + String.valueOf(density),
Toast.LENGTH_LONG).show();
} else if (density == DisplayMetrics.DENSITY_MEDIUM) {
Toast.makeText(this,
"DENSITY_MEDIUM... Density is " + String.valueOf(density),
Toast.LENGTH_LONG).show();
} else if (density == DisplayMetrics.DENSITY_LOW) {
Toast.makeText(this,
"DENSITY_LOW... Density is " + String.valueOf(density),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(
this,
"Density is neither HIGH, MEDIUM OR LOW. Density is "
+ String.valueOf(density), Toast.LENGTH_LONG)
.show();
}
// These are deprecated
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
带有装饰物(包括按钮栏):
private static String getScreenResolution(Context context) {
DisplayMetrics metrics = new DisplayMetrics();
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRealMetrics(metrics);
return "{" + metrics.widthPixels + "," + metrics.heightPixels + "}";
}
没有装饰:
private static String getScreenResolution(Context context) {
DisplayMetrics metrics = new DisplayMetrics();
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
return "{" + metrics.widthPixels + "," + metrics.heightPixels + "}";
}
区别在于Display类的方法getMetrics()与getRealMetrics()。
我的几个应用程序都需要这个,下面的代码是我解决问题的方法。仅显示onCreate中的代码。这是一个独立的应用程序,可在任何设备上运行以返回屏幕信息。
setContentView(R.layout.activity_main);
txSize = (TextView) findViewById(R.id.tvSize);
density = (TextView) findViewById(R.id.density);
densityDpi = (TextView) findViewById(R.id.densityDpi);
widthPixels = (TextView) findViewById(R.id.widthPixels);
xdpi = (TextView) findViewById(R.id.xdpi);
ydpi = (TextView) findViewById(R.id.ydpi);
Configuration config = getResources().getConfiguration();
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();
txSize.setText("Large screen");
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG)
.show();
txSize.setText("Normal sized screen");
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG)
.show();
txSize.setText("Small sized screen");
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
Toast.makeText(this, "xLarge sized screen", Toast.LENGTH_LONG)
.show();
txSize.setText("Small sized screen");
} else {
Toast.makeText(this,
"Screen size is neither large, normal or small",
Toast.LENGTH_LONG).show();
txSize.setText("Screen size is neither large, normal or small");
}
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
Log.i(TAG, "density :" + metrics.density);
density.setText("density :" + metrics.density);
Log.i(TAG, "D density :" + metrics.densityDpi);
densityDpi.setText("densityDpi :" + metrics.densityDpi);
Log.i(TAG, "width pix :" + metrics.widthPixels);
widthPixels.setText("widthPixels :" + metrics.widthPixels);
Log.i(TAG, "xdpi :" + metrics.xdpi);
xdpi.setText("xdpi :" + metrics.xdpi);
Log.i(TAG, "ydpi :" + metrics.ydpi);
ydpi.setText("ydpi :" + metrics.ydpi);
还有一个简单的XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tvSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/density"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/densityDpi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/widthPixels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/xdpi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/ydpi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
如果你是在IE碎片,适配器,型号类或任何其他Java类,做一个非活动不扩展Activity
根本getResources()
行不通。您可以getActivity()
在片段中使用context
,也可以将其传递给相应的类。
mContext.getResources()
我建议让一个类说Utils的类将具有用于常规工作的方法。这样做的好处是,您可以在应用程序中调用此方法的任何位置使用单行代码即可获得所需的结果。
以下算法可用于识别将在静态资源之间进行选择的类别,并且还可满足较新的XX和XXX高密度
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
mDensityDpi = displayMetrics.densityDpi;
mDensity = displayMetrics.density;
mDisplayWidth = displayMetrics.widthPixels;
mDisplayHeight = displayMetrics.heightPixels;
String densityStr = "Unknown";
int difference, leastDifference = 9999;
difference = Math.abs(mDensityDpi - DisplayMetrics.DENSITY_LOW);
if (difference < leastDifference) {
leastDifference = difference;
densityStr = "LOW";
}
difference = Math.abs(mDensityDpi - DisplayMetrics.DENSITY_MEDIUM);
if (difference < leastDifference) {
leastDifference = difference;
densityStr = "MEDIUM";
}
difference = Math.abs(mDensityDpi - DisplayMetrics.DENSITY_HIGH);
if (difference < leastDifference) {
leastDifference = difference;
densityStr = "HIGH";
}
difference = Math.abs(mDensityDpi - DisplayMetrics.DENSITY_XHIGH);
if (difference < leastDifference) {
leastDifference = difference;
densityStr = "XHIGH";
}
difference = Math.abs(mDensityDpi - DisplayMetrics.DENSITY_XXHIGH);
if (difference < leastDifference) {
leastDifference = difference;
densityStr = "XXHIGH";
}
difference = Math.abs(mDensityDpi - DisplayMetrics.DENSITY_XXXHIGH);
if (difference < leastDifference) {
densityStr = "XXXHIGH";
}
Log.i(TAG, String.format("Display [h,w]: [%s,%s] Density: %s Density DPI: %s [%s]", mDisplayHeight, mDisplayWidth, mDensity, mDensityDpi, densityStr));
在Kotlin中获取屏幕分辨率或屏幕尺寸
fun getScreenResolution(context: Context): Pair<Int, Int> {
try {
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display = wm.defaultDisplay
val metrics = DisplayMetrics()
display.getMetrics(metrics)
val width = metrics.widthPixels
val height = metrics.heightPixels
//Log.d(AppData.TAG, "screenSize: $width, $height")
return Pair(width, height)
} catch (error: Exception) {
Log.d(AppData.TAG, "Error : autoCreateTable()", error)
}
return Pair(0, 0)
}