You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

如何通过编程方式实现屏幕单色显示?(无需开启开发者选项单色模式)

实现屏幕单色显示的编程方案

当然可以!完全不用依赖开发者选项里的单色模式,不管是只想让自己的应用变成单色,还是要全局生效,都有对应的编程实现方式,下面给你详细拆解几种可行方案:

1. 全局生效:借助辅助功能服务(AccessibilityService)

这是最稳定的全局实现方式,Android的辅助功能框架本来就支持全局颜色调整,不需要特殊悬浮窗权限,只是需要用户在系统设置里开启你的辅助服务。

步骤和示例代码:

  • 创建一个继承自AccessibilityService的类,在服务连接成功后配置灰度颜色矩阵:
@Override
protected void onServiceConnected() {
    super.onServiceConnected();
    // 标准灰度转换矩阵
    float[] grayMatrix = {
        0.2989f, 0.5870f, 0.1140f, 0, 0,
        0.2989f, 0.5870f, 0.1140f, 0, 0,
        0.2989f, 0.5870f, 0.1140f, 0, 0,
        0, 0, 0, 1, 0
    };
    // 配置放大功能(仅改颜色,不缩放)
    MagnificationConfig config = new MagnificationConfig.Builder()
            .setScale(1.0f)
            .setColorMatrix(grayMatrix)
            .build();
    setMagnificationConfig(config);
}
  • 别忘了在AndroidManifest.xml里声明服务和权限:
<service
    android:name=".GrayScaleAccessibilityService"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
    <intent-filter>
        <action android:name="android.accessibilityservice.AccessibilityService" />
    </intent-filter>
    <meta-data
        android:name="android.accessibilityservice"
        android:resource="@xml/accessibility_service_config" />
</service>
  • 还需要在res/xml/accessibility_service_config.xml中配置服务基本信息,确保系统能识别。

2. 单应用生效:给窗口添加颜色滤镜

如果只需要让你自己的应用变成单色,这个方法最简单,不需要任何系统权限,只需在Activity里给根视图设置灰度滤镜:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 获取应用窗口根视图
    View rootView = getWindow().getDecorView();
    Paint grayPaint = new Paint();
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0); // 饱和度设为0直接转为灰度
    grayPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
    // 开启硬件加速层应用滤镜
    rootView.setLayerType(View.LAYER_TYPE_HARDWARE, grayPaint);
}

这个方案仅对当前应用生效,退出应用后屏幕自动恢复正常,适合不需要全局生效的场景。

3. 全局生效:系统悬浮窗覆盖层

如果不想用辅助功能,也可以通过全屏悬浮窗实现全局灰度,但需要申请SYSTEM_ALERT_WINDOW权限(Android 8.0及以上为TYPE_APPLICATION_OVERLAY类型)。

示例代码:

WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
// 创建全屏透明视图,仅用于应用颜色滤镜
View grayOverlay = new View(this);
grayOverlay.setBackgroundColor(Color.TRANSPARENT);

// 设置灰度滤镜
Paint grayPaint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
grayPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
grayOverlay.setLayerType(View.LAYER_TYPE_HARDWARE, grayPaint);

// 配置悬浮窗参数
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT,
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY :
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
        PixelFormat.TRANSLUCENT
);

// 将悬浮窗添加到系统窗口
windowManager.addView(grayOverlay, params);

注意:用户需要在系统设置中允许应用使用悬浮窗权限,否则该方案无法生效。


内容的提问来源于stack exchange,提问作者cgifox

火山引擎 最新活动