歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android手機分辨率測試程序

Android手機分辨率測試程序

日期:2017/3/1 10:21:07   编辑:Linux編程

Android手機分辨率測試程序可以測試出個人手機設備的分辨率,屬於哪種dpi級別,以便開發參考。

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:id="@+id/screenSize"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/screenSize"
  11. />
  12. <EditText
  13. android:id="@+id/size"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:inputType="numberDecimal"
  17. />
  18. <Button
  19. android:id="@+id/submit"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:text="@string/submit"
  23. />
  24. <EditText
  25. android:id="@+id/showWidth"
  26. android:layout_width="fill_parent"
  27. android:layout_height="wrap_content"
  28. android:inputType="text"
  29. android:editable="false"
  30. />
  31. <EditText
  32. android:id="@+id/showDPI"
  33. android:layout_width="fill_parent"
  34. android:layout_height="wrap_content"
  35. android:inputType="text"
  36. android:editable="false"
  37. />
  38. </LinearLayout>
string.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">Hello World, AndroidDPI!</string>
  4. <string name="app_name">AndroidDPI</string>
  5. <string name="screenSize">輸入該設備的屏幕大小:</string>
  6. <string name="submit">確認</string>
  7. </resources>
Java代碼:
  1. package com.shine.android;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.DisplayMetrics;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. public class AndroidDPI extends Activity {
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. final EditText size = (EditText) findViewById(R.id.size);
  15. final EditText showWidth = (EditText) findViewById(R.id.showWidth);
  16. final EditText showDPI = (EditText) findViewById(R.id.showDPI);
  17. Button submit = (Button) findViewById(R.id.submit);
  18. submit.setOnClickListener(new OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. if(size.length() == 0){
  22. size.setText("請輸入該屏幕的尺寸");
  23. }else {
  24. DisplayMetrics dm = new DisplayMetrics();
  25. getWindowManager().getDefaultDisplay().getMetrics(dm);
  26. int width = dm.widthPixels;
  27. int height =dm.heightPixels;
  28. float sizeInt = Float.parseFloat(size.getText().toString());
  29. showWidth.setText("手機屏幕分辨率為:"+width+"*"+height);
  30. double dpi = Math.sqrt(height*height +width*width)/sizeInt;
  31. String dpiType = "";
  32. if(dpi >320){
  33. dpiType = "屬於xhdpi";
  34. }else if(dpi>240){
  35. dpiType = "屬於hdpi";
  36. }else if(dpi>160){
  37. dpiType = "屬於mdpi";
  38. }else if(dpi>120){
  39. dpiType = "屬於ldpi";
  40. }
  41. showDPI.setText("手機DPI是:"+ dpi+ ","+ dpiType);
  42. }
  43. }
  44. });
  45. }
  46. }
顯示結果:


更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved