在安卓上添加调光器并像Twilight应用程序一样更改屏幕亮度

3

我想在设备屏幕上添加一个调光器,并过滤光线通量,就像twilight应用程序一样。
结果看起来像这样:

enter image description here

我正在寻找与之相关的API、示例代码或文档。目前,我只知道如何在Android上通过编程方式改变亮度。
有什么建议吗?

1个回答

12
为了实现这个结果,您需要创建一个窗口,使用类型 TYPE_SYSTEM_ALERT 并将其显示在所有其他应用程序的顶部。为此,您需要获得 SYSTEM_ALERT_WINDOW 权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

然后使用所需的背景颜色创建新的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF4081"
    android:orientation="vertical">
</LinearLayout>

还有一个服务

public class DrawOverAppsService extends Service {

    public static final String TAG = "DrawOverAppsService";

    private View mOverlayView;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d(TAG, "onCreate");

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                        WindowManager.LayoutParams.FLAG_DIM_BEHIND,
                PixelFormat.TRANSLUCENT);

        // An alpha value to apply to this entire window.
        // An alpha of 1.0 means fully opaque and 0.0 means fully transparent
        params.alpha = 0.1F;

        // When FLAG_DIM_BEHIND is set, this is the amount of dimming to apply.
        // Range is from 1.0 for completely opaque to 0.0 for no dim.
        params.dimAmount = 0.8F;

        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mOverlayView = inflater.inflate(R.layout.overlay_view, null);

        wm.addView(mOverlayView, params);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        Log.d(TAG, "onDestroy");

        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        wm.removeView(mOverlayView);
    }
}

要显示覆盖层,请使用以下命令启动服务:

Intent intent = new Intent(this, DrawOverAppsService.class);
startService(intent);

使用以下方式停止覆盖:

Intent intent = new Intent(this, DrawOverAppsService.class);
stopService(intent);

它起作用了。非常感谢。只有一个问题:我如何通过LinearLayout的alpha颜色来改变屏幕的亮度和强度设置? - ductran
不客气。要更改暗度和亮度设置,您可以使用LayoutParams的alphadimAmount字段,请参阅更新的答案以了解如何使用它们。 - Mattia Maestrini
我如何在添加应用程序到布局后执行点击操作,因为我无法在添加视图后面进行点击。 - Zilan Pattni
@ZilanPattni 添加此标志 - WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE - Lin S

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接