Android 11.0 fügt ein Bildschirmrotationsmenü in „Einstellungen-Anzeige“ hinzu

Android 11.0 fügt ein Bildschirmrotationsmenü in „Einstellungen-Anzeige“ hinzu

Der Effekt ist wie folgt

Fügen Sie hier eine Bildbeschreibung einFügen Sie hier eine Bildbeschreibung ein

Fügen Sie eine Ressourcendatei für die Menüoberfläche hinzu

1. Fügen Sie den Namen und die Beschreibung der Rotationsoption hinzu und ändern Sie packets/apps/Settings/values/strings.xml

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

2. Fügen Sie die Auswahlmenüoption hinzu und ändern Sie packets/apps/Settings/res/values/arrays.xml

„screen_rotate_entries“ entspricht dem Optionsnamen, „screen_rotate_values“ entspricht dem Wert der Option; translatable="false" bedeutet, dass die Übersetzung nicht unterstützt wird, d. h. es besteht keine Notwendigkeit, das chinesische Format für „screen_rotate_values“ zu konfigurieren.

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

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

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

packets/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>

Menüoberfläche hinzufügen

1. Fügen Sie eine Menüoberfläche hinzu und ändern Sie packets/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. Öffnen Sie 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. Öffnen Sie 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;
    }

Zu diesem Zeitpunkt ist das Bildschirmrotationsmenü unter „Einstellungen-Anzeige“ erschienen und die Benutzeroberfläche kann normal gedreht werden.

Lösen Sie verwandte Probleme

1. Die Navigationsleiste ist auf der rechten Seite fixiert, wenn der Bildschirm horizontal ist, anstatt sich nach unten zu drehen. Ändern Sie „frameworks/base/core/res/res/values/config.xml“.

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

2. Nachdem Sie die Bildschirmdrehung eingestellt haben, kehren Sie zum Desktop zurück, aber der Desktop wird nicht gedreht. Ändern Sie packets/apps/Launcher3/res/values/config.xml

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

Verwenden Sie RotationHelper packets/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));
                
        ......省略代码......
    }

Verwenden Sie RecentsOrientedState.java packets/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)));
    }

Laden Sie SettingsActivity.java packets/apps/Launcher3/src/com/android/launcher3/settings/SettingsActivity.java herunter

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

3. Nach dem Festlegen der Bildschirmdrehung wird die Bildschirmdrehung nach dem Neustart des Geräts ungültig. Ändern Sie 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());
       }
        ......省略代码......
    }

Guess you like

Origin blog.csdn.net/yjz_0314/article/details/131703458