【摘录】横竖屏切换

http://www.cnblogs.com/hll2008/archive/2011/01/03/1924952.html

为了实现横竖屏切换显示,背景图的显示采用代码进行控制显示,首先用如下方法获取当前手机是横屏还是竖屏:

代码
// 获取屏幕方向
public   static   int  ScreenOrient(Activity activity)
    {
        
int  orient  =  activity.getRequestedOrientation(); 
        
if (orient  !=  ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE  &&  orient  !=  ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            
// 宽>高为横屏,反正为竖屏  
             WindowManager windowManager  =  activity.getWindowManager();  
             Display display 
=  windowManager.getDefaultDisplay();  
             
int  screenWidth   =  display.getWidth();  
             
int  screenHeight  =  display.getHeight();  
             orient 
=  screenWidth  <  screenHeight  ?  ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        }
        
return  orient;
    }

 

然后编写一个名为AutoBackground的公共方法用来实现屏幕背景的自动切换,后面的几乎每一个功能页面都需要用到这个方法:

代码
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(Background_v);
        }
else // 横向
            view.setBackgroundResource(Background_h);
        }  
    }

 

完成上述两方法后在 MainActivity的onCreate方法中调用AutoBackground方法进行屏幕自动切换:

LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
// 背景自动适应
AndroidHelper.AutoBackground( this , layout, R.drawable.bg_v, R.drawable.bg_h);

 

到此完成了载入页面的UI部分的实现,测试运行模拟器中查看效果,基本上跟最上面的设计效果图相符,测试效果图如下:

 【摘录】横竖屏切换【摘录】横竖屏切换

你可能感兴趣的:(切换)