4

I am writing an application with an element like facebook chat head that freely moves on the screen using finger. I'm trying to find out the velocity of the view so that I can decide the final location of the view once the ACTION_UP is called. The problem is that the velocity returned by velocity tracker is correct sometimes but wrong half of the times.

Many times, when I try to throw the chat head to the right, it gives a negative x velocity, which is an erratic behaviour. So I went into the developer settings of the phone and activated the pointer location option. Now on the top of the phone, on the top of status bar, the displayed 'Xv' value is always right, but the printed log of the X velocity measured with androidVelocityTracker is still erratic.

Here's my code:

chatHead.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int rawX = (int) event.getRawX();
        int rawY = (int) event.getRawY();
        int pointerIndex = 0;
        int pointerId = event.getPointerId(pointerIndex);

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN
                    //velocity tracker
                    if (mTracker == null) {
                        mTracker = VelocityTracker.obtain();
                    } else {
                        mTracker.clear();
                    }
                    mTracker.addMovement(event);
                    return true;

                case MotionEvent.ACTION_UP:
                    Log.d(TAG, "x velocity = " + lastKnownXVelocity);
                    Log.d(TAG, "y velocity = " + lastKnownYVelocity);
                    return true;

                case MotionEvent.ACTION_MOVE:
                    //01. add movement to velocity trakcer
                    mTracker.addMovement(event);
                    mTracker.computeCurrentVelocity(1000);
                    lastKnownXVelocity = VelocityTrackerCompat.getXVelocity(mTracker, pointerId);
                    lastKnownYVelocity = VelocityTrackerCompat.getYVelocity(mTracker, pointerId);
                    //lastKnownXVelocity = mTracker.getXVelocity(); //have also tried this code...it's shows no different results
                    //lastKnownYVelocity = mTracker.getYVelocity();

                    //02. code to drag the circle on pointer move
                    globalParams.x = paramsX;
                    globalParams.y = paramsY;
                    windowManager.updateViewLayout(chatHead, globalParams);
                    return true;

                case MotionEvent.ACTION_CANCEL:
                    // Return a VelocityTracker object back to be re-used by others.
                    mTracker.recycle();
                    break;
            }
            return false;
    }
});

Please let me know if I'm making some mistake or something? Or is there any other way to get the correct velocity.

Thanks in advance!

penduDev
  • 4,743
  • 35
  • 37
  • did you try `GestureDetector`? does it also report wrong velocities from `onFling`? – pskink Nov 02 '16 at 13:44
  • haven't tried that yet...will give it a try...thanks! – penduDev Nov 02 '16 at 14:02
  • `GestureDetector` also returns the exact same values as `VelocityTracker` – penduDev Nov 02 '16 at 14:32
  • try [this](http://pastebin.com/K8ZuQvMk) and check if it reports wrong values – pskink Nov 02 '16 at 14:38
  • When use this, then the measured x and y velocity is always zero...whereas the velocity in the android debugging tool has some velocity value – penduDev Nov 02 '16 at 14:46
  • try a "fling" like gesture – pskink Nov 02 '16 at 14:50
  • do you mean trying a custom velocity detector? – penduDev Nov 02 '16 at 14:52
  • use my code and see the logcat – pskink Nov 02 '16 at 14:53
  • the log prints this: *onTouchEvent 0.0 0.0* – penduDev Nov 02 '16 at 15:01
  • then try a "fling" like gesture: move your finger and while moving lift it up – pskink Nov 02 '16 at 15:06
  • no matter how i fling, if I add the `MOVE_UP` event to the tracker, it always gives the velocity 0...let me share some working code with you – penduDev Nov 02 '16 at 15:10
  • did you trty my exact code with no modification? if i use it i got: `D onTouchEvent -1074.1545 -1970.6707 D onTouchEvent 461.67484 -2441.9404 D onTouchEvent 133.6362 -386.51538` and stuff like that – pskink Nov 02 '16 at 15:24
  • yes, if I extend my view like your code, then it's showing me these values...let me work on it and I will get back to you, if problem is not solved – penduDev Nov 02 '16 at 15:43
  • i found out what's causing the problem but can't figure out the solution, so when I add this line in the `ON_MOVE` : `windowManager.updateViewLayout(chatHeadCircle, updatedLayoutParams)` to enable the circle drag, it starts showing wrong velocities....if I comment out this line, then the velocities are correctly measured – penduDev Nov 02 '16 at 16:09
  • @penduDev Did you solve this issue? I met the same problem, and I use the GestureDetector which is also use the VelocityTracker internal. – codezjx May 23 '17 at 09:31
  • @codezjx I can't seem to recall and don't have the code now. But somehow I managed to make it work. I recently got to know about SurfaceView, maybe it can help. – penduDev May 23 '17 at 09:50
  • I also have this issue with my velocitytracker returning a wrong Y Velocity when I fling my chat head (which uses windowManager.updateViewLayout). – geecko Jun 29 '17 at 02:58
  • @geecko you should try the `SurfaceView`. It separates the ui drawing part from the velocity calculations which should solve the problem. Do let me know how it goes. – penduDev Jul 01 '17 at 01:31
  • @penduDev I ended up solving it by using .setLocation() and .getRawX() & .getRawY() on the motionEvent that was feeding the velocityTracker. I will however look into SurfaceView for something else. Thanks for the advice. – geecko Jul 01 '17 at 02:22

0 Answers0