This program can test the resolution of a personal mobile phone device, which dpi level, for development reference.
Main. xml
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/screenSize"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/screenSize"
/>
<EditText
Android: id = "@ + id/size"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: inputType = "numberDecimal"
/>
<Button
Android: id = "@ + id/submit"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/submit"
/>
<EditText
Android: id = "@ + id/showWidth"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: inputType = "text"
Android: editable = "false"
/>
<EditText
Android: id = "@ + id/showDPI"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: inputType = "text"
Android: editable = "false"
/>
</LinearLayout>
String. xml
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, AndroidDPI! </String>
<String name = "app_name"> AndroidDPI </string>
<String name = "screenSize"> enter the screen size of the device: </string>
<String name = "submit"> OK </string>
</Resources>
Java code:
[Java]
Package com. shine. android;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. DisplayMetrics;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Public class AndroidDPI extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Final EditText size = (EditText) findViewById (R. id. size );
Final EditText showWidth = (EditText) findViewById (R. id. showWidth );
Final EditText showDPI = (EditText) findViewById (R. id. showDPI );
Button submit = (Button) findViewById (R. id. submit );
Submit. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
If (size. length () = 0 ){
Size. setText ("Enter the size of the screen ");
} Else {
DisplayMetrics dm = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (dm );
Int width = dm. widthPixels;
Int height = dm. heightPixels;
Float sizeInt = Float. parseFloat (size. getText (). toString ());
ShowWidth. setText ("cell phone screen resolution:" + width + "*" + height );
Double dpi = Math. sqrt (height * height + width * width)/sizeInt;
String dpiType = "";
If (dpi & gt; 320 ){
DpiType = "xhdpi ";
} Else if (dpi> 240 ){
DpiType = "hdpi ";
} Else if (dpi> 160 ){
DpiType = "belonging to mdpi ";
} Else if (dpi> 120 ){
DpiType = "ldpi ";
}
ShowDPI. setText ("cell phone DPI:" + dpi + "," + dpiType );
}
}
});
}
}
Display result:
From Jason's Java Column