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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  动画