Android - view moves out of screen after rotation

Hi I am trying to implement Translate, Scale and Rotate on View (FrameLayout) in android.

In a nutshell, I have a Fresco SimpleDraweeView inside a FrameLayout as Fresco does not support Matrix transformations since I put the alternative in FrameLayout and do Translation, Rotation and Scaling.

I've added FrameLayout here.

public class InteractiveFrameLayout extends FrameLayout {

private ViewTransformer mViewTransformer;

public InteractiveFrameLayout(Context context) {
    super(context);
    init(context);
}

public InteractiveFrameLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public InteractiveFrameLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

private void init(Context context) {
    // Determine dimensions of 'earth' image
    int baseViewWidth = (int) getResources().getDimension(R.dimen.animation_image_size);
    int baseViewHeight = (int) getResources().getDimension(R.dimen.animation_image_size);

    // Setup Gesture Detectors
    mViewTransformer = new ViewTransformer(this, baseViewWidth, baseViewHeight);
}

public boolean onTouchEvent(MotionEvent event) {
    return mViewTransformer.onTouchEvent(event) || super.onTouchEvent(event);
}

      

}

The below class takes the view and does everything related to ViewTransformations.

public class ViewTransformer {

private View mView;

private Vector2D position;
private float scale = 1;
private float angle = 0;

private TouchManager touchManager = new TouchManager(2);

public ViewTransformer(View view, int viewWidth, int viewHeight) {
    mView = view;

    position = new Vector2D();
    position.set(viewWidth / 2, viewHeight / 2);
}

public boolean onTouchEvent(MotionEvent event) {
    try {
        touchManager.update(event);

        if (touchManager.getPressCount() == 1) {
            position.add(touchManager.moveDelta(0));
            ViewAffineOperation.moveViewTo(mView, position.getX(), position.getY());
        }
        else {
            if (touchManager.getPressCount() == 2) {
                Vector2D current = touchManager.getVector(0, 1);
                Vector2D previous = touchManager.getPreviousVector(0, 1);
                float currentDistance = current.getLength();
                float previousDistance = previous.getLength();

                if (previousDistance != 0) {
                    scale *= currentDistance / previousDistance;
                    ViewAffineOperation.scaleViewBy(mView, scale);
                }

                angle -= Vector2D.getSignedAngleBetween(current, previous);
                ViewAffineOperation.rotateViewBy(mView, getDegreesFromRadians(angle));
            }
        }

        mView.invalidate();
    }
    catch(Throwable t) {
        // So lazy...
    }
    return true;
}

private static float getDegreesFromRadians(float angle) {
    return (float)(angle * 180.0 / Math.PI);
}

public float getScale() {
    return scale;
}

public float getRotationDegrees() {
    return angle;
}

      

}

and this one

public class ViewAffineOperation {

public static void moveViewTo(View view, float focusX, float focusY) {

    ViewGroup.LayoutParams layoutParams = view.getLayoutParams();

    int midPointX = layoutParams.width >> 1;
    int midPointY = layoutParams.height >> 1;
    float dx = (focusX - midPointX);
    float dy = (focusY - midPointY);

    view.setTranslationX(view.getTranslationX() + dx);
    view.setTranslationY(view.getTranslationY() + dy);
}

public static void scaleViewBy(View view, float scaleFactor) {
    view.setScaleX(scaleFactor);
    view.setScaleY(scaleFactor);
}

public static void rotateViewBy(View view, float degrees) {
    view.setRotation(degrees);
}

      

}

The key problem is in

view.setTranslationX(view.getTranslationX() + dx);
view.setTranslationY(view.getTranslationY() + dy);

      

This is the most promising thing I've found on the stack above the thread. Rotate and scale a view based on a single handle in Android

Thanks for the ton at Advance.

@Sasha Salauyou I am trying to reach you to help me find a solution.

+3


source to share





All Articles