Android自定义音量控制图标
2016-02-23 10:20
387 查看
设置音量控制数组
音量控制初始化定义
音量减函数
音量加函数
/** * 使用时四舍五入 */ public static double[] SOUND_VALUE_ARRAY = { 0, 0.75, 1.50, 2.25, 3.00, 3.75, 4.50, 5.25, 6.00, 6.75, 7.50, 8.25, 9.00, 9.75, 10.50, 11.25, 12.00, 12.75, 13.50, 14.25, 15.00 }; /** * 将音量自定义为0-100,增长步长为5 * 这是对应的每个音量的图片 */ private final int[] SOUND_IMAGE_ARRAY = new int[] { R.drawable.osd_music_0, R.drawable.osd_music_5, R.drawable.osd_music_10, R.drawable.osd_music_15, R.drawable.osd_music_20, R.drawable.osd_music_25, R.drawable.osd_music_30, R.drawable.osd_music_35, R.drawable.osd_music_40, R.drawable.osd_music_45, R.drawable.osd_music_50, R.drawable.osd_music_55, R.drawable.osd_music_60, R.drawable.osd_music_65, R.drawable.osd_music_70, R.drawable.osd_music_75, R.drawable.osd_music_80, R.drawable.osd_music_85, R.drawable.osd_music_90, R.drawable.osd_music_95, R.drawable.osd_music_100 };
音量控制初始化定义
/** * 使用时四舍五入 */ AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
音量减函数
public void volumDown(){ if (mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC) > 0) { if (Math.round((float)mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC) * 4 / 3) > 0) { controlImageView.setBackgroundResource( SOUND_IMAGE_ARRAY[Math.round((float)mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3 - 1)]); } else { controlImageView.setBackgroundResource( SOUND_IMAGE_ARRAY[Math.round((float)mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]); } if ((int) Math.round(SOUND_VALUE_ARRAY[Math.round((float)mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]) > 0) { mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int) Math.round(SOUND_VALUE_ARRAY[(int) (Math.floor((float)mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3) - 1)]),0); } else { mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int) Math.round(SOUND_VALUE_ARRAY[Math.round((float)mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]),0); } } else { controlImageView.setBackgroundResource(SOUND_IMAGE_ARRAY[0]); mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0,0); } controlImageView.setVisibility(View.VISIBLE); volHandler.sendEmptyMessageDelayed(VOL_CONTROL, 2000); }
音量加函数
public void volumPu(){ if (mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM) < mAudioManager .getStreamMaxVolume(AudioManager.STREAM_MUSIC)) { if (Math.round((float)mAudioManager .getStreamVolume(AudioManager.STR 4000 EAM_MUSIC) * 4 / 3) < SOUND_IMAGE_ARRAY.length&&mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC)!=15) { controlImageView .setBackgroundResource(SOUND_IMAGE_ARRAY[Math.round((float)mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3 + 1)]); } else { controlImageView .setBackgroundResource(SOUND_IMAGE_ARRAY[Math.round((float)mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]); } if ((int) Math .round(SOUND_VALUE_ARRAY[Math.round((float)mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]) < SOUND_VALUE_ARRAY.length&&mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC)!=15) { mAudioManager .setStreamVolume( AudioManager.STREAM_MUSIC, (int) Math.round(SOUND_VALUE_ARRAY[Math.round((float)mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3) + 1]), 0); } else { mAudioManager .setStreamVolume( AudioManager.STREAM_MUSIC, (int) Math.round(SOUND_VALUE_ARRAY[Math.round((float)mAudioManager .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]), 0); } } else { controlImageView .setBackgroundResource(SOUND_IMAGE_ARRAY[Math.round((float)mAudioManager .getStreamMaxVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]); mAudioManager .setStreamVolume( AudioManager.STREAM_MUSIC, mAudioManager .getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0); } controlImageView.setVisibility(View.VISIBLE); volHandler.sendEmptyMessageDelayed(VOL_CONTROL, 2000); }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories