0

I have a layout where I am putting a view over it and moving only in horizontal direction. I managed to do so, but the issue is the dragging is not very smooth, it shakes alot and sometimes ends up on wrong coordinates.

Apart from it when I play alot with that view, it disappears from the layout.

Here is mu xml file

 <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="300dip"
    android:id="@+id/graph"
    android:orientation="vertical"
    android:background="#ffffff"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <View
        android:id="@+id/drag_line"
        android:layout_width="50dp"
        android:visibility="visible"
        android:background="@drawable/line"
        android:layout_gravity="center_horizontal|top"
        android:layout_height="170dp"
        android:src="@drawable/line" />

</FrameLayout>

here is my onTouch event handler of view,

  line.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            final int action = motionEvent.getAction();
            final int x = (int) motionEvent.getRawX();
            final int y = (int) motionEvent.getRawY();


            switch (action & MotionEvent.ACTION_MASK) {
             `enter code here`   case MotionEvent.ACTION_MOVE:{
                    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
                    int currentX = (int) motionEvent.getX();
                    params.rightMargin = (params.rightMargin - (currentX));
                    params.leftMargin = (params.leftMargin + (currentX));
                    line.setLayoutParams(params);
                    line.invalidate();
                    break;
                }
                case MotionEvent.ACTION_UP: {

                        dynamicUpdate(line.getX());
                        touchFlag = true;

                }
            }
            return true;
        }
    });

I want to handle a condition on ACTION_Up.

I could not understand why the view is shaking over layout on touch move and sometimes getting disappear.

user3290805
  • 445
  • 2
  • 10
  • 28
  • Don't know if it will be helpful now, but dropping it anyways. You might want to see this question http://stackoverflow.com/questions/16273249/android-ontouchevent-coordinates-skip-around – xip Apr 03 '15 at 14:17

1 Answers1

0

I had same problem,event.getX() is the coordinate of the view. If your view moves, then the coordinate of same point in screen will change, so it shakes. You should use event.getRawX() and event.getRawY()

bish
  • 3,381
  • 9
  • 48
  • 69