当前位置:   article > 正文

【Android 11】AOSP Settings添加屏幕旋转按钮_rotation0,1,2,3

rotation0,1,2,3

前言

这里是客户要求添加按钮以实现屏幕旋转。屏幕旋转使用adb的命令很容易实现:

#屏幕翻转
adb shell settings put system user_rotation 1
#屏幕正常模式
adb shell settings put system user_rotation 0
  • 1
  • 2
  • 3
  • 4

这里的值可以是0,1,2,3 的任意一个。我这里没有陀螺仪,所以只需要这个命令就够了。更多的可以参考android 通过adb shell命令旋转Android屏幕朝向方向
但是这有个缺陷,就是开机的动画不能随着设置好的屏幕方向旋转

图片效果

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

代码

  1. 首先在资源配置文件中定义我们需要用到的字段,以供后面国际化适配的时候更方便
    b/packages/apps/Settings/res/values/strings.xml
@@ -2706,6 +2706,10 @@
+    <!-- Screen rotate title-->
+    <string name="screen_rotate_title">Screen rotate</string>
+    <!-- Screen rotate summary-->
+    <string name="screen_rotate_summary">Control screen orientation</string>
  • 1
  • 2
  • 3
  • 4
  • 5

添加了两个字段,分别是screen_rotate_title screen_rotate_summary

  1. 然后配置我们的四个选项值
    packages/apps/Settings/res/values/arrays.xml
@@ -1501,4 +1501,24 @@
         <item>@string/rtt_settings_always_visible</item>
     </string-array>

+    <!-- Screen rotate settings.  These are shown in a list dialog. -->
+    <string-array name="screen_rotate_entries">
+        <item>0</item>
+        <item>90</item>
+        <item>180</item>
+        <item>270</item>
+    </string-array>
+
+    <!-- Do not translate. -->
+    <string-array name="screen_rotate_values" translatable="false">
+        <!-- Do not translate. -->
+        <item>0</item>
+        <!-- Do not translate. -->
+        <item>90</item>
+        <!-- Do not translate. -->
+        <item>180</item>
+        <!-- Do not translate. -->
+        <item>270</item>
+    </string-array>
+
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  1. 在xml中增加我们需要的List按钮,我这里放在了settings——display界面。
    packages/apps/Settings/res/xml/display_settings.xml
+    <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"
+    settings:controller="com.android.settings.display.ScreenRotatePreferenceController"/>
+

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  1. 写Controller文件,就是我们上一步设置的com.android.settings.display.ScreenRotatePreferenceController
package com.android.settings.display;

import android.content.Context;
import android.provider.Settings;
import android.view.Surface;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;

import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
import android.util.Log;
import android.content.Intent;

public class ScreenRotatePreferenceController extends BasePreferenceController implements
        Preference.OnPreferenceChangeListener, PreferenceControllerMixin {

    public static final String KEY_SCREEN_ROTATE = "screen_rotate";
    private ListPreference mScreenRotatePreference;

    public ScreenRotatePreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
    }

    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mScreenRotatePreference = (ListPreference) screen.findPreference(KEY_SCREEN_ROTATE);
        if (mScreenRotatePreference != null) {
            mScreenRotatePreference.setOnPreferenceChangeListener(this);
            int index = Settings.System.getInt(mContext.getContentResolver(), Settings.System.USER_ROTATION, 0);
            mScreenRotatePreference.setValueIndex(index);
        }
    }

    public void setScreenRotation(String value) {
        int 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);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object objValue) {
        final String key = preference.getKey();
        if (key.equals(KEY_SCREEN_ROTATE)) {
            setScreenRotation((String) objValue);
            return true;
        }
        return false;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  1. 把上面这个加到DisplaySettings.java里面
    packages/apps/Settings/src/com/android/settings/DisplaySettings.java
@@ -32,6 +32,7 @@ import com.android.settings.display.TapToWakePreferenceController;
 import com.android.settings.display.ThemePreferenceController;
 import com.android.settings.display.TimeoutPreferenceController;
 import com.android.settings.display.VrDisplayPreferenceController;
+import com.android.settings.display.ScreenRotatePreferenceController;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settingslib.core.AbstractPreferenceController;
 import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -90,6 +91,7 @@ public class DisplaySettings extends DashboardFragment {
         controllers.add(new ShowOperatorNamePreferenceController(context));
         controllers.add(new ThemePreferenceController(context));
         controllers.add(new BrightnessLevelPreferenceController(context, lifecycle));
+        controllers.add(new ScreenRotatePreferenceController(context, ScreenRotatePreferenceController.KEY_SCREEN_ROTATE));
         return controllers;
     }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/747758
推荐阅读
相关标签
  

闽ICP备14008679号