ipad屏幕适配

ipad屏幕适配
1.android:screenOrientation="unspecified|portrait|landscape"

2.adb shell getprop ro.sf.lcd_density //dpi=400
sw-xxxx-dp中的xxxx的计算公式是  sw *160/dpi
屏幕分辨率1920X1080
xxxx=1080*160/400=432

2、android:name=".ui.main.MainActivity"
            android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|smallestScreenSize|layoutDirection|screenLayout"
            android:hardwareAccelerated="true"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="unspecified"
            android:taskAffinity="com.huawei.vassistant"
            android:windowSoftInputMode="adjustUnspecified|stateAlwaysHidden">
3、android:name=".ui.translation.TranslationActivity"
            android:configChanges="keyboardHidden|orientation|screenSize|smallestScreenSize|layoutDirection|screenLayout"
            android:hardwareAccelerated="true"
            android:label="@string/content_description_trans_activity"
            android:launchMode="singleTask"
            android:screenOrientation="unspecified"
            android:windowSoftInputMode="adjustPan">
            
一般在AndroidManifest.xml文件中都没有使用到android:configChanges="keyboardHidden|orientation"配置,当然还是很有用的。
就是如果配置了这个属性,当我们横竖屏切换的时候会直接调用onCreate方法中的onConfigurationChanged方法,而不会重新执行onCreate方法,那当然如果不配置这个属性的话就会重新调用onCreate方法了。http://www.cnblogs.com/carlo/p/4311010.html            
   
5、private void startOrientationListener() {//设置屏幕旋转监听
        mOrientationEventListener = new OrientationEventListener(this) {
            @Override
            public void onOrientationChanged(int i) {
                if (i < 0) return;
                if (!isRotationOn()) {
                    if (isTablet()) {
                        int currentSystemLockedOrientation = getSystemLockedScreenOrientation();
                        if (currentSystemLockedOrientation % 2 == 0) {
                            setOrientationChanged(ORIENTATION_PORTRAIT);
                        } else {
                            setOrientationChanged(ORIENTATION_LANDSCAPE);
                        }
                    }
                    return;
                }

                int orientation;
                if ((70 <= i && i <= 110) || (250 <= i && i <= 290)) {
                    orientation = ORIENTATION_LANDSCAPE;
                } else {
                    orientation = ORIENTATION_PORTRAIT;
                }
                VALog.i(TAG, "angle = " + i + " currentorientation = " +mCurrentOrientation+ " noworientation = " + orientation);
                if (mHandler.hasMessages(HANDLER_MSG_SET_REQUESTED_ORIENTATION)) {
                    mHandler.removeMessages(HANDLER_MSG_SET_REQUESTED_ORIENTATION);
                }
                Message msg = mHandler.obtainMessage(HANDLER_MSG_SET_REQUESTED_ORIENTATION, orientation);
                mHandler.sendMessageDelayed(msg, 200);
            }
        };
        mOrientationEventListener.enable();
    }
    
    public boolean isRotationOn() {//判断是否旋转
        int status = 0;
        try {
            status = android.provider.Settings.System.getInt(
                    getContentResolver(),
                    android.provider.Settings.System.ACCELEROMETER_ROTATION);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }

        if (status == 0) {
            return false;
        } else {
            return true;
        }

    }
    
     public boolean isTablet() {//判断是不是平板
        return (getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }
    
    public int getSystemLockedScreenOrientation(){//判断锁屏方向
        int status = 0;
        try {
            status = android.provider.Settings.System.getInt(
                    getContentResolver(),
                    android.provider.Settings.System.USER_ROTATION);
        } catch (Settings.SettingNotFoundException e) {
            VALog.e(TAG, "getSystemLockedScreenOrientation" + e.getMessage());
        }
        return status;
    }
    
6、代码设置控件Margin
if (!isTablet()) {
            VAUtils.setMarginRightPercentOfScreen(VAssistantConfig.getAppContext(), mBtnTranslation, 0.16);
            VAUtils.setMarginRightPercentOfScreen(VAssistantConfig.getAppContext(), mBtnHelp, 0.16);
        } else {
            VAUtils.setMarginRightPercentOfScreen(VAssistantConfig.getAppContext(), mBtnTranslation, 0.05);
            VAUtils.setMarginRightPercentOfScreen(VAssistantConfig.getAppContext(), mBtnHelp, 0.05);
        }
    }

7、public class VAOrientationControlSetting {//控制界面方向工具类

    private static int mOritentation;

    public static void setOritentation(int mOritentation) {
        VAOrientationControlSetting.mOritentation = mOritentation;
    }

    public static void setOritention(Activity mActivity) {
        if (VAUtils.isTablet()) {
            if (mOritentation == Configuration.ORIENTATION_LANDSCAPE) {
                mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE);//设置界面为横屏mOritentation==activity.getResources().getConfiguration().orientation;
            } else {
                mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT);//设置界面为竖屏
            }
        } else {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT);
        }
    }
}

 

posted @ 2017-10-17 20:04  代码の足迹  阅读(1467)  评论(0编辑  收藏  举报