在Android中滑动以取消动画

时间:2016-09-03 06:25:27

标签: android android-layout animation ontouchlistener ontouchevent

我想在我的应用程序中使用幻灯片取消动画,例如whatsapp。

首先,当我按住录音按钮时,它将开始录音。这部分工作得很好。

其次,当我将手指朝后方向移动到垃圾桶时,已经轻拍,然后"文本视图滑动取消"应向后滑动,录音应暂停。在某一点之后我将执行垃圾桶打开动画。但是如果我向前滑动,如果向前滑动,那么文本视图应该设置在原来的位置,录音应该再次播放。

我的问题:我没有得到任何帮助如何准确地滑动文本视图。

我也从https://github.com/sarathnk/Audio那里得到了帮助,但我无法达到预期的效果。

这是我的Java代码:

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

1 个答案:

答案 0 :(得分:3)

尝试使用此代码即可。

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