I want to add a dimmer to device screen and filters the flux of light like the twilight app
The result look like this:

I'm looking for the APIs, example code or documents to achieve it. At the moment, I just know how to change the brightness programmatically on android.
Is there any suggestion?
To achieve this result you need to create a window, using the type TYPE_SYSTEM_ALERT and show it on top of all other apps. To do so you need the permission SYSTEM_ALERT_WINDOW:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Then create a new layout with the desired background color:
<?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>
And a 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);
}
}
To display the overlay start the service with:
Intent intent = new Intent(this, DrawOverAppsService.class);
startService(intent);
And stop the overlay with:
Intent intent = new Intent(this, DrawOverAppsService.class);
stopService(intent);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With