【问题标题】:Add a dimmer and change screen brightness on android like twilight app在 Android 上添加调光器并更改屏幕亮度,例如暮光之城应用程序
【发布时间】:2015-10-29 16:06:52
【问题描述】:

我想在设备屏幕上添加一个调光器并过滤光通量,例如twilight app
结果如下所示:

我正在寻找实现它的 API、示例代码或文档。目前,我只知道如何在 android 上以编程方式更改亮度。
有什么建议吗?

【问题讨论】:

    标签: android screen brightness


    【解决方案1】:

    要实现此结果,您需要创建一个窗口,使用类型 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>
    

    还有一个Service

    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颜色?
    • 不客气。要更改暗淡和强度设置,您可以使用LayoutParamsalphadimAmount 字段,请参阅有关如何使用它们的更新答案。
    • 如何在布局上添加应用程序后执行点击,因为添加视图后我无法点击视图后面
    • @ZilanPattni 添加这个标志 - WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多