private void translateMatrixAfterRotate(int axis, float trans, float prevImageSize, float imageSize, int prevViewSize, int viewSize, int drawableSize) {
        if (imageSize < viewSize) {
            //
            // The width/height of image is less than the view's width/height. Center it.
            //
            m[axis] = (viewSize - (drawableSize * m[Matrix.MSCALE_X])) * 0.5f;

        } else if (trans > 0) {
            //
            // The image is larger than the view, but was not before rotation. Center it.
            //
            m[axis] = -((imageSize - viewSize) * 0.5f);

        } else {
            //
            // Find the area of the image which was previously centered in the view. Determine its distance
            // from the left/top side of the view as a fraction of the entire image's width/height. Use that percentage
            // to calculate the trans in the new view width/height.
            //
            float percentage = (Math.abs(trans) + (0.5f * prevViewSize)) / prevImageSize;
            m[axis] = -((percentage * imageSize) - (viewSize * 0.5f));
        }
    }
After rotating, the matrix needs to be translated. This function finds the area of image
which was previously centered and adjusts translations so that is again the center, post-rotation.

@param axis          Matrix.MTRANS_X or Matrix.MTRANS_Y
@param trans         the value of trans in that axis before the rotation
@param prevImageSize the width/height of the image before the rotation
@param imageSize     width/height of the image after rotation
@param prevViewSize  width/height of view before rotation
@param viewSize      width/height of view after rotation
@param drawableSize  width/height of drawable
app/src/main/java/com/rozdoum/socialcomponents/views/TouchImageView.java