0

I am working on a sample application which allows resizing of view on dragging the selection knobs. Resizing works fine when the view is not rotated. When the view is rotated, if we resize the view, it gets offset by a certain amount. I need to make the opposite corner of the view stay in its original position while the view is being resized. Could anyone help me in understanding how this can be achieved in android. The code snippet for resize is given below:

public void resize(float dx, float dy, SelectionView.KnobTypes selectedKnob) {
    float newDx = dx;
    float newDy = dy;
    FrameLayout.LayoutParams params;
    float viewWidth, viewHeight, ratio, newWidth, newHeight;
    viewWidth = getWidth();
    viewHeight = getHeight();
    ratio = 1;
    switch (selectedKnob) {
        case eLeftTop:
            newWidth = viewWidth - dx;
            newHeight = viewHeight - dy;

            if (newWidth <= mKnobRadius * 4 || newHeight <= mKnobRadius * 4) {
                break;
            }

            if (viewWidth > viewHeight) {
                if (viewHeight != 0) {
                    ratio = viewWidth / viewHeight;
                }
                newWidth = newHeight * ratio;
            } else {
                if (viewWidth != 0) {
                    ratio = viewHeight / viewWidth;
                }
                newHeight = newWidth * ratio;
            }
            params = new FrameLayout.LayoutParams(Math.round(newWidth),
                    Math.round(newHeight));
            setLayoutParams(params);


            newDx = Math.round(newWidth) - Math.round(viewWidth);
            newDy = Math.round(newHeight) - Math.round(viewHeight);

            setTranslationX(getTranslationX() - newDx);
            setTranslationY(getTranslationY() - newDy);
            break;

        case eTopRight:
            newWidth = viewWidth + dx;
            newHeight = viewHeight - dy;

            if (newWidth <= mKnobRadius * 4 || newHeight <= mKnobRadius * 4) {
                break;
            }

            if (viewWidth > viewHeight) {
                if (viewHeight != 0) {
                    ratio = viewWidth / viewHeight;
                }
                newWidth = newHeight * ratio;
            } else {
                if (viewWidth != 0) {
                    ratio = viewHeight / viewWidth;
                }
                newHeight = newWidth * ratio;
            }
            params = new FrameLayout.LayoutParams(Math.round(newWidth),
                    Math.round(newHeight));
            setLayoutParams(params);
            newDx = Math.round(newWidth) - Math.round(viewWidth);
            newDy = Math.round(newHeight) - Math.round(viewHeight);
            setTranslationY(getTranslationY() - newDy);
            break;

        case eRightBottom:
            newWidth = viewWidth + dx;
            newHeight = viewHeight + dy;

            if (newWidth <= mKnobRadius * 4 || newHeight <= mKnobRadius * 4) {
                break;
            }

            if (viewWidth > viewHeight) {
                if (viewHeight != 0) {
                    ratio = viewWidth / viewHeight;
                }
                newWidth = newHeight * ratio;
            } else {
                if (viewWidth != 0) {
                    ratio = viewHeight / viewWidth;
                }
                newHeight = newWidth * ratio;
            }
            params = new FrameLayout.LayoutParams(Math.round(newWidth),
                    Math.round(newHeight));
            setLayoutParams(params);
            break;

        case eLeftBottom:
            newWidth = viewWidth - dx;
            newHeight = viewHeight + dy;

            if (newWidth <= mKnobRadius * 4 || newHeight <= mKnobRadius * 4) {
                break;
            }

            if (viewWidth > viewHeight) {
                if (viewHeight != 0) {
                    ratio = viewWidth / viewHeight;
                }
                newWidth = newHeight * ratio;
            } else {
                if (viewWidth != 0) {
                    ratio = viewHeight / viewWidth;
                }
                newHeight = newWidth * ratio;
            }
            params = new FrameLayout.LayoutParams(Math.round(newWidth),
                    Math.round(newHeight));
            setLayoutParams(params);
            newDx = Math.round(newWidth) - Math.round(viewWidth);
            newDy = Math.round(newHeight) - Math.round(viewHeight);

            setTranslationX(getTranslationX() - newDx);
            break;

        default:
            break;
    }
}

dx and dy are the offset by which the mouse is moved, selected knob is the knob which is currently being dragged.

The working sample is uploaded to this github repo: https://github.com/vivekrk/resizesample.git

Vivek
  • 680
  • 1
  • 12
  • 24
  • see my answer here http://stackoverflow.com/questions/21633545/android-imageview-scaling-and-translating-issue – pskink Apr 09 '14 at 10:34
  • I need to resize the view when the drag handle is pulled. Also when the drag handle is being pulled, the view does not resize from the center of the view. The opposite knob of the dragged knob is kept constant while resizing. – Vivek Apr 09 '14 at 10:45
  • see my answer here http://stackoverflow.com/questions/21633545/android-imageview-scaling-and-translating-issue – pskink Apr 09 '14 at 10:48

0 Answers0