使用SeekBar组件调节屏幕亮度
2013-06-16 17:37
375 查看
Android手机里也可以通过程序进行屏幕亮度的调节,而这种操作往往就是通过SeekBar组件实现的,而要想实现亮度调节功能就必须android.view.Window类的screenBrightness属性实现,而此属性的取值范围是0~1
(由暗到亮)
![](http://img.blog.csdn.net/20130616173553593)
.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<SeekBar
android:id="@+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android_book"/>
<TextView
android:id="@+id/light"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
.java
package com.example.seekbardemo3adjustthelight;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private SeekBar seekbar=null;
private TextView light=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.light=(TextView) super.findViewById(R.id.light);
this.seekbar=(SeekBar) super.findViewById(R.id.seekbar);
this.seekbar.setMax(100);
this.seekbar.setOnSeekBarChangeListener(new SeekBarChangeListenerImp());
}
public class SeekBarChangeListenerImp implements OnSeekBarChangeListener{
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
int cur=seekBar.getProgress();
MainActivity.this.setScreenBrightness(cur/100);
MainActivity.this.light.setText("当前屏幕亮度:"+cur/100);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
//设置屏幕亮度的函数
private void setScreenBrightness(float num){
WindowManager.LayoutParams layoutParams=super.getWindow().getAttributes();
layoutParams.screenBrightness=num;//设置屏幕的亮度
super.getWindow().setAttributes(layoutParams);
}
}
(由暗到亮)
.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<SeekBar
android:id="@+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android_book"/>
<TextView
android:id="@+id/light"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
.java
package com.example.seekbardemo3adjustthelight;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private SeekBar seekbar=null;
private TextView light=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.light=(TextView) super.findViewById(R.id.light);
this.seekbar=(SeekBar) super.findViewById(R.id.seekbar);
this.seekbar.setMax(100);
this.seekbar.setOnSeekBarChangeListener(new SeekBarChangeListenerImp());
}
public class SeekBarChangeListenerImp implements OnSeekBarChangeListener{
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
int cur=seekBar.getProgress();
MainActivity.this.setScreenBrightness(cur/100);
MainActivity.this.light.setText("当前屏幕亮度:"+cur/100);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
//设置屏幕亮度的函数
private void setScreenBrightness(float num){
WindowManager.LayoutParams layoutParams=super.getWindow().getAttributes();
layoutParams.screenBrightness=num;//设置屏幕的亮度
super.getWindow().setAttributes(layoutParams);
}
}
相关文章推荐
- 使用SeekBar组件调节屏幕亮度
- SeekBar调节手机屏幕亮度
- Ubuntu如何使用命令调节屏幕亮度【Ubantu系列】
- Android:用Seekbar来调节屏幕亮度
- android通过seekbar调节屏幕亮度
- linux下使用软件方式调节屏幕亮度
- 联想电脑屏幕亮度无法调节
- C# 调节屏幕亮度 .
- ubuntu 11.10亮度无法调节和无法保存屏幕亮度解决办法gksu gedit /etc/default/grub
- 笔记本在Ubuntu调节屏幕亮度的技巧
- android系统屏幕亮度调节
- android调节屏幕亮度
- win8不能调节屏幕亮度解决方法
- win7系统中怎么调节屏幕亮度方法介绍
- android屏幕亮度自动调节解析
- Android_调节屏幕亮度(背景灯)及保持背景灯常亮的方法
- android调节屏幕亮度
- android 如何设置开机后屏幕亮度默认值为自动调节
- Android列表组件ListView使用详解之屏幕触摸事件
- android调节屏幕亮度