从上面的答案,我将它们结合在一起,创造了以下功能:
public static String getDeviceDensity(Context context){
String deviceDensity = "";
switch (context.getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
deviceDensity = 0.75 + " ldpi";
break;
case DisplayMetrics.DENSITY_MEDIUM:
deviceDensity = 1.0 + " mdpi";
break;
case DisplayMetrics.DENSITY_HIGH:
deviceDensity = 1.5 + " hdpi";
break;
case DisplayMetrics.DENSITY_XHIGH:
deviceDensity = 2.0 + " xhdpi";
break;
case DisplayMetrics.DENSITY_XXHIGH:
deviceDensity = 3.0 + " xxhdpi";
break;
case DisplayMetrics.DENSITY_XXXHIGH:
deviceDensity = 4.0 + " xxxhdpi";
break;
default:
deviceDensity = "Not found";
}
return deviceDensity;
}
现在,该设备要获得密度信息和文件夹将使用它,只需添加上述方法该活动,并添加以下行的onCreate在描述
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Log.d("Screen Density: ", Helper.getDeviceDensity(this));
}