Get view to animate to its new position using WindowManager.updateViewLayout()
Asked Answered
C

1

8

I add a view to the WindowManager using WindowManager.updateViewLayout().

I then attache an onTouch listener to it, and make it "follow" the user's finger (drag). I do this by changing the x and y values of the WindowManager.LayoutParams I passed in updateViewLayout.

When the user lifts the finger, I want the view to animate to the screen's left/right edge according to where it is closer to. The effect I get is the view sticking to the edge immediately, without any animation.

If the view's width/height change tough (and following that, the width/height of the WindowManager.LayoutParams), the view animates to its new place.

Is there a way to get it to animate after just changing the x/y values (without having the width/height change)?

Conjoined answered 24/7, 2014 at 22:10 Comment(0)
G
6

You can do it like this: run an animator whenever you lift the finger:

// <inside switch case in your touchEvent handling>  
// params variable is your view's params

case MotionEvent.ACTION_UP:
    ValueAnimator va = ValueAnimator.ofFloat(params.x, 0);
    int mDuration = 250;
    va.setDuration(mDuration);
    va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        public void onAnimationUpdate(ValueAnimator animation) {
           params.x = Math.round((Float) animation.getAnimatedValue());
           mWindowManager.updateViewLayout(mOverlayView, params);
        }
    });
    va.start();

This code always animates it to the left edge of the screen as you release your finger. You can sort that out by by keeping track of the view's coordinates and checking against the middle of the screen and then changing the animation target.

Gayomart answered 14/3, 2018 at 11:28 Comment(1)
!!! perfect !!!Puri

© 2022 - 2024 — McMap. All rights reserved.