Sauvegarder
Choix utilisateur pour les Cookies
Nous utilisons des cookies afin de vous proposer les meilleurs services possibles. Si vous déclinez l'utilisation de ces cookies, le site web pourrait ne pas fonctionner correctement.
Tout accepter
Tout décliner
Essentiel
Ces cookies sont nécessaires au bon fonctionnement du site, vous ne pouvez pas les désactiver.
Affichage
Accepter
Analytique
Outils utilisés pour analyser les données de navigation et mesurer l'efficacité du site internet afin de comprendre son fonctionnement.
Google Analytics
Accepter
Décliner

How to manage screen brightness for your Activity?

Bandeau Conference
Hello,

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
  getWindow().setAttributes(lp);

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.putInt(getContentResolver(),
  android.provider.Settings.System.SCREEN_BRIGHTNESS, i);
//et pour le mode
  android.provider.Settings.System.putInt(getContentResolver(),
  android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, System.SCREEN_BRIGHTNESS_MODE_MANUAL);

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:

  @Override
  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
        value=255-((255*i)/width);
        gradientAlpha.add(i, value);
      }
    }
    // the drawing the rainbow:
    drawHorizontalAlpha();
  }
  /**
   * 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++) {
      paint.setColor(Color.argb(gradientAlpha.get(i),255,255,255));
      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
Cette adresse e-mail est protégée contre les robots spammeurs. Vous devez activer le JavaScript pour la visualiser.
Auteur Android2EE
Ebooks to learn Android Programming.
AndroidEvolution

Auteur d'origine: mathias
Sauvegarder
Choix utilisateur pour les Cookies
Nous utilisons des cookies afin de vous proposer les meilleurs services possibles. Si vous déclinez l'utilisation de ces cookies, le site web pourrait ne pas fonctionner correctement.
Tout accepter
Tout décliner
Essentiel
Ces cookies sont nécessaires au bon fonctionnement du site, vous ne pouvez pas les désactiver.
Affichage
Accepter
Analytique
Outils utilisés pour analyser les données de navigation et mesurer l'efficacité du site internet afin de comprendre son fonctionnement.
Google Analytics
Accepter
Décliner