<1.惯例,来布局>
<?xml version="1.0" encoding="utf-8"?><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"> <SeekBar android:id="@+id/seek_bar" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/showSeek" android:textSize="20.0sp" android:gravity="center"/></LinearLayout><2.源码>
public class MainActivity extends AppCompatActivity { PRivate SeekBar seekBar; private TextView showseek; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { update((float)seekBar.getProgress()/10); } }); } //实例化控件 private void init(){ seekBar= (SeekBar) findViewById(R.id.seek_bar); showseek= (TextView) findViewById(R.id.showSeek); //亮度是从0~1中的一个浮点数,由于getProess得到的是Int型,所以扩大10倍,赋值时除以10 seekBar.setMax(10); } private void update(float b){ //取得window属性保存在layoutParams中 WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.screenBrightness = b; getWindow().setAttributes(layoutParams); //显示修改后的亮度 layoutParams = getWindow().getAttributes(); showseek.setText("当前亮度:"+String.valueOf(layoutParams.screenBrightness)); }}
新闻热点
疑难解答