Android仿微信发送语音消息动态提示,支持上滑取消发送
程序员文章站
2024-03-15 14:18:53
...
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>
代码就这么多,是不是很简单啊,哈哈,喜欢请点个赞,谢谢!