歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android實現自動換屏

Android實現自動換屏

日期:2017/3/1 10:55:25   编辑:Linux編程

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. LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
  7. autoBackground(this, layout, R.drawable.bg, R.drawable.bg);
  8. }
Copyright © Linux教程網 All Rights Reserved