在旧矩阵android中应用旋转变换 [英] apply rotate transformation in old matrix android
本文介绍了在旧矩阵android中应用旋转变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用于转换图像的矩阵。旧矩阵是尺度和平移的函数。
现在如何在旧矩阵中从中心应用旋转而不影响比例和平移等其他转换。
I have a matrix for transforming an image. Old matrix is a function of a scale and translation. Now how to apply rotation from center in old Matrix without affecting the other transformation like scale and translation.
我已经尝试过这个
//get old matrix
Matrix matrix = getImageMatrix();
float tx = getMatrixValue(matrix, Matrix.MTRANS_X);
float ty = getMatrixValue(matrix, Matrix.MTRANS_Y);
float scaleX = getMatrixValue(matrix, Matrix.MSCALE_X);
float scaleY = getMatrixValue(matrix, Matrix.MSCALE_Y);
float skewX = getMatrixValue(matrix, Matrix.MSKEW_X);
float skewY = getMatrixValue(matrix, Matrix.MSKEW_Y);
//calculating the actual scale
float sx = (float)Math.sqrt((scaleX*scaleX)+(skewY*skewY));
float sy = (float)Math.sqrt((scaleY*scaleY)+(skewX*skewX));
//calculating the rotateAngle
float rAngle = Math.round(Math.atan2(scaleX, skewX) * (180 / Math.PI));
//calculate the actual width and height of image
float width = sx * drawable.getIntrinsicWidth();
float height = sy * drawable.getIntrinsicHeight();
//calculate the center pivot for rotation
float cx = (width/2)+tx;
float cy = (height/2)+ty;
//Applying Rotation from center pivot
matrix.postTranslate(-cx , -cy);
matrix.postRotate(rotateAngle, (width/2)+tx, (height/2)+ty);
matrix.postTranslate(cx, cy);
setImageMatrix(matrix);
invalidate();
我没有得到轮换所需的结果。它改变了翻译。我在这做了什么错...?
i didn't get the desire result of rotation. It makes change in translation. what's wrong i did in this..?
查看完整代码(第220行到第220行)
http://pastebin.com/NWrNw0Nd
Have a look on full code (line no 220 to onwards) http://pastebin.com/NWrNw0Nd
推荐答案
这就是你的做法。短而甜。
This is how you do it. Short and sweet.
private RectF rect = new RectF(); // only allocate once
public void rotate(int rotateAngle) {
if (rotateAngle % 90 != 0) {
throw new IllegalArgumentException("angle must be a multiple of 90 degrees");
}
Matrix matrix = getImageMatrix();
// get the original dimensions of the drawable
rect.set(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
// calculate where the current matrix has put the image
matrix.mapRect(rect); // rect now has updated coordinates
// rotate pivoting on the center point
matrix.postRotate(rotateAngle, rect.centerX(), rect.centerY());
setImageMatrix(matrix); // i think setImageMatrix() will call invalidate() itself
}
这篇关于在旧矩阵android中应用旋转变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文