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

news/2024/7/3 0:21:41

前言

这里是客户要求添加按钮以实现屏幕旋转。屏幕旋转使用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;
     }


http://lihuaxi.xjx100.cn/news/2220559.html

相关文章

代码随想三刷二叉树篇2

代码随想三刷二叉树篇2 101. 对称二叉树题目代码 104. 二叉树的最大深度题目代码 111. 二叉树的最小深度题目代码 222. 完全二叉树的节点个数题目代码 110. 平衡二叉树题目代码 257. 二叉树的所有路径题目代码 101. 对称二叉树 题目 链接 代码 /*** Definition for a binar…

安装,管理程序

文章目录 Linuxd应用程序基础应用程序与系统命令的关系 典型应用程序目录常见的软件包装类型 rpm软件包管理工具RPM软件包rpm命令格式查询rpm软件包信息查询已安装的查询未安装的 安装或升级rpm软件卸载指定rpm软件辅助选项 维护RPM数据库解决软件包依赖关系方法 源代码编译安装…

铠侠全面复产:NAND价格还会涨吗?

近期&#xff0c;日本经济新闻&#xff08;Nikkei&#xff09;报道指出&#xff0c;经历长达20个月的产能削减后&#xff0c;全球第四大三维NAND闪存制造商铠侠已全面恢复生产。这一转变不仅标志着铠侠再次全力投入到市场份额的争夺中&#xff0c;也可能预示着闪存市场价格即将…

20240616日志:大模型压缩方法DMS

Location: Beijing 1 大模型剪枝 Fig. 1.1大模型压缩-剪枝 剪枝的理论来源基于彩票假设&#xff08;Lottery Ticket Hypothesis&#xff09;&#xff0c;指在神经网络中存在一种稀疏连接模式&#xff0c;即仅利用网络的一小部分连接&#xff08;彩票&#xff09;就足以实现与整…

git idea分支cherry-pick

git idea分支cherry-pick cherry-pick请注意操作前更新代码&#xff01;&#xff01;&#xff01;操作步骤 cherry-pick cherry-pick 挑拣樱桃&#xff0c;对应在分支开发中就是把提交记录从A分支挑拣到B分支 请注意操作前更新代码&#xff01;&#xff01;&#xff01; 操作…

数智教育创新如何向未来?腾讯云与你探索革新之路

引言 随着科技革命的快速发展&#xff0c;掀起教育领域的变革&#xff0c;新理念、新技术、新模式、新应用正不断涌现&#xff0c;正塑造着教育的未来形态。未来科技还将如何赋能教育创新&#xff1f; 5月31日&#xff0c;由腾讯云TVP 与西安电子科技大学联合举办的「数智教育的…

聊聊系统架构之负载均衡优化实践

一、写在前面 最近在进行线上监控检查时&#xff0c;我遇到了两个超出预期的案例。首先&#xff0c;网关层的监控数据与应用实际监控数据存在不一致性&#xff0c;尤其是max有较大的差异&#xff0c;详见如下图。其次在某个应用中&#xff0c;通过httpclient请求某域名时发现只…

创建最基本的web服务器-http模块

在Node.js中&#xff0c;可以使用内置的http模块来创建一个最基本的web服务器。以下是一个简单的示例&#xff0c;它创建了一个HTTP服务器&#xff0c;该服务器监听一个端口&#xff0c;并在接收到请求时发送一个“Hello, World!”的响应。 // 引入http模块 const http requi…