【Android 11】AOSP Settings添加屏幕旋转按钮

前言

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

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

这里的值可以是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>

添加了两个字段,分别是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. 在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. 写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. 把上面这个加到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;
     }

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android AOSP11中,可以通过以下步骤来监听setStatusBarColor: 1. 创建一个继承自View类的自定义视图(CustomView),并且重写它的onApplyWindowInsets方法。 2. 在onApplyWindowInsets方法中,使用WindowInsetsCompat类获取状态栏的颜色,并将其保存到一个变量中。 3. 在CustomView的构造函数中,使用ViewCompat类的addOnApplyWindowInsetsListener方法,将CustomView添加为监听器。 4. 在CustomView的onApplyWindowInsets方法中,调用super.onApplyWindowInsets(insets)方法,以确保系统默认的WindowInsets处理逻辑被执行。 5. 在CustomView中,可以通过监听状态栏颜色的变化,来执行一些自定义的操作。 下面是一个简单的示例代码: ```java public class CustomView extends View { private int mStatusBarColor = Color.TRANSPARENT; public CustomView(Context context, AttributeSet attrs) { super(context, attrs); ViewCompat.setOnApplyWindowInsetsListener(this, new OnApplyWindowInsetsListener() { @Override public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) { mStatusBarColor = insets.getSystemWindowInsetTop(); return insets.consumeSystemWindowInsets(); } }); } @Override public WindowInsets onApplyWindowInsets(WindowInsets insets) { // Call the super implementation to consume the system window insets. insets = super.onApplyWindowInsets(insets); // Do something with the status bar color. // ... return insets; } } ``` 注意:要在AndroidManifest.xml中为CustomView所在的Activity设置fitsSystemWindows属性为true,以确保系统会为状态栏留出空间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值