Slide to cancel animation in Android

I want the slide to cancel the animation as whatsapp in my application.

First, when I hold the record button, it will start recording. This part works well.

Secondly, when I move my finger in the opposite direction to the garbage substance while it is already tapping, then the “Text slide to cancel” should be moved back and the recording should pause. After a certain point, I will run the trash to open the animation. But if when moving backward, if I move forward, the text view should be set to its original position, and the recording should be played again.

My problem: I do not get any help on how to accurately display the text.

I also got help from here https://github.com/sarathnk/Audio , but I could not achieve the desired result.

This is my Java code:

holdtoRecord = (ImageView)  findViewById(R.id.hold);
slider = (TextView) findViewById(R.id.slide);

holdtoRecord.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    // start recording.
                    mVideoView.pause();
                    onHold();

                    return true;

                }

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    // Stop recording and save file


                    mVideoView.start();
                    offHold();

                    return true;
                }


                if (event.getAction() == MotionEvent.ACTION_MOVE) {


                }

enter image description here

+4
source share
1 answer

Try with this code, it will work.

    int xPos_terminal = 100;
    int xPos_DragInitial = 0;


hold.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {


                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    // start recording.

                    xPos_DragInitial = (int) motionEvent.getX();
                    xPos_SliderOriginal = (int) slider.getX();

                    int xPos_Trash = (int) trash_cap.getX();

                    Log.e("original", String.valueOf(xPos_Trash));
                    return true;

                }

                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    return true;

                }



                if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {


                     int xPos_current = (int) motionEvent.getX();


                    //Handling of SlideToCancel View
                    String slideToViewMsg = "";
                    if(xPos_current < xPos_DragInitial)
                    {
                        //LEFT DIRECTION
                        Log.e("----- LEFT DIRECTION --","S");
                        if(xPos_SliderOriginal > (xPos_terminal))
                        {
                            slideToViewMsg = "Animate SlideToCancel view to Reduce its X";
                             lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                            int newXOfSlider = (int) slider.getX();
                            newXOfSlider -= 2;
                            lp.setMargins(newXOfSlider, (int) slider.getY(), 0, 0);
                            slider.setLayoutParams(lp);
                        }
                        else
                        {
                            slideToViewMsg = "Donot Animate SlideToCancel view";
                        }
                    }
                    else
                    {
                        //RIGHT DIRECTION
                        Log.e("---- RIGHT DIRECTION --","r");
                        if((int)slider.getX() < xPos_SliderOriginal)
                        {
                            slideToViewMsg = "Animate SlideToCancel view to increase X";
                            int newXOfSlider = (int) slider.getX();
                            newXOfSlider += 2;
                            lp.setMargins(newXOfSlider, (int) slider.getY(), 0, 0);
                            slider.setLayoutParams(lp);
                        }
                        else
                        {
                            slideToViewMsg = "Donot Animate SlideToCancel View";
                        }
                    }

                    Log.e("x",slideToViewMsg);
                    //Handling of Delet button  
                    if (xPos_current < (xPos_terminal))
                    {
                        //Animate Delete button to open


                    }
                    else
                    {
                        //Animate Delet Button To Close

                    }

                    //chnage xPosInitial to xPosCurrent to get actual direction
                    xPos_DragInitial = xPos_current;
                    return true;

                }

                return false;
            }
        });
    }
+3
source

Source: https://habr.com/ru/post/1653503/


All Articles