Android11.0在设置-显示中添加屏幕旋转菜单

Android11.0在设置-显示中添加屏幕旋转菜单

效果如下

在这里插入图片描述在这里插入图片描述

添加菜单界面资源文件

1. 添加旋转选项的名称及说明,修改packages/apps/Settings/values/strings.xml

<string name="screen_rotate_title">Screen rotate</string>
<string name="screen_rotate_summary">Control screen orientation</string>

2. 添加选择菜单选项,修改packages/apps/Settings/res/values/arrays.xml

“screen_rotate_entries” 对应选项名称,“screen_rotate_values” 对应选项的值;translatable=“false” 表示不支持翻译,即不必再对 “screen_rotate_values” 配置中文格式

    <string-array name="screen_rotate_entries">
        <item>0</item>
        <item>90</item>
        <item>180</item>
        <item>270</item>
    </string-array>

    <string-array name="screen_rotate_values" translatable="false">
        <item>0</item>
        <item>90</item>
        <item>180</item>
        <item>270</item>
    </string-array>

packages/apps/Settings/res/values-zh-rCN/strings.xml

    <string name="screen_rotate_title">屏幕旋转</string>
    <string name="screen_rotate_summary">控制屏幕方向</string>

packages/apps/Settings/res/values-zh-rCN/arrays.xml

  <string-array name="screen_rotate_entries">
    <item>0</item>
    <item>90</item>
    <item>180</item>
    <item>270</item>
  </string-array>

添加菜单界面

1.添加菜单界面,修改packages/apps/Settings/res/xml/display_settings.xml

    <SwitchPreference
        android:key="auto_rotate"
        android:title="@string/accelerometer_title"
        settings:keywords="@string/keywords_auto_rotate" settings:controller="com.android.settings.display.AutoRotatePreferenceController" />

   <--新添加的-->
    <ListPreference
        android:key="screen_rotate"
        android:title="@string/screen_rotate_title"
        android:summary="@string/screen_rotate_summary"
        android:persistent="false"
        android:entries="@array/screen_rotate_entries"
        android:entryValues="@array/screen_rotate_values"
        />

    <Preference
        android:key="color_mode"
        android:title="@string/color_mode_title"
        android:fragment="com.android.settings.display.ColorModePreferenceFragment"
        settings:controller="com.android.settings.display.ColorModePreferenceController"
        settings:keywords="@string/keywords_color_mode" />

2. 添加ScreenRotationPreferenceController.java packager/apps/Settings/src/com/android/settings/display/ScreenRotationPreferenceController.java

package com.android.settings.display;

import android.content.Context;

import android.provider.Settings;
import android.view.Surface;

import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;

import androidx.preference.ListPreference;
import androidx.preference.Preference;

public class ScreenRotationPreferenceController extends AbstractPreferenceController implements
        PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
    
    
    private static final String TAG = "ScreenRotationPreferenceController";

    private final String mScreenRotationKey;

    public ScreenRotationPreferenceController(Context context, String key) {
    
    
        super(context);
        mScreenRotationKey = key;
        Settings.System.putInt(mContext.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
    }

    @Override
    public boolean isAvailable() {
    
    
        return true;
    }

    @Override
    public String getPreferenceKey() {
    
    
        return mScreenRotationKey;
    }

    @Override
    public void updateState(Preference preference) {
    
    
        ListPreference pref = (ListPreference) preference;
        int index = Settings.System.getInt(mContext.getContentResolver(), Settings.System.USER_ROTATION, 0);
        pref.setValueIndex(index);
    }


    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
    
    
        setScreenRotation(preference, (String) newValue);
        return true;
    }

    private void setScreenRotation(Preference preference, String value) {
    
    
        int rotation = Surface.ROTATION_0;
        switch (value) {
    
    
            case "0":
                rotation = Surface.ROTATION_0;
                break;
            case "90":
                rotation = Surface.ROTATION_90;
                break;
            case "180":
                rotation = Surface.ROTATION_180;
                break;
            case "270":
                rotation = Surface.ROTATION_270;
                break;
        }
        Settings.System.putInt(mContext.getContentResolver(), Settings.System.USER_ROTATION, rotation);
        preference.setSummary(value);
    }

}

