Skip to content

Instantly share code, notes, and snippets.

@xxxzhi
Created October 30, 2013 08:53
Show Gist options
  • Save xxxzhi/7229247 to your computer and use it in GitHub Desktop.
Save xxxzhi/7229247 to your computer and use it in GitHub Desktop.
监听ImageView 实现按住弹出popwindow 说话,可设置上滑还是下滑取消说话
public interface AudioOpeateIterface{
public void setPlayCompletionListener(MediaPlayer.OnCompletionListener listener);
public void playRecord(String src);
public void startRecord();
public String stopRecord();
public void deleteRecord();
public void stopPlay();
public void playRecord();
}
/**
* @author houzhi
* 监听ImageView 实现按住弹出popwindow 说话,可设置上滑还是下滑取消说话
*
* 1.down 的时候更换背景
* 2 move 的时候 进行位置判断,超出了,提示取消之类的
* 3 up 判断是否被取消
*/
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Build;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import com.szl.tmfinal.R;
import com.szl.tmfinal.task.AudioActivityOperate;
import com.szl.tmfinal.task.AudioService.AudioOpeateIterface;
import com.szl.tmfinal.task.Constants;
import com.szl.tmfinal.task.MyLog;
public class VoiceTouchListener implements View.OnTouchListener{
public interface OnRecordEndListener{
void onRecordEndListener(String voiceUrl);
}
private OnRecordEndListener mEndListener = null;
public void setOnRecordEndListener(OnRecordEndListener endListener){
this.mEndListener = endListener;
}
private AudioOpeateIterface audioOperate = null;
final ImageView img;
private PopupWindow popupWindow;
Activity activity;
ImageButton voice;
ViewGroup parent;
/**
* 上滑
*/
private boolean isUp= false;
public void setUp(){
isUp = true;
}
private int initImgId = R.drawable.bg_voice;
public void setInitImgId(int initImgId) {
this.initImgId = initImgId;
}
public VoiceTouchListener(ImageButton voice,Activity activity,ViewGroup parent,AudioOpeateIterface audioOperate){
this.voice = voice;
this.activity = activity;
this.parent = parent;
this.audioOperate = audioOperate;
View popView = activity.getLayoutInflater().inflate(R.layout.pop_voice, null);
img = (ImageView) popView.findViewById(R.id.image);
popupWindow = new PopupWindow(
popView);
Resources resources = activity.getResources();
popupWindow.setWidth(resources.getDimensionPixelOffset(R.dimen.voice_hint_side));
popupWindow.setHeight(resources.getDimensionPixelOffset(R.dimen.voice_hint_side));
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
// TODO Auto-generated method stub
MyLog.i("", "dismiss");
}
});
}
final int SIGH_LONG_CLICK_= 500;
public static final float SIGN_CANCLE_DIS = 100;
long start = 0;
float startY = 0;
boolean isCancel = false;
private RelativeLayout.LayoutParams layoutParams;
private VoiceAsyncTask task = null;
class VoiceAsyncTask extends AsyncTask<Void, Integer, Void>{
private int imgId[] = new int[]{R.drawable.img_voice_hint1,R.drawable.img_voice_hint2,R.drawable.img_voice_hint3,
R.drawable.img_voice_hint4,R.drawable.img_voice_hint5};
private int imgIdUp[] = new int []{R.drawable.img_voice_hint_up1,R.drawable.img_voice_hint_up2,R.drawable.img_voice_hint_up3,
R.drawable.img_voice_hint_up4,R.drawable.img_voice_hint_up5};
private ImageView img ;
boolean isRunOk = false;
public VoiceAsyncTask(ImageView img){
this.img = img;
}
boolean iscancel = false;
public void cancel(){
iscancel = true;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
for(int i=0;i!=5;++i){
if(iscancel)
break;
try {
TimeUnit.MILLISECONDS.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
publishProgress(i);
if(i == 4){i = 0;}
}
isRunOk = true;
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
if(values.length>0){
//录音画面动画
if(isUp){
img.setImageResource(imgIdUp[values[0]]);
}else
img.setImageResource(imgId[values[0]]);
}
super.onProgressUpdate(values);
}
}
boolean viewChange = true;
public void noChangeWhileClick(){
viewChange = false;
}
void changeOtherViewVisible(int sign){
if(!viewChange)
return ;
for(int i=0;i!=parent.getChildCount();++i){
View child = parent.getChildAt(i);
if(child!=voice){
//隐藏
child.setVisibility(sign);
}else{
}
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if( !popupWindow.isShowing()){
//打开popwindow
popupWindow.showAtLocation(voice, Gravity.CENTER, 0, 0);
// popupWindow.showAsDropDown(voice);
//开始启动录音
audioOperate.startRecord();
}
changeOtherViewVisible(View.GONE);
layoutParams = (RelativeLayout.LayoutParams) voice.getLayoutParams();
if(viewChange){
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(layoutParams );
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.leftMargin = layoutParams.leftMargin;
params.rightMargin = layoutParams.rightMargin;
voice.setImageResource(R.drawable.bg_voice_back);
voice.setLayoutParams(params);
}
if(task!=null){
task.cancel();
}
task = new VoiceAsyncTask(img);
// 启动异步任务,加载录音画面
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(Constants.FULL_TASK_EXECUR);
} else {
task.execute();
}
start = event.getDownTime();
startY = event.getRawY();
isCancel = false;
break;
case MotionEvent.ACTION_MOVE:
if(Math.abs(event.getRawY() - startY)>SIGN_CANCLE_DIS&&popupWindow.isShowing()){
//下滑超过制定距离 表示取消
img.setImageResource(R.drawable.img_voice_hint_cancel);
isCancel = true;
}else{
isCancel = false;
}
break;
case MotionEvent.ACTION_UP:
//获取
popupWindow.dismiss();
voice.setImageResource(initImgId);
voice.setLayoutParams(layoutParams);
changeOtherViewVisible(View.VISIBLE);
if(isCancel){
//取消 录音
audioOperate.stopRecord();
audioOperate.deleteRecord();
}else{
//不取消 录音
//录音结束
if(mEndListener!=null){
mEndListener.onRecordEndListener(audioOperate.stopRecord());
}
}
break;
default:
break;
}
if(isCancel){
task.cancel();
}else{
//手指下滑,取消录入
if(task.isRunOk)
img.setImageResource(R.drawable.img_voice_hint5);
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment