Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fling implementation on android canvas

I have the usual gesture detector for detecting fling , It is an instance attribute of a SurfaceView

GestureDetector flingDetector = new GestureDetector(getContext(),new SimpleOnGestureListener() {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                    // Fling implementation
            return true;
        }
});

I am drawing a lot of complex stuff on a canvas and I have a translate(dx,dy) method that I use with onScroll.

So my question is how do I implement the fling using the translate method ?

There seem to be a lot of questions on detecting fling , my question is on implementing it .

like image 897
Gautam Avatar asked Aug 07 '12 05:08

Gautam


1 Answers

I am not sure this will answer your question, I'll give it a try.

Check http://developer.android.com/reference/android/view/MotionEvent.html for MotionEvent.

You can use the two events received as e1 and e2 on the onFling method, and calculate coordinate differences with e1.getX(), e2.getX(), e1.getY(), e2.getY().... With this you would have the dx and dy to use with translate(dx,dy).

Since the fling seems more of a dynamic gesture, you could decide that fling means an ampler movement, and apply an amplification factor to dx and dy, so that when the user scrolls, they get a precise movement, but on fling, the actual movement gets amplified.

If this factor depends on velocity, you have a custom response for every user input.

(A different thing would be animating the result, which I guess would depend on other things).

An example I might try if it were me:

  • User scrolls softly: Movement is dx,dy. Translate(dx,dy).
  • User flings:
    Real motion: dx=(e2.getX()-e1.getX(). dy = (e2.getY()-e1.getY(). Fling factor: (Custom implementation). Modified motion: dxModified = dx*velocityX*F. dyModified = dy*velocityY*F. Finally: translate (dxModified,dyModified)

    Hope this helps to some extent.

    Edit: I did not realize this question was from 2012, hopefully this will help someone some time. It would be nice to know about the final implementation anyway!.

like image 78
user3647353 Avatar answered Oct 14 '22 00:10

user3647353