问题:现在有一个有背景的控件AA(比如ImageView、LinearLayout、ViewGroup或者其他的控件),现在想在这个控件上放置其他控件BB来显示当前的状态(比如Launcher中的屏幕指示器),控件AA的背景在程序中是可以动态修改的,如果AA的背景和BB的背景色很相近,BB的效果就不是很明显,不能很清楚的现实出当前AA的状态。
解决办法:获取到AA背景的RGB值,根据RGB值计算当前屏幕的亮度,为BB制作两套图片,分别为较亮的和较暗的,如果AA的亮度值比较高,则BB使用较暗的图片,如果AA的亮度值比较低,则BB使用较亮的图片,这样BB的作用就很明显。
代码如下:
- 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().getDefaultDisplay().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);
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().getDefaultDisplay().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);
代码解释:得到Drawable对象后转换为Bitmap(Drawable对象没有返回它RGB值的函数,Bitmap可以返回某个点的RGB值bitmap.getPixel),对当前区域取一些特定点获取其RGB值,bitmap.getPixel返回的是ARGB值,通过移位操作获取到R、G、B的值,使用亮度=0.229×R + 0.587*G + 0.114*B进行亮度值计算,将所有点的亮度值相加后取一个平均值,如果这个值比128大,则这个图片较亮,如果这个值比128小,则这个图比较暗。