Android仿微信发送语音消息动态提示,支持上滑取消发送
2017-12-15 09:21
561 查看
Android仿微信发送语音消息动态提示,支持上滑取消发送
先来几张图说明一下,简单直接:是不是看了图片就秒懂 了。
下面来分析代码实现,直接撸代码。
主页面 AudioSendActivity.java
package com.example.main; import android.app.Activity; import android.app.AlertDialog; import android.graphics.Color; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; import android.widget.TextView; import com.example.test.R; public class AudioSendActivity extends Activity implements OnTouchListener{ private AlertDialog mAudioDialog; private TextView mAudioNotify; private ImageView mVoiceState; private TextView mAudioSend; private boolean mCancelSend; private final String TAG = "AudioSendActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_audiosend); mAudioSend = (TextView) findViewById(R.id.audio_send); mAudioSend.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { if (v.getId() == R.id.audio_send) { float startY = 0; float endY = 0; boolean send = false; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startY = event.getY(); Log.d(TAG, "audioButtonDown() MotionEvent.ACTION_DOWN"); showAudioDialog(); break; case MotionEvent.ACTION_UP: endY = event.getY(); hideAudioDialog(); send = true; break; case MotionEvent.ACTION_MOVE: float moveY = event.getY(); int instance = (int) Math.abs((moveY - startY)); Log.d(TAG, "--action move--instance:"+instance); if (instance > 100) { changeAudioDialogCancel(true); } else { changeAudioDialogCancel(false); } break; default: break; } return true; } return false; } public void showAudioDialog() { if (mAudioDialog == null) { mAudioDialog = new AlertDialog.Builder(this).create(); mAudioDialog.show(); mAudioDialog.getWindow().setContentView(R.layout.audio_dialog); mAudioDialog.getWindow().setGravity(Gravity.CENTER); mAudioNotify = (TextView) mAudioDialog .findViewById(R.id.audio_nofity); mVoiceState = (ImageView) mAudioDialog .findViewById(R.id.voice_state); } else if (!mAudioDialog.isShowing()) { mAudioDialog.show(); } mVoiceState.setImageResource(R.drawable.message_audio); AnimationDrawable drawable = (AnimationDrawable) mVoiceState .getDrawable(); drawable.start(); } private void hideAudioDialog() { if (mAudioDialog != null && mAudioDialog.isShowing()) { mAudioDialog.dismiss(); } } private void changeAudioDialogCancel(boolean cancel) { if (mCancelSend == cancel) return; if (mAudioDialog != null && mAudioDialog.isShowing() && mAudioNotify != null) { if (cancel) { mVoiceState.setImageResource(R.drawable.message_voice_quit); mAudioNotify.setTextColor(Color.RED); mAudioNotify.setText(R.string.fingers); } else { mAudioNotify.setTextColor(Color.WHITE); mAudioNotify.setText(R.string.fingers_slide_to); mVoiceState.setImageResource(R.drawable.message_audio); AnimationDrawable drawable = (AnimationDrawable) mVoiceState .getDrawable(); drawable.start(); } } mCancelSend = cancel; } }
主页面布局activity_audiosend.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/green" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="200px" android:padding="20px" android:id="@+id/audio_send" android:gravity="center" android:text="按住 发送"/> </RelativeLayout>
Dialog对话框布局audio_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" android:layout_gravity="center_horizontal" android:background="@drawable/pop_bg"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center_horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/voice_state" android:src="@drawable/message_audio"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/fingers_slide_to" android:textColor="@color/white" android:id="@+id/audio_nofity"/> </LinearLayout>
voiceState讲话动画message_audio.xml
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <item android:drawable="@drawable/message_voice_1" android:duration="200"/> <item android:drawable="@drawable/message_voice_2" android:duration="200"/> <item android:drawable="@drawable/message_voice_3" android:duration="200"/> <item android:drawable="@drawable/message_voice_4" android:duration="200"/> <item android:drawable="@drawable/message_voice_5" android:duration="200"/> <item android:drawable="@drawable/message_voice_6" android:duration="200"/> <item android:drawable="@drawable/message_voice_7" android:duration="200"/> </animation-list>
代码就这么多,是不是很简单啊,哈哈,喜欢请点个赞,谢谢!
相关文章推荐
- Android仿微信发送语音消息的功能及示例代码
- 仿微信发送语音消息的功能
- Android 消息数字提示,类似微信,BadgeView
- Android实现录音方法(仿微信语音、麦克风录音、发送语音、解决5.0以上BUG)
- Android 录音实现方法、仿微信语音、麦克风录音、发送语音
- Android仿微信新消息提示音
- 详解android高仿微信后台接受消息,发送通知,监测网络连接
- 在控件周围动态显示 类似微信消息提示效果
- MessageDisplayKit 一个类似微信App的IM应用,拥有发送文字、图片、语音、视频、地理位置消息,管理本地通信录、分享朋友圈、漂流交友、摇一摇和更多有趣的功能。
- Android 之应用启动图标未读消息BadgeNumber动态提醒(如微信 QQ等)
- C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)
- Android仿微信语音消息的录制和播放功能
- Android实现微信底部的带消息提示数量
- Android 录音实现方法、仿微信语音、麦克风录音、发送语音、解决5.0以上BUG
- Android实现微信底部的带消息提示数量
- C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)
- 微信小程序实现动态设置placeholder提示文字及按钮选中/取消状态的方法
- Android实现微信底部的带消息提示数量
- Android学习开发——用RecyclerView模拟微信的消息发送和接受报错处理
- android 融云 + 科大讯飞 实现仿微信语音消息转换为文字