Android obtains the brightness of an image.

Source: Internet
Author: User

 

Problem: there is now a background control AA (such as ImageView, LinearLayout, ViewGroup, or other controls ), now, if you want to place another control BB on this control to display the current status (for example, the screen indicator in Launcher), the background of the control AA can be dynamically modified in the program, if the background of AA is similar to the background color of BB, the effect of BB is not obvious. It is not clear that the current AA status is displayed.

 

Solution: Obtain the RGB value of the AA background, calculate the brightness of the current screen based on the RGB value, and create two sets of pictures for BB, which are brighter and darker, respectively, if the brightness value of AA is relatively high, BB uses a darker image. If the brightness value of AA is relatively low, BB uses a brighter image, so that BB plays a significant role.

 

The Code is as follows:

 

 

Drawable localDrawable = wpm. getDrawable ();

Bitmap bitmap = Bitmap

. CreateBitmap (

LocalDrawable. getIntrinsicWidth (),

LocalDrawable. getIntrinsicHeight (),

LocalDrawable. getOpacity ()! = PixelFormat. OPAQUE? Bitmap. Config. ARGB_8888

: Bitmap. Config. RGB_565 );

Canvas canvas = new Canvas (bitmap );

LocalDrawable. setBounds (0, 0, localDrawable. getIntrinsicWidth (),

LocalDrawable. getIntrinsicHeight ());

LocalDrawable. draw (canvas );

 

Int localWidth = this. getWindowManager (). getdefadisplay display (). getWidth ();

Int y [] = {0, 4, 9, 13, 18, 23, 28, 33, 38, 43, 48 };

Int x [] = {0, localWidth/8, localWidth * 2/8, localWidth * 3/8,

LocalWidth * 4/8, localWidth * 5/8, localWidth * 6/8,

LocalWidth * 7/8, localWidth };

 

Int r;

Int g;

Int B;

Int number = 0;

Double bright = 0;

Integer localTemp;

For (int I = 0; I <x. length; I ++ ){

For (int j = 0; j <y. length; j ++ ){

Number ++;

LocalTemp = (Integer) bitmap. getPixel (x [I], y [j]);

R = (localTemp | 0xff00ffff)> 16 & 0x00ff;

G = (localTemp | 0xffff00ff)> 8 & 0x0000ff;

B = (localTemp | 0xffffff00) & 0x0000ff;

 

Bright = bright + 0.299 * r + 0.587 * g + 0.114 * B;

Log. I ("xiao", "bright =" + bright );

}

}

LocalDrawable = null;

Bitmap = null;

Bright = (int) (bright/number );

 

Code explanation: Get the Drawable object and convert it to Bitmap (the Drawable object does not return its RGB value function, Bitmap can return the RGB value of a certain point bitmap. getPixel), get the RGB value of some specific points in the current region, bitmap. getPixel returns the ARGB value. The R, G, and B values are obtained through the shift operation. The brightness is calculated using the brightness = 0.229 × R + 0.587 * G + 0.114 * B, add the brightness values of all vertices and obtain an average value. If the value is greater than 128, the image is brighter. If the value is smaller than 128, the image is darker.

 

From: column xiaoxiaobian3310903

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.