Android Geek Android Geek - 3 months ago 100
Android Question

Slide to cancel Animation in Android

I want a slide to cancel Animation like whatsapp in my App.

Firstly: When I hold on record button it will start recording. This part is working good.

Secondly: When I move the finger in back direction towards trash can while already tapped then Text View Slide to cancel should slide backwards and recording should paused. After a certain point I will perform the trash can open animation. But if While sliding back ward if I slide towards forward then text view should be set at its original position and recording should play again.

My problem: I am not getting any help how to slide text view exactly.Please if any one can help. It's very urgent I am stuck on this from last 5 days.

I have also taken the help from here https://github.com/sarathnk/Audio but I was not able to achieve 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

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