1. 程式人生 > >Android仿ios錄音動畫

Android仿ios錄音動畫

太懶了,以前沒有記錄過開發用到的技術,時間久了發現腦子很亂,會什麼接觸過什麼都無法想起,所以。。。

開整

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