How to resize a view by dragging just one side without moving any other

27 Views Asked by At

With this code I have an ImageView that I use as an anchor to resize a view by dragging the left side of that view. How ever when I do it it changes the coordinates on the right side too. I mean in stead of moving just the left side to left it moves also the right side to right. The thing I need is when I drag one side only this side to move and the other to stay on its places.

public class TopLeftAnchorTouchListener implements View.OnTouchListener {
private int _xDelta;
private int actualWidth;
private View viewToResize;



public TopLeftAnchorTouchListener(View viewToResize) {
    this.viewToResize = viewToResize;

}


private int initialWidth;
private int initialX;



@Override
public boolean onTouch(View view, MotionEvent event) {
    final int X = (int) event.getRawX();
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) viewToResize.getLayoutParams();

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            
            // Capture initial conditions of the view to resize.
            initialWidth = viewToResize.getWidth();
            // Capture initial touch point.
            initialX = X;

            break;
            
        case MotionEvent.ACTION_MOVE:
            // Compute how far we have moved in the X direction.
            _xDelta = X - initialX;
            // Adjust the size of the targeted view. Note that we don't have to position
            // the resize handle since it will be positioned correctly due to the layout.
            actualWidth = lp.width = initialWidth - _xDelta;
            viewToResize.setLayoutParams(lp);

            break;
    }

    return true;
}}
0

There are 0 best solutions below