Android实现自动换屏


Android实现自动换屏:

  1. /** 
  2.      * 获取屏幕的方向 
  3.      * @param activity 
  4.      * @return 
  5.      */  
  6.     public static int screenOrient(Activity activity)  
  7.     {  
  8.         int orientation = activity.getRequestedOrientation();  
  9.         if(orientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE   
  10.                 && orientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)  
  11.         {  
  12.             //宽>高为横屏,反则为竖屏    
  13.             WindowManager manager = activity.getWindowManager();  
  14.             Display display = manager.getDefaultDisplay();  
  15.             int height = display.getHeight();  
  16.             int width = display.getWidth();  
  17.             orientation = width > height?ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;  
  18.         }  
  19.         return orientation;  
  20.     }   

 

  1. /** 
  2.      * 自动换屏的方法 
  3.      * @param activity   
  4.      * @param view 
  5.      * @param Background_v 
  6.      * @param Background_h 
  7.      */  
  8.     public static void autoBackground(Activity activity,View view,int Background_v, int Background_h)  
  9.     {  
  10.         int orient = screenOrient(activity);  
  11.         if (orient == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)  
  12.         {  
  13.             //纵向   
  14.             view.setBackgroundResource(R.drawable.bg);  
  15.         }else {  
  16.             //横向   
  17.             view.setBackgroundResource(R.drawable.bg);  
  18.         }  
  19.     }  
 
  1. public void onCreate(Bundle savedInstanceState) {  
  2.        super.onCreate(savedInstanceState);  
  3.        requestWindowFeature(Window.FEATURE_NO_TITLE);  
  4.        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  5.        setContentView(R.layout.main);  
  6.          
  7.        LinearLayout layout = (LinearLayout)findViewById(R.id.layout);  
  8.        autoBackground(this, layout, R.drawable.bg, R.drawable.bg);  
  9.    }  

相关内容