17

Does anyone have an idea how to implement an Brightness Screen Filter like the one here:

http://www.appbrain.com/app/screen-filter/com.haxor

I need a starting point and I can't figure out how to do it.

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
Alex
  • 2,153
  • 4
  • 29
  • 43
  • i have faced same issue. i have also posted this question http://stackoverflow.com/questions/2222146/control-screen-brightness-in-android-using-background-service. I was able to resolve the issue. U can see the right method in the post. – Naveen Murthy Apr 14 '11 at 08:09
  • try this link it may help you a little: http://android-er.blogspot.com/2011/02/change-android-screen-brightness.html –  May 20 '11 at 12:56

4 Answers4

8

Just make a transparent full screen activity that lets touches pass through. To make touches pass through use the following Window flags before setting the contentView:

@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  Window window = getWindow();

  // Let touches go through to apps/activities underneath.
  window.addFlags(FLAG_NOT_TOUCHABLE);

  // Now set up content view
  setContentView(R.layout.main);
}

For your main.xml layout file just use a full screen LinearLayout with a transparent background:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/background"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#33000000">
</LinearLayout>

Then to adjust the "brightness" just change the value of the background colour from your code somewhere:

findViewById(R.id.background).setBackgroundColor(0x66000000);
pheelicks
  • 7,201
  • 2
  • 43
  • 49
  • Thanks, it's working related to setting the transparency, but the touch events seems to work strange, I can scroll but when I click an program icon seems like it freeze (not quite freeze, but not opening the programs) – Alex Nov 26 '10 at 20:13
  • Any other ideas ?I don't think that having an transparent activity on top is the solution – Alex Nov 29 '10 at 12:20
  • I'm not giving up, nor I have any new ideas beside something related to OpenGL and surfaceview, but I still hope that someone could have a clear idea how this is done. – Alex Dec 08 '10 at 11:29
  • Alex, I do think my solution should work. I have done something similar and have no issues with touches getting passed through to the app behind. Try playing around with the window flags to see if you can make it work – pheelicks Dec 08 '10 at 17:45
  • 2
    I have implemented independently almost this exact solution and it is working perfectly, even with sophisticated touch events like pinch-zoom. The touch events problem may be separate. – OverclockedTim Mar 04 '11 at 19:09
  • **2016** window.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); – Andy the android Sep 28 '16 at 14:08
4
  • Get an instance of WindowManager.

    WindowManager windowManager = (WindowManager) Class.forName("android.view.WindowManagerImpl").getMethod("getDefault", new Class[0]).invoke(null, new Object[0]);

  • Create a full screen layout xml(layout parameters set to fill_parent)

  • Set your view as not clickable, not focusable, not long clickable, etc so that touch is passed through to your app and the app can detect it.

    view.setFocusable(false);
    view.setClickable(false);
    view.setKeepScreenOn(false);
    view.setLongClickable(false);
    view.setFocusableInTouchMode(false);

  • Create a layout parameter of type android.view.WindowManager.LayoutParams. LayoutParams layoutParams = new LayoutParams();

  • Set layout parameter like height, width etc

    layoutParams.height = LayoutParams.FILL_PARENT; 
    layoutParams.width = LayoutParams.FILL_PARENT;
    layoutParams.flags = 280; // You can try LayoutParams.FLAG_FULLSCREEN too
    layoutParams.format = PixelFormat.TRANSLUCENT; // You can try different formats
    layoutParams.windowAnimations = android.R.style.Animation_Toast; // You can use only animations that the system to can access
    layoutParams.type = LayoutParams.TYPE_SYSTEM_OVERLAY;
    layoutParams.gravity = Gravity.BOTTOM;
    layoutParams.x = 0;
    layoutParams.y = 0;
    layoutParams.verticalWeight = 1.0F;
    layoutParams.horizontalWeight = 1.0F;
    layoutParams.verticalMargin = 0.0F;
    layoutParams.horizontalMargin = 0.0F;
    
  • Key step: You can set what percentage of brightness you need. layoutParams.setBackgroundDrawable(getBackgroundDrawable(i));

    private Drawable getBackgroundDrawable(int i) {
    int j = 255 - (int) Math.round(255D * Math.exp(4D * ((double) i / 100D) - 4D));
    return new ColorDrawable(Color.argb(j, 0, 0, 0));}
    
  • Finally add view to windowManager that you created earlier.

    windowManager.addView(view, layoutParams);

Note: You need SYSTEM_ALERT_WINDOW permission to lay an overlay on the screen.

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Have tested this and it works. Let me know if you get stuck.

  • please can you post a complete answer to this [question](http://stackoverflow.com/questions/22748031/create-a-simple-screen-filter) Using examples will be highly appreciated – noobProgrammer Apr 03 '14 at 19:25
1

Of course you can't use this is production code, but if you are playing around .. try this Undocumented hack

It uses :

private void setBrightness(int brightness) {
try {
  IHardwareService hardware = IHardwareService.Stub.asInterface(
   ServiceManager.getService("hardware"));
  if (hardware != null) {
    hardware.setScreenBacklight(brightness);
  }
 } catch (RemoteException doe) {          
  }        
 }

Remember that it uses this permission :

 <uses-permission android:name="android.permission.HARDWARE_TEST"/>
Reno
  • 32,851
  • 11
  • 85
  • 99
  • 1
    where can i find hardware09.jar? for this [tutorial(http://www.tutorialforandroid.com/2009/01/changing-screen-brightness.html) – noobProgrammer Jan 30 '14 at 11:01
0

You ca try this also:

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.max_bright);



        WindowManager.LayoutParams lp = getWindow().getAttributes();

        lp.screenBrightness = 100 / 100.0f;

        getWindow().setAttributes(lp);

    }
yanchenko
  • 53,981
  • 33
  • 142
  • 163
akkilis
  • 1
  • 1