Welcome Android Developpers

Welcome Android Developpers
My name is Mathias Séguy, I am an Android Expert and Trainer and created the Android2EE company.

For full information, it’s here : Android2EE's Training
You can meet me on Google+, follow me on Twitter or on LinkedIn

Tuesday, November 15, 2011

How to manage screen brightness for your Activity?

Bandeau Conference

There are several notions to understand when trying to change screen brightness.

First: You want to change your Activity's brightness

Until CupCake, if you want to manage the brightness of your activity you have to use the following code in your onResume (or onCreate) method:
// make it effective
  LayoutParams lp = getWindow().getAttributes();
  lp.screenBrightness = 1.0f; // 0.0 - 1.0

This code changes the brightness of your screen when called, but doesn't change the system's brigthness parameters (that's the good practice). The brightness will go back to normal when your activity is destroyed (finished). There is no need to ask for permission in your Manifest neither to set back the brightness values when leaving your application. Brief, you this code when managing your screen's brightness.

Second: Changing the system's brightness, old method (deprecated in a way).

The code below seems to change the brightness:
// change the brightness using i where 0<=i<255
  android.provider.Settings.System.SCREEN_BRIGHTNESS, i);
//et pour le mode

Yep, but... This code changes the system's brightness parameters. They have no direct effect on anything. For the effects to be set, you need to restart your device (so what this code is for?o? don't know...). This code is not the one to use even if it appears a lot in forums.

Third: Managing brightness of your view (transparency).

Sometimes, you have the control on the onDraw method (don't ask why :o). There you can draw transparency areas (and therefore manage brightness if you have a back black background). The code below shows how to play with the alpha when drawing areas. This example shows how to draw a gradient from black to white:

  public void onDraw(Canvas canvas) {
    // retrieve the width
    width = this.getWidth();
    this.canvas = canvas;
    // if the gradientAlpha is not instanciate, do it:
    if (gradientAlpha == null) {
      // instanciate the gradientColor
      gradientAlpha = new ArrayList<Integer>(255);
      int value;
      // instanciate all the values of the list depending on the width of the area to draw
      for(int i=0;i<width;i++) {
        //for x=widht, value=0 and for x=0, value=255
        //So value=255-255*x/width
        gradientAlpha.add(i, value);
    // the drawing the rainbow:
   * Fill the view with a vertical rainbow
  private void drawHorizontalAlpha() {
    // Just browse the absissa and for each draw a vertical line using the gradient color
    // associated to that absissa
    for (int i = 0; i < width; i++) {
      canvas.drawRect(i, 0, i+1, height, paint);

When alpha equals 0 it's transparent, when alpha equals 255 it's opaque.
Using this technic, you can easily draw a gradient between two colors: first display you first color, then redraw on the same region your second color changing the alpha according to absissa.

So, Thanks who?
Thanks, Android2ee, the Android Programming Ebooks :o)

Mathias Séguy
Auteur Android2EE
Ebooks to learn Android Programming.

No comments:

Post a Comment