1. 01./* First, get the Display from the WindowManager */   
  2. 02.Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();   
  3. 03.   
  4. 04./* Now we can retrieve all display-related infos */   
  5. 05.int width = display.getWidth();   
  6. 06.int height = display.getHeight();   
  7. 07.int orientation = display.getOrientation();   
  8. 08.或者   
  9. 09.public int getScreenOrientation()   
  10. 10.{   
  11. 11.    Display getOrient = getWindowManager().getDefaultDisplay();   
  12. 12.   
  13. 13.    int orientation = getOrient.getOrientation();   
  14. 14.   
  15. 15.    // Sometimes you may get undefined orientation Value is 0   
  16. 16.    // simple logic solves the problem compare the screen   
  17. 17.    // X,Y Co-ordinates and determine the Orientation in such cases   
  18. 18.    if(orientation==Configuration.ORIENTATION_UNDEFINED){   
  19. 19.   
  20. 20.        Configuration config = getResources().getConfiguration();   
  21. 21.        orientation = config.orientation;   
  22. 22.   
  23. 23.        if(orientation==Configuration.ORIENTATION_UNDEFINED){   
  24. 24.            //if height and widht of screen are equal then   
  25. 25.            // it is square orientation   
  26. 26.            if(getOrient.getWidth()==getOrient.getHeight()){   
  27. 27.                orientation = Configuration.ORIENTATION_SQUARE;   
  28. 28.            }else{ //if widht is less than height than it is portrait   
  29. 29.                if(getOrient.getWidth() < getOrient.getHeight()){   
  30. 30.                    orientation = Configuration.ORIENTATION_PORTRAIT;   
  31. 31.                }else{ // if it is not any of the above it will defineitly be landscape   
  32. 32.                    orientation = Configuration.ORIENTATION_LANDSCAPE;   
  33. 33.                }   
  34. 34.            }   
  35. 35.        }   
  36. 36.    }   
  37. 37.    return orientation; // return value 1 is portrait and 2 is Landscape Mode   
  38. 38.}   
  39. /* First, get the Display from the WindowManager */
  40. Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
  41. /* Now we can retrieve all display-related infos */
  42. int width = display.getWidth();
  43. int height = display.getHeight();
  44. int orientation = display.getOrientation();
  45. 或者
  46. public int getScreenOrientation()
  47. {
  48.     Display getOrient = getWindowManager().getDefaultDisplay();
  49.     int orientation = getOrient.getOrientation();
  50.     // Sometimes you may get undefined orientation Value is 0
  51.     // simple logic solves the problem compare the screen
  52.     // X,Y Co-ordinates and determine the Orientation in such cases
  53.     if(orientation==Configuration.ORIENTATION_UNDEFINED){
  54.         Configuration config = getResources().getConfiguration();
  55.         orientation = config.orientation;
  56.         if(orientation==Configuration.ORIENTATION_UNDEFINED){
  57.             //if height and widht of screen are equal then
  58.             // it is square orientation
  59.             if(getOrient.getWidth()==getOrient.getHeight()){
  60.                 orientation = Configuration.ORIENTATION_SQUARE;
  61.             }else{ //if widht is less than height than it is portrait
  62.                 if(getOrient.getWidth() < getOrient.getHeight()){
  63.                     orientation = Configuration.ORIENTATION_PORTRAIT;
  64.                 }else{ // if it is not any of the above it will defineitly be landscape
  65.                     orientation = Configuration.ORIENTATION_LANDSCAPE;
  66.                 }
  67.             }
  68.         }
  69.     }
  70.     return orientation; // return value 1 is portrait and 2 is Landscape Mode
  71. }
posted on 2011-08-29 18:37  情定诺坎普  阅读(340)  评论(0编辑  收藏  举报