Android手機分辨率測試程序可以測試出個人手機設備的分辨率,屬於哪種dpi級別,以便開發參考。
main.xml
- <?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
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, AndroidDPI!</string>
- <string name="app_name">AndroidDPI</string>
- <string name="screenSize">輸入該設備的屏幕大小:</string>
- <string name="submit">確認</string>
- </resources>
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("請輸入該屏幕的尺寸");
- }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("手機屏幕分辨率為:"+width+"*"+height);
- double dpi = Math.sqrt(height*height +width*width)/sizeInt;
- String dpiType = "";
- if(dpi >320){
- dpiType = "屬於xhdpi";
- }else if(dpi>240){
- dpiType = "屬於hdpi";
- }else if(dpi>160){
- dpiType = "屬於mdpi";
- }else if(dpi>120){
- dpiType = "屬於ldpi";
- }
- showDPI.setText("手機DPI是:"+ dpi+ ","+ dpiType);
- }
- }
- });
- }
- }
顯示結果:
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11