How to create a whatsapp like recording button with slide to cancel

前端 未结 5 1908
逝去的感伤
逝去的感伤 2020-12-11 02:07

As in whatsapp I need a recoding button and a slide to cancel and fade animation , I have searched for similar code but didn\'t got one. I am new to android programming any

5条回答
  •  一向
    一向 (楼主)
    2020-12-11 02:53

    You can put a scale animation on the button and touch gestures to detect the user's movements..

    Checkout the sample here..
    https://github.com/varunjohn/Audio-Recording-Animation

    This sample also has delete animation and lock feature similar to whatsapp..

    Check Sample code here

    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;
            }
        });
    

提交回复
热议问题