Android 如何创建类似whatsapp的录制按钮并取消幻灯片
正如在whatsapp中一样,我需要一个重新编码按钮和一张幻灯片来取消和淡入动画,我已经搜索了类似的代码,但没有找到。Android 如何创建类似whatsapp的录制按钮并取消幻灯片,android,whatsapp,Android,Whatsapp,正如在whatsapp中一样,我需要一个重新编码按钮和一张幻灯片来取消和淡入动画,我已经搜索了类似的代码,但没有找到。 我是android编程新手,任何帮助或链接都可能有帮助。我创建了一个github项目。你可以看看它 audioSendButton.setOnTouchListener(新视图.OnTouchListener(){ @凌驾 公共布尔onTouch(视图、运动事件、运动事件){ if(motionEvent.getAction()==motionEvent.ACTION\u向下)
我是android编程新手,任何帮助或链接都可能有帮助。我创建了一个github项目。你可以看看它
audioSendButton.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图、运动事件、运动事件){
if(motionEvent.getAction()==motionEvent.ACTION\u向下){
FrameLayout.LayoutParams参数=(FrameLayout.LayoutParams)slideText
.getLayoutParams();
参数leftMargin=dp(30);
slideText.setLayoutParams(参数);
setAlpha(slideText,1);
startedDraggingX=-1;
//startRecording();
startrecord();
audioSendButton.getParent()
.RequestDisallowWinterCeptTouchEvent(true);
recordPanel.setVisibility(View.VISIBLE);
}else if(motionEvent.getAction()==motionEvent.ACTION\u UP
||motionEvent.getAction()==motionEvent.ACTION\u取消){
startedDraggingX=-1;
停止记录();
//停止记录(真);
}else if(motionEvent.getAction()==motionEvent.ACTION\u MOVE){
float x=motionEvent.getX();
如果(x<-distCanMove){
停止记录();
//停止录音(假);
}
x=x+ViewProxy.getX(audioSendButton);
FrameLayout.LayoutParams参数=(FrameLayout.LayoutParams)slideText
.getLayoutParams();
如果(开始拖动x!=-1){
浮动距离=(x-开始拖动x);
params.leftMargin=dp(30)+(int)dist;
slideText.setLayoutParams(参数);
浮动alpha=1.0f+距离/距离移动;
如果(α>1){
α=1;
}else if(alpha<0){
α=0;
}
setAlpha(slideText,alpha);
}
如果(x dp(30)){
参数leftMargin=dp(30);
slideText.setLayoutParams(参数);
setAlpha(slideText,1);
startedDraggingX=-1;
}
}
view.onTouchEvent(运动事件);
返回true;
}
});
我在whatsapp应用程序中实现了发送按钮,该按钮可以处于发送状态或记录状态。你可以在我的电脑上看一下
用法很简单
<com.gunhansancar.android.animbutton.AnimButton
android:id="@+id/animButton"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="50dp"
android:layout_height="50dp"
app:first="@drawable/ic_mic"
app:second="@drawable/ic_send" />
您只需设置第一个和第二个可绘制。您还必须通过调用goToState()方法来设置状态。您可以使用我创建的库 它很容易设置,而且它模拟了与WhatsApp相同的行为 只需添加视图
RecordView
和RecordButton
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.devlomi.recordview.MainActivity">
<com.devlomi.record_view.RecordView
android:id="@+id/record_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@id/record_button"
app:slide_to_cancel_arrow="@drawable/ic_keyboard_arrow_left"
app:slide_to_cancel_text="Slide To Cancel"
app:slide_to_cancel_margin_right="10dp"/>
<com.devlomi.record_view.RecordButton
android:id="@+id/record_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/bg_mic"
android:scaleType="centerInside"
app:src="@drawable/ic_mic_white"
/>
最后,您可以处理记录状态
- 开始录制时启动
- onCancel刷卡取消时
- onFinish完成录制并返回录制的时间(毫秒)
- 仅秒当记录时间时,您可以在按钮上放置缩放动画并触摸手势以检测用户的移动
在此处签出样本。
此示例还具有类似于whatsapp的删除动画和锁定功能 检查此处的示例代码imageViewAudio.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (isDeleting) { return true; } if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { cancelOffset = (float) (imageViewAudio.getX() / 2.8); lockOffset = (float) (imageViewAudio.getX() / 2.5); if (firstX == 0) { firstX = motionEvent.getRawX(); } if (firstY == 0) { firstY = motionEvent.getRawY(); } startRecord(); } else if (motionEvent.getAction() == MotionEvent.ACTION_UP || motionEvent.getAction() == MotionEvent.ACTION_CANCEL) { if (motionEvent.getAction() == MotionEvent.ACTION_UP) { stopRecording(RecordingBehaviour.RELEASED); } } else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) { if (stopTrackingAction) { return true; } UserBehaviour direction = UserBehaviour.NONE; float motionX = Math.abs(firstX - motionEvent.getRawX()); float motionY = Math.abs(firstY - motionEvent.getRawY()); if (motionX > directionOffset && motionX > directionOffset && lastX < firstX && lastY < firstY) { if (motionX > motionY && lastX < firstX) { direction = UserBehaviour.CANCELING; } else if (motionY > motionX && lastY < firstY) { direction = UserBehaviour.LOCKING; } } else if (motionX > motionY && motionX > directionOffset && lastX < firstX) { direction = UserBehaviour.CANCELING; } else if (motionY > motionX && motionY > directionOffset && lastY < firstY) { direction = UserBehaviour.LOCKING; } if (direction == UserBehaviour.CANCELING) { if (userBehaviour == UserBehaviour.NONE || motionEvent.getRawY() + imageViewAudio.getWidth() / 2 > firstY) { userBehaviour = UserBehaviour.CANCELING; } if (userBehaviour == UserBehaviour.CANCELING) { translateX(-(firstX - motionEvent.getRawX())); } } else if (direction == UserBehaviour.LOCKING) { if (userBehaviour == UserBehaviour.NONE || motionEvent.getRawX() + imageViewAudio.getWidth() / 2 > firstX) { userBehaviour = UserBehaviour.LOCKING; } if (userBehaviour == UserBehaviour.LOCKING) { translateY(-(firstY - motionEvent.getRawY())); } } lastX = motionEvent.getRawX(); lastY = motionEvent.getRawY(); } view.onTouchEvent(motionEvent); return true; } });
imageViewAudio.setOnTouchListener(新视图.OnTouchListener(){ @凌驾 公共布尔onTouch(视图、运动事件、运动事件){ if(isDeleting){ 返回true; } if(motionEvent.getAction()==motionEvent.ACTION\u向下){ cancelOffset=(float)(imageViewAudio.getX()/2.8); lockOffset=(float)(imageViewAudio.getX()/2.5); 如果(firstX==0){ firstX=motionEvent.getRawX(); } if(firstY==0){ firstY=motionEvent.getRawY(); } startRecord(); }else if(motionEvent.getAction()==motionEvent.ACTION\u UP ||motionEvent.getAction()==motionEvent.ACTION\u取消){ if(motionEvent.getAction()==motionEvent.ACTION\u UP){ 停止记录(RecordingBehavior.RELEASED); } }else if(motionEvent.getAction()==motionEvent.ACTION\u MOVE){ if(停止跟踪动作){ 返回true; } userbehavior direction=userbehavior.NONE; float motionX=Math.abs(firstX-motionEvent.getRawX()); float motionY=Math.abs(firstY-motionEvent.getRawY()); 如果(运动X>方向偏移&& motionX>方向偏移&& lastX
motionY&&lastX motionX&&lastY motionY&&motionX>directionOffset&&lastX RecordView recordView = (RecordView) findViewById(R.id.record_view); RecordButton recordButton = (RecordButton) findViewById(R.id.record_button); //IMPORTANT recordButton.setRecordView(recordView);
recordView.setOnRecordListener(this); @Override public void onStart() { //Start Recording.. Log.d("RecordView", "onStart"); } @Override public void onCancel() { //On Swipe To Cancel Log.d("RecordView", "onCancel"); } @Override public void onFinish(long recordTime) { //Stop Recording.. String time = getHumanTimeText(recordTime); Log.d("RecordView", "onFinish"); Log.d("RecordTime", time); } @Override public void onLessThanSecond() { //When the record time is less than One Second Log.d("RecordView", "onLessThanSecond"); }
imageViewAudio.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (isDeleting) { return true; } if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { cancelOffset = (float) (imageViewAudio.getX() / 2.8); lockOffset = (float) (imageViewAudio.getX() / 2.5); if (firstX == 0) { firstX = motionEvent.getRawX(); } if (firstY == 0) { firstY = motionEvent.getRawY(); } startRecord(); } else if (motionEvent.getAction() == MotionEvent.ACTION_UP || motionEvent.getAction() == MotionEvent.ACTION_CANCEL) { if (motionEvent.getAction() == MotionEvent.ACTION_UP) { stopRecording(RecordingBehaviour.RELEASED); } } else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) { if (stopTrackingAction) { return true; } UserBehaviour direction = UserBehaviour.NONE; float motionX = Math.abs(firstX - motionEvent.getRawX()); float motionY = Math.abs(firstY - motionEvent.getRawY()); if (motionX > directionOffset && motionX > directionOffset && lastX < firstX && lastY < firstY) { if (motionX > motionY && lastX < firstX) { direction = UserBehaviour.CANCELING; } else if (motionY > motionX && lastY < firstY) { direction = UserBehaviour.LOCKING; } } else if (motionX > motionY && motionX > directionOffset && lastX < firstX) { direction = UserBehaviour.CANCELING; } else if (motionY > motionX && motionY > directionOffset && lastY < firstY) { direction = UserBehaviour.LOCKING; } if (direction == UserBehaviour.CANCELING) { if (userBehaviour == UserBehaviour.NONE || motionEvent.getRawY() + imageViewAudio.getWidth() / 2 > firstY) { userBehaviour = UserBehaviour.CANCELING; } if (userBehaviour == UserBehaviour.CANCELING) { translateX(-(firstX - motionEvent.getRawX())); } } else if (direction == UserBehaviour.LOCKING) { if (userBehaviour == UserBehaviour.NONE || motionEvent.getRawX() + imageViewAudio.getWidth() / 2 > firstX) { userBehaviour = UserBehaviour.LOCKING; } if (userBehaviour == UserBehaviour.LOCKING) { translateY(-(firstY - motionEvent.getRawY())); } } lastX = motionEvent.getRawX(); lastY = motionEvent.getRawY(); } view.onTouchEvent(motionEvent); return true; } });