0

I'm trying to add 'kinetic' move in Y-axis from top of the screen to bottom (and vice versa) to custom layout displayed above all apps using the window manager.

I tried to do it in this way:

private void animateFlingInYAxis(View view) {
        FlingAnimation fling = new FlingAnimation(view, DynamicAnimation.SCROLL_Y);
        fling.setStartValue(0.9F);
        fling.setStartVelocity(0f);
        //fling.setMinValue(0.5F);
        fling.setFriction(0.2F);
        fling.start();
}

But the following result is no working correctly. I tried to change values SCROLL_Y to TRANSLATION_Y and set a custom range of values but without luck.

Action is processed in ACTION_MOVE event in this way:

case MotionEvent.ACTION_MOVE:
                params.y = initialY + (int) (event.getRawY() - initialTouchY);
                if(isMovingInYAxis((int) initialTouchY ,(int) event.getRawY())) {
                    windowManager.updateViewLayout(view, params);
                    animateFlingInYAxis(view);
                }
                break;

I would like to ask how to animate moving in Y-axis in the right way?

Many thanks for any advice.

Edit1: After setting setStartVelocity(2000); seems to be working better, but the view is partially hiding (see screenshot below).

Edit2: I Implemented GestureDetector.OnGestureListener in Custom View, now I am able to catch fling and scroll events. In onFling method, I am able to recognize direction of fling gesture (TOP_BOTTOM, BOTTOM_TOP), but I am not able to to do the animation of view of given direction.

@Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Logger.d("onFling");
        if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            Logger.d("Bottom to top");
            animateFlingInYAxis(view, "BOTTOM_TOP");
            return false; // Bottom to top
        }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            Logger.d("Top to bottom");
            animateFlingInYAxis(view, "TOP_BOTTOM");
            return false; // Top to bottom
        }
        return false;
    }



private void animateFlingInYAxis(View view, String type) {

        FlingAnimation fling = new FlingAnimation(view, DynamicAnimation.TRANSLATION_Y);
        if(type.equals("BOTTOM_TOP")) {
            fling.setStartValue(100); //WHICH PARAMS TO PASS?
            fling.setMinValue(480); //WHICH PARAMS TO PASS?
            fling.setMaxValue(0); //WHICH PARAMS TO PASS?
        } else{
            fling.setStartValue(470);
            fling.setMinValue(480);
            fling.setMaxValue(0);
        }
        fling.setStartVelocity(2000);
        fling.setFriction(0.8F);
        fling.start();

enter image description here

redrom
  • 11,502
  • 31
  • 157
  • 264
  • `setStartVelocity(0)`? it makes little sense to scroll something with zero initial velocity – pskink Feb 01 '18 at 14:08
  • I tried to set several values from 0 to 0.9f, only change I noticed is that with the higher value in setStartVelocity in view partially missing when is moving in Y-axis. – redrom Feb 01 '18 at 14:14
  • give it 2000 for example, not 0.9 – pskink Feb 01 '18 at 14:15
  • Thanks, I updated the question and attached screenshot. – redrom Feb 01 '18 at 14:34
  • good, now read `GestureDetector` documentation and pay attention to `onScroll` and `onFling` methods of `GestureDetector.OnGestureListener` – pskink Feb 01 '18 at 14:38
  • Did you mean that catching the fling event in MotionEvent.ACTION_MOVE is wrong? – redrom Feb 01 '18 at 14:44
  • yes, its wrong, did you read the official `GestureDetector` docs and follow first two steps on how `"To use this class:"`? – pskink Feb 01 '18 at 14:58
  • I implemented Gesture detector and now I am able to catch onScroll and onFling events but I dont't know which params should be passed to get corresponding animation – redrom Feb 01 '18 at 22:20
  • `distanceY` and `velocityY` – pskink Feb 02 '18 at 02:49
  • Could You please add an example passing params from onFling method to FlingAnimation? Value velocityY is clear, but distance no. Should be calculated from rawY? Where to be passed in fling animation? (there is several params). Any example appreciated. – redrom Feb 02 '18 at 07:57

0 Answers0