Android实现自动换屏
Android实现自动换屏
Android实现自动换屏:
- /**
- * 获取屏幕的方向
- * @param activity
- * @return
- */
- public static int screenOrient(Activity activity)
- {
- int orientation = activity.getRequestedOrientation();
- if(orientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
- && orientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
- {
- //宽>高为横屏,反则为竖屏
- WindowManager manager = activity.getWindowManager();
- Display display = manager.getDefaultDisplay();
- int height = display.getHeight();
- int width = display.getWidth();
- orientation = width > height?ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
- }
- return orientation;
- }
- /**
- * 自动换屏的方法
- * @param activity
- * @param view
- * @param Background_v
- * @param Background_h
- */
- public static void autoBackground(Activity activity,View view,int Background_v, int Background_h)
- {
- int orient = screenOrient(activity);
- if (orient == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
- {
- //纵向
- view.setBackgroundResource(R.drawable.bg);
- }else {
- //横向
- view.setBackgroundResource(R.drawable.bg);
- }
- }
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.main);
- LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
- autoBackground(this, layout, R.drawable.bg, R.drawable.bg);
- }
评论暂时关闭