Hello,
There are several notions to understand when trying to change screen 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.