以编程方式更改系统显示尺寸Android N [英] Change the system display size programmatically Android N

查看:186
本文介绍了以编程方式更改系统显示尺寸Android N的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:除了以前存在的更改<$的功能外,Android N还具有从设置更改系统显示大小的功能。 c $ c>字体大小。

Background: Android N comes with a feature to change system Display Size from settings, in addition to the previously present feature of changing Font Size.

更改显示大小

图片来源:pcmag.com

问题

如果某个应用具有 android.permission.WRITE_SETTINGS 权限来更改设置,则有如何以编程方式更改设备的字体设置:字体样式和字体大小?。但是,我找不到以编程方式更改显示尺寸的方法。可能吗?

If an app has android.permission.WRITE_SETTINGS permission to change the settings, there are ways to change the system font size programatically as mentioned in How to programmatically change font settings of Device: font style and font size?. However I couldn't find a way to change the display size programmatically. Is it possible?

我尝试了什么?

我已经检查了可能的选项在 Settings.System 的方便功能列表中

I've checked the possible options in the list of Settings.System convenience functions provided for changing settings programmatically.

更新

我已经为此处相同: https://code.google.com/p/ android / issues / detail?id = 214124 。如果觉得有用,请给它加注星标。

I've opened a feature request for the same here: https://code.google.com/p/android/issues/detail?id=214124 . If you feel it would be useful please star it.

推荐答案

只需分享我解决此要求的方法即可。我通过使用肮脏的Java反射方法来实现此功能-尽管效果不佳。

Just share my approach to tackle this requirement. I achieve this function by using the dirty Java reflection method - although it's not that elegant.

主要参考源代码文件为:

The main reference source code files are:

  • ScreenZoomSettings.java (https://github.com/aosp-mirror/platform_packages_apps_settings/blob/master/src/com/android/settings/display/ScreenZoomSettings.java)
  • DisplayDensityUtils.java (http://androidxref.com/9.0.0_r3/xref/frameworks/base/packages/SettingsLib/src/com/android/settingslib/display/DisplayDensityUtils.java)
  • WindowManagerGlobal.java (http://androidxref.com/9.0.0_r3/xref/frameworks/base/core/java/android/view/WindowManagerGlobal.java)

然后,按照以下步骤操作,即可获得所需的控制权:

And, follow the below steps, then you can get the required control:


  1. 读取 ZoomScreenSettings .java的onCreate()和commit()。他们演示了如何正确获取密度值并将其设置到框架中。

  2. 读取 DisplayDensityUtils .java。它显示了如何使用 WindowManagerService 来控制系统密度。因为我们无法通过反射获取 DisplayDensityUtils 的实例,所以我们需要了解使用了哪些 WindowManagerService 方法。 / li>
  3. 使用反射获取 WindowManagerService 的实例,并编写 DisplayDensityUtils -

  1. Read ZoomScreenSettings.java's onCreate() and commit(). They demonstrate how to correctly get and set the density value into the framework.
  2. Read DisplayDensityUtils.java. It shows how to use WindowManagerService to control the system density. Because we can't get the instance of DisplayDensityUtils via reflection, we need to understand which WindowManagerService methods are utilized.
  3. Use reflection to get WindowManagerService's instance, and write a DisplayDensityUtils-like class into your project.



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




  1. 使用类似于 DisplayDensityUtils 的类设置或获取密度值。刚提到的一件事是,如果您想传递一个索引值(例如,对于大尺寸的显示器,则为2),请将其馈送到类似 DisplayDensityUtils 的类的 mValues 数组以获取实际密度值,该值是传递给框架的正确值。获取当前密度指数也适用相同的概念。

  1. Set or get the density value with your DisplayDensityUtils-like class. One thing just to mention is that, if you want to pass an index value (e.g., 2 for large display size), please feed it to your DisplayDensityUtils-like class's mValues array to get the actual density value which is the right one to pass to the framework. Getting the current density index also applies the same concept.

这篇关于以编程方式更改系统显示尺寸Android N的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