项目中遇到这样的需求,需要动态求出正在编辑的图片的中心点坐标,编辑操作共有三种,平移,缩放和旋转.
其中平移缩放可以按照如下代码正确求出图片左上角和右下角坐标,但是旋转不可以.
/**
*
* @param matrix imageView的矩阵对象
* @param imageView 图片对象
* @return
*/
public float[] getLeftTop(Matrix matrix, GestureCropImageView imageView) {
Rect rectTemp = imageView.getDrawable().getBounds();
float[] values = new float[9];
matrix.getValues(values);
//左上角X坐标
mLeftX = values[2];
//左上角Y坐标
mLeftY = values[5];
//如果旋转的话
if (mState == ROTATE_STATE) {
//x0= (x - rx0)*cos(a) - (y - ry0)*sin(a) + rx0 ;
//
//y0= (x - rx0)*sin(a) + (y - ry0)*cos(a) + ry0 ;
//右下角X坐标
mRightX = (float) ((mRightX - mMidPntX) * Math.cos(mAngle / 180 * Math.PI) - (mRightY - mMidPntY) * Math.sin(mAngle / 180 * Math.PI) + mRightX);
//右下角Y坐标
mRightY = (float) ((mRightX - mMidPntX) * Math.sin(mAngle / 180 * Math.PI) + (mRightY - mMidPntY) * Math.cos(mAngle / 180 * Math.PI) + mRightY);
Log.i(TAG, "宽度 :" + rectTemp.width() * values[0]);
Log.i(TAG, "长度 :" + rectTemp.height() * values[4]);
} else {
//右下角X坐标
mRightX = values[2] + rectTemp.width() * values[0];
//右下角Y坐标
mRightY = values[5] + rectTemp.height() * values[4];
}
float[] arr = {mLeftX, mLeftY, mRightX, mRightY};
return arr;
}
2.现在已知的条件有,图片旋转的角度,图片按某点旋转的点坐标,左上角的坐标,如何才能求出右下角的坐标?
//x0= (x - rx0)cos(a) - (y - ry0)sin(a) + rx0 ;
//y0= (x - rx0)sin(a) + (y - ry0)cos(a) + ry0 ;
这是我的一种思路,但是出来的结果不对.
The problem has been solved, the method is as follows:
There is a simple method, you can try it, use the transform method of the MotionEvent class provided by Android
The specific steps are to first generate a MotionEvent object based on the center coordinates of the image before rotation, then call the transform method, pass the Matrix, and then take it out The corresponding transformed x, y should be the coordinates you want
You can first get the coordinates of the center point before transformation, and then use the Matrix.mappoints method after setting the matrix to get the coordinates of the center point after transformation