Implementation on Android Canvas

I have a regular gesture detector for fling detection, this is an attribute of a SurfaceView instance

 GestureDetector flingDetector = new GestureDetector(getContext(),new SimpleOnGestureListener() { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // Fling implementation return true; } }); 

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

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

There seem to be a lot of questions on detecting glimpses, my question is how to implement it.

+6
source share
1 answer

I am not sure if this will answer your question, I will try.

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

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

Since fling seems more like a dynamic gesture, you can decide that fling means the movement of the amplifier and applies the gain to dx and dy, so that when the user scrolls, they get the exact movement, but when thrown, the actual movement is amplified.

If this ratio is speed-dependent, you have a custom answer for each user input.

(Another thing is to revive the result, which, I think, will depend on other things).

An example that I could try if it were me:

  • User scrolls gently: Movement equals dx, dy. Convert (Dx, Dy).
  • User sends:
    Real motion: dx = (e2.getX () - e1.getX (). Dy = (e2.getY () - e1.getY (). Displacement coefficient: (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 understand that this question has been since 2012, I hope this helps someone in due time. It would be nice to know about the final implementation anyway!

+4
source

Source: https://habr.com/ru/post/922259/


All Articles