1

I want to be able to drag a view horizontally in a smooth way. I use the following code

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        {
           \\setup before dragging
        }
        break;
        case MotionEvent.ACTION_MOVE:
        {
             v.setX((dx += speedFactor));
        }
        break;
        case MotionEvent.ACTION_UP:
        {
            \\setup after dragging
        }
        return true;
    }
    return true;
}

when I use a small speedFactore < 10 the animation is smooth but the view moves very slowly. When I increase it it doesn't move as smooth as I wish.

How to dragging very smooth and a little bit faster?

Zaid Alyafeai
  • 155
  • 1
  • 13

1 Answers1

3

Typically, you do not have a constant "speedFactor" because dragging means you wish to have the View under your finger, so it moves as fast as your finger.

You store the initial touch position at the start (ACTION_DOWN) and change the position of the view according to the difference of the current and the initial position during ACTION_MOVE-events.

How smooth movement is, depends on the computing power of the device among other things.

mkoe
  • 116
  • 9
  • I am not interested in making the view following my finger because the view fills the screen horizontally. My own issue is making the dragging looks as smooth as possible. – Zaid Alyafeai Jul 20 '15 at 22:00
  • 1
    if the view fills the screen horizontally, then what is moving at all? – mkoe Jul 20 '15 at 22:09
  • I am trying to implement a delete on swipe function. – Zaid Alyafeai Jul 20 '15 at 22:27
  • 1
    Something like the notification drawer? It is still dragging so the above should apply. I can think of two solutions: either create a custom view and implement the drawing and touch-handling by hand, or dynamically change the layout-parameters ([this post](http://stackoverflow.com/questions/6535648/how-can-i-dynamically-set-the-position-of-view-in-android) might be of some help). Clearly you have implemented some solution by yourself, since your issue is not "how" but smootheness. So you should post your code in the question. – mkoe Jul 21 '15 at 05:58
  • 1
    I have implemented dragging in custom views myself, which is pretty smooth even on low-end devices. – mkoe Jul 21 '15 at 05:59
  • I want to create the same effect as in the notification drawer but I am not getting it right. The dragging in the notification drawer is smoother than mine. Does it help to implement a custom view and do the drawing and that stuff? – Zaid Alyafeai Jul 21 '15 at 06:14
  • I would really appreciate it if you post the code of your custom view. – Zaid Alyafeai Jul 21 '15 at 06:15
  • 1
    I have a class that would be too complex to post here. To get started with custom drawing, take a look at the [documentation](http://developer.android.com/training/custom-views/custom-drawing.html). You have to extend the View class and override onDraw() for rendering as well as onTouchEvent() to handle touch input. I cannot tell with certainty that it's smoother than what you do, but I can think of no faster alternative. – mkoe Jul 21 '15 at 06:58