菜鸟之路-Volume、Brightness调节
2015-10-23 13:58
609 查看
本人是一个刚刚入行3月的新码农,从Hello World开始的那种,现在已经被分在项目组里做项目,可是由于能力原因只能做些打杂的模块,开始的比较坎坷,还要多谢博客里各位先行者的指引,现在我也要把我的收获写下来,<span style="color:#ff0000;">如果有什么不对或者更完美的方法非常感谢能帮我提出来,在此感激不尽!!!</span>
<span style="color:#ff0000;"> </span>
我做的音量和亮度调节是用简单的用seekbar调节音量和亮度,关键的代码为:
seekbar.setMax(205);
<span style="color:#ff0000;"> </span><span style="background-color: rgb(255, 255, 255);">int currentLight = Settings.System.getInt(getContext() .getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255);</span>
seekbar.setProgress(currentLight - 50);
为seekbar绑定最大值、获取系统当前亮度,然后将seekbar设置为当前亮度。
AudioManager mMgr = (AudioManager) getContext().getSystemService(getContext().AUDIO_SERVICE);
int currentMusic = mMgr.getStreamVolume(AudioManager.STREAM_MUSIC);
seekbar.setMax(15);
seekbar.setProgress(currentMusic);
原理同上但获取音量需要getSystemService(getContext().AUDIO_SERVICE)来调用系统。
下面上代码
首先是亮度:
seekbar.setMax(205); int currentLight = Settings.System.getInt(getContext()
.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255);
seekbar.setProgress(currentLight - 50);
seekbar
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
int tmpInt = seekBar.getProgress() + 50;
Settings.System.putInt(getContext()
.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, tmpInt);
tmpInt = Settings.System.getInt(getContext()
.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, -1);
WindowManager.LayoutParams wl = ((Activity) getContext())
.getWindow().getAttributes();
float tmpFloat = (float) tmpInt / 255; if (tmpFloat > 0 && tmpFloat <= 1) { wl.screenBrightness = tmpFloat; } ((Activity) getContext()).getWindow().setAttributes(wl); } });
然后是音量<pre name="code" class="html">final AudioManager mMgr = (AudioManager) getContext().getSystemService( getContext().AUDIO_SERVICE); int currentMusic = mMgr.getStreamVolume(AudioManager.STREAM_MUSIC); seekbar.setMax(15); seekbar.setProgress(currentMusic); seekbar .setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { mMgr.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
//这里有个特殊,<span style="font-family: Arial, Helvetica, sans-serif;">STREAM_VOICE_CALL指的是电话音量,他和其他音量的最大值不同所以进行一下运算</span> mMgr.setStreamVolume(AudioManager.STREAM_VOICE_CALL, (int)(progress*2.5), 0); mMgr.setStreamVolume(AudioManager.STREAM_SYSTEM, progress, 0); mMgr.setStreamVolume(AudioManager.STREAM_RING, progress, 0); mMgr.setStreamVolume(AudioManager.STREAM_ALARM, progress, 0); mMgr.setStreamVolume(AudioManager.STREAM_NOTIFICATION, progress, 0); mMgr.setStreamVolume(AudioManager.STREAM_DTMF, progress, 0); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } });这样就好了~~~
相关文章推荐
- android Setting中隐藏项实现原理与代码
- bash 启动时候每次一条tips
- PM日记:小试1 中午时光
- 码农 黑客和2B程序员之间的区别
- Docker如何管理数据
- System Volume Information完全可以删除
- openstack Essex中nova-volume
- nova-volume指令
- 删除 OpenStack Nova Volume 时遇到的 error_deleting 问题
- cmd fsutil 命令 - 创建指定大小文件命令
- esxi不能挂盘
- WIN7/WIN2008重置用户Profile
- 综合操作实例(跨系统的卷组迁移)
- 30岁,谁来衡量我们的价值
- 关于System Volume Information占用的大量磁盘空间
- LVM的安装和使用方法
- LVM
- dreamweaver cs6怎么设置背景音乐有哪些方式
- linux lvm扩容