您的位置:首页 > 移动开发 > Android开发

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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android