今天做项目的时候,需要实现一个功能,就是进入一个应用,在这个应用中,屏幕的亮度变为最亮。关键代码如下
bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub WindowManager.LayoutParams param = getWindow().getAttributes(); param.screenBrightness = 255 / 255.0f; getWindow().setAttributes(param); } }); bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub WindowManager.LayoutParams param = getWindow().getAttributes(); param.screenBrightness = 125 / 255.0f; getWindow().setAttributes(param); } }); bt3.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub WindowManager.LayoutParams param = getWindow().getAttributes(); param.screenBrightness = 2 / 255.0f; getWindow().setAttributes(param); } });
看下效果图
可以看到,方法就是通过
WindowManager.LayoutParams param = getWindow().getAttributes(); param.screenBrightness = 255 / 255.0f; getWindow().setAttributes(param);
来设定屏幕亮度的