Save
Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Essential
These cookies are needed to make the website work correctly. You can not disable them.
Affichage
Accept
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline

How to manage screen brightness for your Activity?

Bandeau Conference
Hello,.

There are several concepts 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 effective it
LayoutParams lp @description 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);
and for 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 members 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 foreach 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 it the same region your second color changing the alpha selon absissa.

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

Mathias Séguy
This email address is being protected from spambots. You need JavaScript enabled to view it.
Author Android2EE
EBooks to learn Android Programming.
AndroidEvolution

Original author: mathias
Save
Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Essential
These cookies are needed to make the website work correctly. You can not disable them.
Affichage
Accept
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline