以编程方式更改系统显示大小Android N.

Sho*_*uri 9 android android-settings android-7.0-nougat

背景:Display Size除了之前现有的更改功能外,Android N还具有从设置更改系统的功能Font Size.

更改显示尺寸:

在此输入图像描述

图像来源:pcmag.com

问题:

如果应用程序android.permission.WRITE_SETTINGS有权更改设置,有多种方法可以编程方式更改系统字体大小,如如何以编程方式更改设备的字体设置:字体样式和字体大小?.但是我找不到以编程方式更改显示大小的方法.可能吗?

我试过了什么?

我已经检查了Settings.System便利函数列表中的可能选项,这些函数用于以编程方式更改设置.

更新:

我已在此处打开了相同的功能请求:https://code.google.com/p/android/issues/detail?id = 214124.如果你觉得它会有用,那就明星吧.

感谢您的帮助!

小智 6

只是分享我解决这个要求的方法。我通过使用肮脏的 Java 反射方法来实现这个功能——尽管它不是那么优雅。

主要参考源代码文件有:

并且,请按照以下步骤操作,然后您可以获得所需的控件:

  1. 阅读ZoomScreenSettings.java 的 onCreate() 和 commit()。他们演示了如何正确获取和设置密度值到框架中。
  2. 阅读DisplayDensityUtils.java。它显示了如何使用WindowManagerService来控制系统密度。因为我们无法DisplayDensityUtils通过反射获取实例,所以我们需要了解使用了哪些WindowManagerService方法。
  3. 使用反射获取WindowManagerService的实例,并DisplayDensityUtils在您的项目中编写一个类似 类的类。
// Where wm is short for window manager
val wmGlobalClz = Class.forName("android.view.WindowManagerGlobal")
val getWmServiceMethod = wmGlobalClz.getDeclaredMethod("getWindowManagerService")
val wmService = getWmServiceMethod.invoke(wmGlobalClz)

val wmInterfaceClz = Class.forName("android.view.IWindowManager")

// Now, we already have the ability to do many things we want.
// For instance, to get the default density value.
val getInitialDisplayDensityMethod = wmInterfaceClz.getDeclaredMethod(
        "getInitialDisplayDensity", 
        Integer.TYPE
)
val defaultDensity = getInitialDisplayDensityMethod.invoke(
        wmService,
        Display.DEFAULT_DISPLAY
) as Int

Run Code Online (Sandbox Code Playgroud)
  1. 使用您DisplayDensityUtils的类设置或获取密度值。需要提及的一件事是,如果您想传递一个索引值(例如,2 表示大显示尺寸),请将其提供给您的DisplayDensityUtils-like 类的mValues数组以获取实际密度值,该值是正确的传递给框架。获取电流密度指数也适用相同的概念。


小智 5

在清单中,在应用程序下: android:configChanges="density"

在活动/应用程序中:

public void adjustDisplayScale(Configuration configuration) {
    if (configuration != null) {
        Log.d("TAG", "adjustDisplayScale: " + configuration.densityDpi);
        if(configuration.densityDpi >= 485) //for 6 inch device OR for 538 ppi
            configuration.densityDpi = 500; //decrease "display size" by ~30
        else if(configuration.densityDpi >= 300) //for 5.5 inch device OR for 432 ppi
            configuration.densityDpi = 400; //decrease "display size" by ~30
        else if(configuration.densityDpi >= 100) //for 4 inch device OR for 233 ppi
            configuration.densityDpi = 200; //decrease "display size" by ~30
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        metrics.scaledDensity = configuration.densityDpi * metrics.density;
        this.getResources().updateConfiguration(configuration, metrics);
    }
}
Run Code Online (Sandbox Code Playgroud)

在 super.onCreate(..) 之后调用它:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    adjustDisplayScale( getResources().getConfiguration());
Run Code Online (Sandbox Code Playgroud)

这将在“显示尺寸”的“更小”和“小”设置之间设置显示尺寸,覆盖用户设置的任何显示尺寸设置。它可以为 4 英寸、5.5 英寸、6 英寸等设备正确自我调整......但我相信有比使用那些 if 语句更好的方法。


pRa*_*NaY 1

在引用Settings.System时 ,有一个 [ putConfiguration(ContentResolver cr, Configuration config)]( https://developer.android.com/reference/android/provider/Settings.System.html#putConfiguration(android.content.ContentResolver , android.content.res.Configuration) ) 方法。该方法的使用方法是:

从配置对象写入一批与配置相关的设置的便捷功能。

配置

这包括用户指定的配置选项(区域设置列表和缩放)以及设备配置(例如输入模式屏幕尺寸屏幕方向)。

使用SCREENLAYOUT_SIZE_MASK值设置屏幕尺寸的配置。这是:

SCREENLAYOUT_SIZE_MASK 位定义屏幕的整体尺寸。它们可能是SCREENLAYOUT_SIZE_SMALLSCREENLAYOUT_SIZE_NORMALSCREENLAYOUT_SIZE_LARGESCREENLAYOUT_SIZE_XLARGE之一。

我希望它对你有帮助。