Android仿ios录音动画
2015-10-23 13:53
441 查看
太懒了,以前没有记录过开发用到的技术,时间久了发现脑子很乱,会什么接触过什么都无法想起,所以。。。
开整
1.先定义所需动画以dialog形式管理
2.自定义Button对其按下移动弹起事件进行管理
3.调用系统的MediaRecorder实现录音功能
只列出部分源码,下有源码地址
public class VideoButton extends Button {
Context context;
private VedioDialog dialog;//语音动画dialog
private Float onKeyDown,onKeyMove;//按下时坐标,移动后坐标
private long onKeyDownTime,OnKeyUpTime,recordTime;//按下时间,弹起时间,录音时间
private AudioRecorder mAudioRecorder;//实例录音
private boolean IsRecordOn = false;//是否在录音
private boolean IsCancle = false;//是否取消录音
private RecordListener listener;
private String path;
public VideoButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(context);
}
public VideoButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init(context);
}
private void init(Context context) {
this.context = context;
this.setText(R.string.video_hold_speak);
dialog = new VedioDialog(context);
dialog.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
((VedioDialog) dialog).reInit();
}
});
}
public void setAudioRecord(AudioRecorder record) {
this.mAudioRecorder = record;
}
public void setRecordListener(RecordListener listener) {
this.listener = listener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
onActionDown(event);//按下事件处理
break;
case MotionEvent.ACTION_MOVE:
onActionMove(event);
break;
case MotionEvent.ACTION_UP:
onActionUp(event);
break;
case MotionEvent.ACTION_CANCEL:
recordFailure();
break;
default:
recordFailure();
break;
}
return super.onTouchEvent(event);
}
/**
* @Title: onActionUp
* @Description: 弹起时间处理
* @param event
* @return: void
*/
private void onActionUp(MotionEvent event) {
if (IsRecordOn) {
IsRecordOn = false;
OnKeyUpTime = System.currentTimeMillis();
this.setText(R.string.video_hold_speak);
}
}
/**
* @Title: onActionMove
* @Description: 按下后移动事件处理
* @param event
* @return: void
*/
private void onActionMove(MotionEvent event) {
// TODO Auto-generated method stub
onKeyMove = event.getY();
if (onKeyDown-onKeyMove>50) {
dialog.onMoveUp();
IsCancle = true;
}
if (onKeyDown-onKeyMove<20) {
dialog.onNoMoveUp();
IsCancle = false;
}
}
/**
* @Title: onActionDown
* @Description: 按下事件处理
* @param event
* @return: void
*/
private void onActionDown(MotionEvent event) {
// TODO Auto-generated method stub
setText(R.string.video_hold_up);
onKeyDown = event.getY();
handler.sendEmptyMessageDelayed(1001, 200);
onKeyDownTime = System.currentTimeMillis();
if (mAudioRecorder!=null) {
dialog.show();
IsRecordOn = true;
Record record = new Record();
record.execute();
}
}
//动画延迟消失
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1000:
dialog.dismiss();
break;
default:
break;
}
};};
/**
* @Title: recordFailure
* @Description: 录音失败
* @return: void
*/
private void recordFailure(){
if (dialog!=null) {
dialog.dismiss();
}
if (mAudioRecorder!=null&&IsRecordOn) {
IsRecordOn=false;
recordTime = 0;
path = null;
mAudioRecorder.deleteOldFile(path);
}
}
public interface RecordListener {
public void recordEnd(String filePath,long recordTime);
}
class Record extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... params) {
try {
mAudioRecorder.ready();
mAudioRecorder.start();
while (IsRecordOn) {
}
mAudioRecorder.stop();
} catch (Exception e) {
// TODO: handle exception
Log.w("Audio", "doInBackground", e);
}
return mAudioRecorder.getFilePath();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
path = result;
if (IsCancle) {
recordTime = 0;
mAudioRecorder.deleteOldFile(path);
path = null;
dialog.dismiss();
}else {
if (OnKeyUpTime-onKeyDownTime<1000) {
dialog.onTimeLitter();
mAudioRecorder.deleteOldFile(path);
handler.sendEmptyMessageDelayed(1000, 300);
}else{
recordTime = OnKeyUpTime-onKeyDownTime;
if (listener!=null) {
listener.recordEnd(path,recordTime);
}
dialog.dismiss();
}
}
IsCancle = false;
}
}
}
源码地址:http://download.csdn.net/detail/clare_ju/9206379
开整
1.先定义所需动画以dialog形式管理
2.自定义Button对其按下移动弹起事件进行管理
3.调用系统的MediaRecorder实现录音功能
只列出部分源码,下有源码地址
public class VideoButton extends Button {
Context context;
private VedioDialog dialog;//语音动画dialog
private Float onKeyDown,onKeyMove;//按下时坐标,移动后坐标
private long onKeyDownTime,OnKeyUpTime,recordTime;//按下时间,弹起时间,录音时间
private AudioRecorder mAudioRecorder;//实例录音
private boolean IsRecordOn = false;//是否在录音
private boolean IsCancle = false;//是否取消录音
private RecordListener listener;
private String path;
public VideoButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(context);
}
public VideoButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init(context);
}
private void init(Context context) {
this.context = context;
this.setText(R.string.video_hold_speak);
dialog = new VedioDialog(context);
dialog.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
((VedioDialog) dialog).reInit();
}
});
}
public void setAudioRecord(AudioRecorder record) {
this.mAudioRecorder = record;
}
public void setRecordListener(RecordListener listener) {
this.listener = listener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
onActionDown(event);//按下事件处理
break;
case MotionEvent.ACTION_MOVE:
onActionMove(event);
break;
case MotionEvent.ACTION_UP:
onActionUp(event);
break;
case MotionEvent.ACTION_CANCEL:
recordFailure();
break;
default:
recordFailure();
break;
}
return super.onTouchEvent(event);
}
/**
* @Title: onActionUp
* @Description: 弹起时间处理
* @param event
* @return: void
*/
private void onActionUp(MotionEvent event) {
if (IsRecordOn) {
IsRecordOn = false;
OnKeyUpTime = System.currentTimeMillis();
this.setText(R.string.video_hold_speak);
}
}
/**
* @Title: onActionMove
* @Description: 按下后移动事件处理
* @param event
* @return: void
*/
private void onActionMove(MotionEvent event) {
// TODO Auto-generated method stub
onKeyMove = event.getY();
if (onKeyDown-onKeyMove>50) {
dialog.onMoveUp();
IsCancle = true;
}
if (onKeyDown-onKeyMove<20) {
dialog.onNoMoveUp();
IsCancle = false;
}
}
/**
* @Title: onActionDown
* @Description: 按下事件处理
* @param event
* @return: void
*/
private void onActionDown(MotionEvent event) {
// TODO Auto-generated method stub
setText(R.string.video_hold_up);
onKeyDown = event.getY();
handler.sendEmptyMessageDelayed(1001, 200);
onKeyDownTime = System.currentTimeMillis();
if (mAudioRecorder!=null) {
dialog.show();
IsRecordOn = true;
Record record = new Record();
record.execute();
}
}
//动画延迟消失
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1000:
dialog.dismiss();
break;
default:
break;
}
};};
/**
* @Title: recordFailure
* @Description: 录音失败
* @return: void
*/
private void recordFailure(){
if (dialog!=null) {
dialog.dismiss();
}
if (mAudioRecorder!=null&&IsRecordOn) {
IsRecordOn=false;
recordTime = 0;
path = null;
mAudioRecorder.deleteOldFile(path);
}
}
public interface RecordListener {
public void recordEnd(String filePath,long recordTime);
}
class Record extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... params) {
try {
mAudioRecorder.ready();
mAudioRecorder.start();
while (IsRecordOn) {
}
mAudioRecorder.stop();
} catch (Exception e) {
// TODO: handle exception
Log.w("Audio", "doInBackground", e);
}
return mAudioRecorder.getFilePath();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
path = result;
if (IsCancle) {
recordTime = 0;
mAudioRecorder.deleteOldFile(path);
path = null;
dialog.dismiss();
}else {
if (OnKeyUpTime-onKeyDownTime<1000) {
dialog.onTimeLitter();
mAudioRecorder.deleteOldFile(path);
handler.sendEmptyMessageDelayed(1000, 300);
}else{
recordTime = OnKeyUpTime-onKeyDownTime;
if (listener!=null) {
listener.recordEnd(path,recordTime);
}
dialog.dismiss();
}
}
IsCancle = false;
}
}
}
源码地址:http://download.csdn.net/detail/clare_ju/9206379
相关文章推荐
- Gifski:一个跨平台的高质量 GIF 编码器
- 模仿动画的放大缩小容器
- Android中ViewFlipper的使用及设置动画效果实例详解
- jQuery实现美观的多级动画效果菜单代码
- php判断GIF图片是否为动画的方法
- jQuery实现动画效果circle实例
- 浅析JavaScript动画
- js排序动画模拟-插入排序
- javascript+HTML5的Canvas实现Lab单车动画效果
- js实现按钮颜色渐变动画效果
- jQuery实现连续动画效果实例分析
- jQuery实现带有洗牌效果的动画分页实例
- jquery实现先淡出再折叠收起的动画效果
- jQuery实现有动画淡出效果的二级折叠菜单代码
- jQuery使用animate创建动画用法实例
- jquery带动画效果幻灯片特效代码
- jQuery实现带动画效果的多级下拉菜单代码
- jQuery动画特效实例教程
- JQuery动画和停止动画实例代码
- 基于jquery css3实现点击动画弹出表单源码特效