Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 438
dors Avatar asked Jul 24 '14 22:07

dors


1 Answers

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.

like image 71
Adam Avatar answered Sep 23 '22 09:09

Adam