Android移动开发-音量调节对话框的实现
来源:互联网 发布:电脑软件开发学校 编辑:程序博客网 时间:2024/06/06 02:38
Android手机上的需要音量调节有六大类铃音,分别是通话音、系统音、铃音、媒体音、闹钟音和通知音,不过音量键只有加大与减少两个键,当用户按音量增加键时,应用怎么知道用户希望加大那类铃音的音量呢?
要解决这个问题,最好是弹出一个对话框,让用户选择希望调节的铃音类型,并显示拖动条,方便用户把音量一次调整到位,不必连续按增加键或减少键。
- layout/dialog_volume.xml界面布局代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/transparent" android:gravity="center" android:orientation="vertical" android:paddingLeft="40dp" android:paddingRight="40dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical"> <SeekBar android:id="@+id/sb_music" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:max="100" android:thumb="@drawable/seekbar_point" /> </LinearLayout></LinearLayout>
- widget.VolumeDialog.java.java逻辑代码如下:
package com.fukaimei.keyevent.widget;import android.app.Dialog;import android.content.Context;import android.media.AudioManager;import android.os.Handler;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnKeyListener;import android.view.ViewGroup.LayoutParams;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import com.fukaimei.keyevent.R;public class VolumeDialog implements OnSeekBarChangeListener, OnKeyListener { private Dialog dialog; private View view; private SeekBar sb_music; private AudioManager mAudioMgr; private int MUSIC = AudioManager.STREAM_MUSIC; private int mMaxVolume; private int mNowVolume; public VolumeDialog(Context context) { mAudioMgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); mMaxVolume = mAudioMgr.getStreamMaxVolume(MUSIC); mNowVolume = mAudioMgr.getStreamVolume(MUSIC); view = LayoutInflater.from(context).inflate(R.layout.dialog_volume, null); dialog = new Dialog(context, R.style.VolumeDialog); sb_music = (SeekBar) view.findViewById(R.id.sb_music); sb_music.setOnSeekBarChangeListener(this); sb_music.setProgress(sb_music.getMax() * mNowVolume / mMaxVolume); } public void show() { dialog.getWindow().setContentView(view); dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); dialog.show(); sb_music.setFocusable(true); sb_music.setFocusableInTouchMode(true); sb_music.setOnKeyListener(this); } public void dismiss() { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } } public boolean isShowing() { if (dialog != null) { return dialog.isShowing(); } else { return false; } } public void adjustVolume(int direction, boolean fromActivity) { if (direction == AudioManager.ADJUST_RAISE) { mNowVolume += 1; } else { mNowVolume -= 1; } sb_music.setProgress(sb_music.getMax() * mNowVolume / mMaxVolume); mAudioMgr.adjustStreamVolume(MUSIC, direction, AudioManager.FLAG_PLAY_SOUND); if (mListener != null && fromActivity != true) { mListener.onVolumeAdjust(mNowVolume); } close(); } private void close() { mHandler.removeCallbacks(mClose); mHandler.postDelayed(mClose, 2000); } private Handler mHandler = new Handler(); private Runnable mClose = new Runnable() { @Override public void run() { dismiss(); } }; @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { mNowVolume = mMaxVolume * seekBar.getProgress() / seekBar.getMax(); mAudioMgr.setStreamVolume(MUSIC, mNowVolume, AudioManager.FLAG_PLAY_SOUND); if (mListener != null) { mListener.onVolumeAdjust(mNowVolume); } close(); } @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getAction() == KeyEvent.ACTION_DOWN) { adjustVolume(AudioManager.ADJUST_RAISE, false); return true; } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && event.getAction() == KeyEvent.ACTION_DOWN) { adjustVolume(AudioManager.ADJUST_LOWER, false); return true; } else { return false; } } private VolumeAdjustListener mListener; public void setVolumeAdjustListener(VolumeAdjustListener listener) { mListener = listener; } public static interface VolumeAdjustListener { public abstract void onVolumeAdjust(int volume); }}
- layout/activity_volume_set.xml界面布局代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <TextView android:id="@+id/tv_volume" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按下加减键调整音乐音量" android:textColor="@color/black" android:textSize="17sp" /></LinearLayout>
- VolumeSetActivity.java逻辑代码如下:
package com.fukaimei.keyevent;import android.content.Context;import android.media.AudioManager;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.KeyEvent;import android.widget.TextView;import com.fukaimei.keyevent.widget.VolumeDialog;public class VolumeSetActivity extends AppCompatActivity implements VolumeDialog.VolumeAdjustListener { private TextView tv_volume; private VolumeDialog dialog; private AudioManager mAudioMgr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_volume_set); tv_volume = (TextView) findViewById(R.id.tv_volume); mAudioMgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getAction() == KeyEvent.ACTION_DOWN) { showVolumeDialog(AudioManager.ADJUST_RAISE); return true; } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && event.getAction() == KeyEvent.ACTION_DOWN) { showVolumeDialog(AudioManager.ADJUST_LOWER); return true; } else if (keyCode == KeyEvent.KEYCODE_BACK) { finish(); return false; } else { return false; } } private void showVolumeDialog(int direction) { if (dialog == null || dialog.isShowing() != true) { dialog = new VolumeDialog(this); dialog.setVolumeAdjustListener(this); dialog.show(); } dialog.adjustVolume(direction, true); onVolumeAdjust(mAudioMgr.getStreamVolume(AudioManager.STREAM_MUSIC)); } @Override public void onVolumeAdjust(int volume) { tv_volume.setText("调节后的音乐音量大小为:" + volume); }}
阅读全文
0 0
- Android移动开发-音量调节对话框的实现
- Android音量调节的实现
- android 实现音量调节
- Android实现音量调节的方法
- WINCE音量调节的实现
- WINCE音量调节的实现
- WINCE音量调节的实现
- WINCE音量调节的实现
- 【Android界面实现】自定义音量调节控件的实现
- Android里的音量调节
- Android音量调节的实现(RingtoneManager和RingerVolumePreference)
- Android音量调节的实现(RingtoneManager和RingerVolumePreference) .
- Android音量调节的实现(RingtoneManager和RingerVolumePreference)
- Android音量调节的实现(RingtoneManager和RingerVolumePreference)
- android 的 c层 实现 音乐播放与音量调节
- Android音量调节的实现(RingtoneManager和RingerVolumePreference)
- Android音量调节的实现(RingtoneManager和RingerVolumePreference)
- android 开发Seekbar 调节媒体音量例子
- 717. 1-bit and 2-bit Characters(第九周)
- 《Deep Learning》学习笔记一: BN的理解。
- 区域赛总结
- [LeetCode]566. Reshape the Matrix
- Elastic-Job-Lite 源码阅读 ---- 任务初始化
- Android移动开发-音量调节对话框的实现
- APP测试的认识
- html文件基本结构
- LVM(ext4+xfs的扩容和缩容)
- STM32GPIO口8种模式细致分析(类比51单片机)
- 字符串匹配_2
- 并发新特性—Lock锁和条件变量
- 分布式监控中心设计方案
- Java运行不了zookeeper可视化工具jar文件问题解决