实例介绍
【实例截图】

【核心代码】
package com.example.voice_rcd;
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Button mBtnSend;
private TextView mBtnRcd;
private Button mBtnBack;
private EditText mEditTextContent;
private RelativeLayout mBottom;
private ListView mListView;
private ChatMsgViewAdapter mAdapter;
private List<ChatMsgEntity> mDataArrays = new ArrayList<ChatMsgEntity>();
private boolean isShosrt = false;
private LinearLayout voice_rcd_hint_loading, voice_rcd_hint_rcding,
voice_rcd_hint_tooshort;
private ImageView img1, sc_img1;
private SoundMeter mSensor;
private View rcChat_popup;
private LinearLayout del_re;
private ImageView chatting_mode_btn, volume;
private boolean btn_vocie = false;
private int flag = 1;
private Handler mHandler = new Handler();
private String voiceName;
private long startVoiceT, endVoiceT;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat);
// 启动activity时不自动弹出软键盘
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
initView();
initData();
}
public void initView() {
mListView = (ListView) findViewById(R.id.listview);
mBtnSend = (Button) findViewById(R.id.btn_send);
mBtnRcd = (TextView) findViewById(R.id.btn_rcd);
mBtnSend.setOnClickListener(this);
mBtnBack = (Button) findViewById(R.id.btn_back);
mBottom = (RelativeLayout) findViewById(R.id.btn_bottom);
mBtnBack.setOnClickListener(this);
chatting_mode_btn = (ImageView) this.findViewById(R.id.ivPopUp);
volume = (ImageView) this.findViewById(R.id.volume);
rcChat_popup = this.findViewById(R.id.rcChat_popup);
img1 = (ImageView) this.findViewById(R.id.img1);
sc_img1 = (ImageView) this.findViewById(R.id.sc_img1);
del_re = (LinearLayout) this.findViewById(R.id.del_re);
voice_rcd_hint_rcding = (LinearLayout) this
.findViewById(R.id.voice_rcd_hint_rcding);
voice_rcd_hint_loading = (LinearLayout) this
.findViewById(R.id.voice_rcd_hint_loading);
voice_rcd_hint_tooshort = (LinearLayout) this
.findViewById(R.id.voice_rcd_hint_tooshort);
mSensor = new SoundMeter();
mEditTextContent = (EditText) findViewById(R.id.et_sendmessage);
//语音文字切换按钮
chatting_mode_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (btn_vocie) {
mBtnRcd.setVisibility(View.GONE);
mBottom.setVisibility(View.VISIBLE);
btn_vocie = false;
chatting_mode_btn
.setImageResource(R.drawable.chatting_setmode_msg_btn);
} else {
mBtnRcd.setVisibility(View.VISIBLE);
mBottom.setVisibility(View.GONE);
chatting_mode_btn
.setImageResource(R.drawable.chatting_setmode_voice_btn);
btn_vocie = true;
}
}
});
mBtnRcd.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
//按下语音录制按钮时返回false执行父类OnTouch
return false;
}
});
}
private String[] msgArray = new String[] { "有人就有恩怨","有恩怨就有江湖","人就是江湖","你怎么退出? ","生命中充满了巧合","两条平行线也会有相交的一天。"};
private String[] dataArray = new String[] { "2012-10-31 18:00",
"2012-10-31 18:10", "2012-10-31 18:11", "2012-10-31 18:20",
"2012-10-31 18:30", "2012-10-31 18:35"};
private final static int COUNT = 6;
public void initData() {
for (int i = 0; i < COUNT; i ) {
ChatMsgEntity entity = new ChatMsgEntity();
entity.setDate(dataArray[i]);
if (i % 2 == 0) {
entity.setName("白富美");
entity.setMsgType(true);
} else {
entity.setName("高富帅");
entity.setMsgType(false);
}
entity.setText(msgArray[i]);
mDataArrays.add(entity);
}
mAdapter = new ChatMsgViewAdapter(this, mDataArrays);
mListView.setAdapter(mAdapter);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_send:
send();
break;
case R.id.btn_back:
finish();
break;
}
}
private void send() {
String contString = mEditTextContent.getText().toString();
if (contString.length() > 0) {
ChatMsgEntity entity = new ChatMsgEntity();
entity.setDate(getDate());
entity.setName("高富帅");
entity.setMsgType(false);
entity.setText(contString);
mDataArrays.add(entity);
mAdapter.notifyDataSetChanged();
mEditTextContent.setText("");
mListView.setSelection(mListView.getCount() - 1);
}
}
private String getDate() {
Calendar c = Calendar.getInstance();
String year = String.valueOf(c.get(Calendar.YEAR));
String month = String.valueOf(c.get(Calendar.MONTH));
String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH) 1);
String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
String mins = String.valueOf(c.get(Calendar.MINUTE));
StringBuffer sbBuffer = new StringBuffer();
sbBuffer.append(year "-" month "-" day " " hour ":"
mins);
return sbBuffer.toString();
}
//按下语音录制按钮时
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!Environment.getExternalStorageDirectory().exists()) {
Toast.makeText(this, "No SDCard", Toast.LENGTH_LONG).show();
return false;
}
if (btn_vocie) {
System.out.println("1");
int[] location = new int[2];
mBtnRcd.getLocationInWindow(location); // 获取在当前窗口内的绝对坐标
int btn_rc_Y = location[1];
int btn_rc_X = location[0];
int[] del_location = new int[2];
del_re.getLocationInWindow(del_location);
int del_Y = del_location[1];
int del_x = del_location[0];
if (event.getAction() == MotionEvent.ACTION_DOWN && flag == 1) {
if (!Environment.getExternalStorageDirectory().exists()) {
Toast.makeText(this, "No SDCard", Toast.LENGTH_LONG).show();
return false;
}
System.out.println("2");
if (event.getY() > btn_rc_Y && event.getX() > btn_rc_X) {//判断手势按下的位置是否是语音录制按钮的范围内
System.out.println("3");
mBtnRcd.setBackgroundResource(R.drawable.voice_rcd_btn_pressed);
rcChat_popup.setVisibility(View.VISIBLE);
voice_rcd_hint_loading.setVisibility(View.VISIBLE);
voice_rcd_hint_rcding.setVisibility(View.GONE);
voice_rcd_hint_tooshort.setVisibility(View.GONE);
mHandler.postDelayed(new Runnable() {
public void run() {
if (!isShosrt) {
voice_rcd_hint_loading.setVisibility(View.GONE);
voice_rcd_hint_rcding
.setVisibility(View.VISIBLE);
}
}
}, 300);
img1.setVisibility(View.VISIBLE);
del_re.setVisibility(View.GONE);
startVoiceT = SystemClock.currentThreadTimeMillis();
voiceName = startVoiceT ".amr";
start(voiceName);
flag = 2;
}
} else if (event.getAction() == MotionEvent.ACTION_UP && flag == 2) {//松开手势时执行录制完成
System.out.println("4");
mBtnRcd.setBackgroundResource(R.drawable.voice_rcd_btn_nor);
if (event.getY() >= del_Y
&& event.getY() <= del_Y del_re.getHeight()
&& event.getX() >= del_x
&& event.getX() <= del_x del_re.getWidth()) {
rcChat_popup.setVisibility(View.GONE);
img1.setVisibility(View.VISIBLE);
del_re.setVisibility(View.GONE);
stop();
flag = 1;
File file = new File(android.os.Environment.getExternalStorageDirectory() "/"
voiceName);
if (file.exists()) {
file.delete();
}
} else {
voice_rcd_hint_rcding.setVisibility(View.GONE);
stop();
endVoiceT = SystemClock.currentThreadTimeMillis();
flag = 1;
int time = (int) ((endVoiceT - startVoiceT) / 1000);
if (time < 1) {
isShosrt = true;
voice_rcd_hint_loading.setVisibility(View.GONE);
voice_rcd_hint_rcding.setVisibility(View.GONE);
voice_rcd_hint_tooshort.setVisibility(View.VISIBLE);
mHandler.postDelayed(new Runnable() {
public void run() {
voice_rcd_hint_tooshort
.setVisibility(View.GONE);
rcChat_popup.setVisibility(View.GONE);
isShosrt = false;
}
}, 500);
return false;
}
ChatMsgEntity entity = new ChatMsgEntity();
entity.setDate(getDate());
entity.setName("高富帅");
entity.setMsgType(false);
entity.setTime(time "\"");
entity.setText(voiceName);
mDataArrays.add(entity);
mAdapter.notifyDataSetChanged();
mListView.setSelection(mListView.getCount() - 1);
rcChat_popup.setVisibility(View.GONE);
}
}
if (event.getY() < btn_rc_Y) {//手势按下的位置不在语音录制按钮的范围内
System.out.println("5");
Animation mLitteAnimation = AnimationUtils.loadAnimation(this,
R.anim.cancel_rc);
Animation mBigAnimation = AnimationUtils.loadAnimation(this,
R.anim.cancel_rc2);
img1.setVisibility(View.GONE);
del_re.setVisibility(View.VISIBLE);
del_re.setBackgroundResource(R.drawable.voice_rcd_cancel_bg);
if (event.getY() >= del_Y
&& event.getY() <= del_Y del_re.getHeight()
&& event.getX() >= del_x
&& event.getX() <= del_x del_re.getWidth()) {
del_re.setBackgroundResource(R.drawable.voice_rcd_cancel_bg_focused);
sc_img1.startAnimation(mLitteAnimation);
sc_img1.startAnimation(mBigAnimation);
}
} else {
img1.setVisibility(View.VISIBLE);
del_re.setVisibility(View.GONE);
del_re.setBackgroundResource(0);
}
}
return super.onTouchEvent(event);
}
private static final int POLL_INTERVAL = 300;
private Runnable mSleepTask = new Runnable() {
public void run() {
stop();
}
};
private Runnable mPollTask = new Runnable() {
public void run() {
double amp = mSensor.getAmplitude();
updateDisplay(amp);
mHandler.postDelayed(mPollTask, POLL_INTERVAL);
}
};
private void start(String name) {
mSensor.start(name);
mHandler.postDelayed(mPollTask, POLL_INTERVAL);
}
private void stop() {
mHandler.removeCallbacks(mSleepTask);
mHandler.removeCallbacks(mPollTask);
mSensor.stop();
volume.setImageResource(R.drawable.amp1);
}
private void updateDisplay(double signalEMA) {
switch ((int) signalEMA) {
case 0:
case 1:
volume.setImageResource(R.drawable.amp1);
break;
case 2:
case 3:
volume.setImageResource(R.drawable.amp2);
break;
case 4:
case 5:
volume.setImageResource(R.drawable.amp3);
break;
case 6:
case 7:
volume.setImageResource(R.drawable.amp4);
break;
case 8:
case 9:
volume.setImageResource(R.drawable.amp5);
break;
case 10:
case 11:
volume.setImageResource(R.drawable.amp6);
break;
default:
volume.setImageResource(R.drawable.amp7);
break;
}
}
public void head_xiaohei(View v) { // 标题栏 返回按钮
}
}
标签: weixin
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论