簡體   English   中英

在像twilight app之類的android上添加調光器並更改屏幕亮度

[英]Add a dimmer and change screen brightness on android like twilight app

我想在設備屏幕上添加調光器,並像光之城應用一樣過濾光通量
結果看起來像這樣:

在此處輸入圖片說明

我正在尋找實現它的API,示例代碼或文檔。 此刻,我只知道如何在android上以編程方式更改亮度。
有什么建議嗎?

要獲得此結果,您需要使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM