android中判断横屏或者竖屏并改变背景

在android中,判断横屏还是竖屏,并且根据方向改变背景,代码如下: 

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;
    }

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); 
        }  
    } 



其中Background_v是纵向时的背景图,view.setBackgroundResource为横向时的背景图 

然后在activity的oncreate 方法 中去调用 
LinearLayout layout=(LinearLayout)findViewById(R.id.layout); 
//背景自动适应 
androidUtil.AutoBackground(this, layout, R.drawable.bg_v, R.drawable.bg_h);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值