3. 注册ScreenRotationPreferenceController packager/apps/Settings/src/com/android/settings/DisplaySettings.java


  private static final String KEY_SCREEN_ROTATE = "screen_rotate";
  
  private static List<AbstractPreferenceController> buildPreferenceControllers(
            Context context, Lifecycle lifecycle) {
    
    
        final List<AbstractPreferenceController> controllers = new ArrayList<>();
        ......省略代码......
        
        //新增
        controllers.add(new ScreenRotationPreferenceController(context, KEY_SCREEN_ROTATE));
        
        ......省略代码......
        return controllers;
    }

至此,设置-显示中已经出现屏幕旋转菜单,并可以正常旋转界面

解决相关问题

1.导航栏在横屏的时候是固定在右侧的,而不是旋转到底部 修改 frameworks/base/core/res/res/values/config.xml

修改前为true
<bool name="config_navBarCanMove">true</bool>
修改后
<bool name="config_navBarCanMove">false</bool>

2. 设置屏幕旋转后,回到桌面,桌面没有旋转 修改 packages/apps/Launcher3/res/values/config.xml

//修改前
<bool name="allow_rotation">false</bool>
//修改后
<bool name="allow_rotation">true</bool>

修改RotationHelper packages/apps/Launcher3/src/com/android/launcher3/states/RotationHelper.java

public static boolean getAllowRotationDefaultValue(Context context) {
    
    
        //新添加
        //默认支持旋转,这里直接return true
        if (null != context && context.getResources().getBoolean(R.bool.allow_rotation)) {
    
    
            return true;
        }

        ......省略代码......
        return originalSmallestWidth >= 600;
    }

 public RotationHelper(Activity activity) {
    
    
        mActivity = activity;
            ......省略代码......
            
            //方法添加参数
            mHomeRotationEnabled = mSharedPrefs.getBoolean(ALLOW_ROTATION_PREFERENCE_KEY,
                    getAllowRotationDefaultValue(mActivity));
       
       ......省略代码......
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
    
    
        ......省略代码......
        
        mHomeRotationEnabled = mSharedPrefs.getBoolean(ALLOW_ROTATION_PREFERENCE_KEY,
                getAllowRotationDefaultValue(mActivity));
                
        ......省略代码......
    }

修改 RecentsOrientedState.java packages/apps/Launcher3/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java

private void updateHomeRotationSetting() {
    
    
        setFlag(FLAG_HOME_ROTATION_ALLOWED_IN_PREFS,
                mSharedPrefs.getBoolean(ALLOW_ROTATION_PREFERENCE_KEY, RotationHelper.getAllowRotationDefaultValue(mContext)));
    }

修改SettingsActivity.java packages/apps/Launcher3/src/com/android/launcher3/settings/SettingsActivity.java

        protected boolean initPreference(Preference preference) {
    
    
        
           ......省略代码......
           
           preference.setDefaultValue(getAllowRotationDefaultValue(getContext()));
        
           ......省略代码......
        }

3. 设置屏幕旋转后,设备重启后,屏幕旋转失效 修改 frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    
    
        super.onViewCreated(view, savedInstanceState);
        
        ......省略代码......

       if (display != null && rotationButtonController.isRotationLocked()) {
    
    
          //修改前为 rotationButtonController.setRotationLockedAtAngle(display.getRotation());
          //修改后,注释掉此句,或者获取 int user_rotation = Settings.System.getInt(getContext().getContentResolver(), Settings.System.USER_ROTATION, 0);的值设置
           //rotationButtonController.setRotationLockedAtAngle(display.getRotation());
       }
        ......省略代码......
    }

猜你喜欢

转载自blog.csdn.net/yjz_0314/article/details/131703458