Search code examples
androidresizeontouchlistener

Resizing view using onTouchListener


Hi I am creating an application that would simply resize a view when I start dragging a button designated for resizing.

Layout to be resized is declared as: resizeAbleLayout and the button for dragging to resize that layout is declared as: btnResize

I have been able to resize the view but when I finished resizing the view, and start to resize it again it does not start to resize from the size that I left it.

My question is that how would I be able to save the last resized position of the view and smoothly resize it without shinking suddenly? :D

This is my code used for btnResize:

  btnResize.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int x=(int)event.getX();
            int y=(int)event.getY();
                switch (event.getAction()){
                    case MotionEvent.ACTION_MOVE:
                        resizeAbleLayout.getLayoutParams().width = x;
                        resizeAbleLayout.getLayoutParams().height = y;
                        resizeAbleLayout.requestLayout();
                        break;
                }
            return true;
        }
    });

Thanks in advance for any help! :D


Solution

  • try using the += or -= not to use =:

    switch (event.getAction()){
         case MotionEvent.ACTION_MOVE:
         resizeAbleLayout.getLayoutParams().width += x/Math.abs(x);
         resizeAbleLayout.getLayoutParams().height += y/Math.abs(y);
         resizeAbleLayout.requestLayout();
         break;
    }