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 .