Android:用Seekbar来调节屏幕亮度

 
 3 
 4 
 5 import com.dh.shoudiantong.screendatils.VerticalSeekBar;
 6 
 7 import android.app.Activity;
 8 import android.content.Context;
 9 import android.os.Bundle;
10 import android.view.MotionEvent;
11 import android.view.Window;
12 import android.view.WindowManager;
13 import android.widget.RelativeLayout;
14 import android.widget.SeekBar;
15 import android.widget.SeekBar.OnSeekBarChangeListener;
16 
17 
18 
19 public class ScreenActivity extends Activity{
20 
21     private RelativeLayout mylayout;//布局
22     Context context;
23     private int Max_Brightness = 255;//进度条的最大值
24     private VerticalSeekBar vSeekBar = null;//调节亮度的进度条
25     private float fBrightness = 0.0f;//亮度值
26     private WindowManager.LayoutParams lp = null;//窗口处理器的参数类
27     @Override
28     protected void onCreate(Bundle savedInstanceState) {
29         // TODO Auto-generated method stub
30         super.onCreate(savedInstanceState);
31         HideStatusBase();
32         setContentView(R.layout.screen_activity);
33         mylayout=(RelativeLayout)findViewById(R.id.mylayout);        
34         
35         vSeekBar = (VerticalSeekBar) findViewById(R.id.verticalSeekBar);
36         vSeekBar.setMax(Max_Brightness);
37         vSeekBar.setProgress(Max_Brightness);//默认进度值为最大
38         
39         vSeekBar.setOnSeekBarChangeListener(new SeekBarChangedListener());
40        //默认屏幕为最大亮度
41         lp = getWindow().getAttributes();
42         fBrightness = (float) vSeekBar.getProgress() / (float)Max_Brightness;
43         lp.screenBrightness =fBrightness;
44         getWindow().setAttributes(lp);
45 lp.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; 46 47 } 48 class SeekBarChangedListener implements OnSeekBarChangeListener{ 49 @Override 50 public void onProgressChanged(SeekBar seekBar, int progress, 51 boolean fromUser) { 52 // TODO Auto-generated method stub 53 if(progress<1) progress=1;//此处是为了避免screenbrightness=0,从而导致屏幕自动休眠锁屏 54 fBrightness = (float) progress/ (float)Max_Brightness; 55 lp.screenBrightness =fBrightness; 56 getWindow().setAttributes(lp); 57 } 58 59 @Override 60 public void onStartTrackingTouch(SeekBar seekBar) { 61 // TODO Auto-generated method stub 62 63 } 64 65 @Override 66 public void onStopTrackingTouch(SeekBar seekBar) { 67 // TODO Auto-generated method stub 68 69 } 70 71 } 72 /** 73 * 屏幕点击事件显示菜单 74 */ 75 @Override 76 public boolean onTouchEvent(MotionEvent event){ 77 78 return false; 79 } 80 /** 81 * 全屏设置 82 */ 83 private void HideStatusBase() 84 { 85 requestWindowFeature(Window.FEATURE_NO_TITLE); 86 int flag=WindowManager.LayoutParams.FLAG_FULLSCREEN; 87 Window myWindow=this.getWindow(); 88 myWindow.setFlags(flag,flag); 89 } 90 91 } 92 93

效果图:

 

lp.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
是为了避免屏幕进入休眠状态

注意:当SeekBrightness=0时,屏幕会自动休眠锁屏;为了解决这个问题,我将progress小于1时设为1;

以上仅为本人为了记录,方便以后查略;若有错误,请不吝指正。

posted @ 2013-09-13 15:56  逍遥剑臣  阅读(1300)  评论(0编辑  收藏  举报