Search code examples
androidmotioneventdensity-independent-pixel

Velocity Tracker accounting for screen pixel density


When using Android's VelocityTracker to track MotionEvents, is it necessary to account for screen density to return the "density independent velocity"?

velocityTracker.computeCurrentVelocity(1); // pixels per milliseconds
float velocityY = velocityTracker.getYVelocity();

Do I have to multiple this velocityY with a screen scale factor so that I can track the same physical gesture velocity across all devices?


Solution

  • For most uses you you might have, you don't need to worry about that.

    Let's think about the case of scrolling a custom view: if the user moves his finger 1cm, it could be 100px or 400px depending in the screen density. Anyway, what you want to move is to move the drawing the same number of pixels, without scaling them.

    One exception could be if you are writing a game and the distance the user drags his finger means the power they are going to use to perform some action. In this case it would probably be fair to adjust for the screen density.

    Summing up: if you want to update the interface, you don't need to worry about it. If what you want to do with this velocity is independent of the screen, you probably want to adjust it.