我有一个自定义视图扩展了ImageView,我希望在其中使用矩阵来应用缩放,平移,翻转和旋转等操作。
缩放和平移在ontouch事件中运行良好,并且翻转在onclick中也能正常工作。但旋转不如预期。
从中心翻转
public void flip(String flipmode)
{
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);
Drawable drawable = getDrawable();
if (drawable == null) {
return ;
}
float width = scaleX * drawable.getIntrinsicWidth();
float height = scaleY * drawable.getIntrinsicHeight();
float skewX = getMatrixValue(matrix, Matrix.MSKEW_X);
float rAngle = Math.round(Math.atan2(scaleX, skewX) * (180 / Math.PI));
if (DEBUG) {
Log.e(TAG, "onUp: " + tx + " " + ty);
Log.e(TAG, "scaleX: " + scaleX+" scaleY: " + scaleY);
Log.e(TAG, "drawable size: " + drawable.getIntrinsicWidth() + " " + drawable.getIntrinsicHeight());
Log.e(TAG, "Rotate Angle: " + rAngle);
Log.e(TAG, "x center : " + (width/2)+tx + " y center " + (height/2)+ty);
}
if (flipmode == "ORIENTATION_FLIP_HORIZONTAL") {
matrix.postScale(-1.0f, 1.0f, (width / 2) + tx, (height / 2) + ty);
} else if (flipmode == "ORIENTATION_FLIP_VERTICAL") {
matrix.postScale(1.0f, -1.0f, (width / 2) + tx, (height / 2) + ty);
}
setImageMatrix(matrix);
invalidate();
}
从中心旋转
public void rotate(int rotateAngle)
{
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);
Drawable drawable = getDrawable();
if (drawable == null) {
return ;
}
float sx = (float)Math.sqrt((scaleX*scaleX)+(skewY*skewY));
float sy = (float)Math.sqrt((scaleY*scaleY)+(skewX*skewX));
float width = sx * drawable.getIntrinsicWidth();
float height = sy * drawable.getIntrinsicHeight();
float rAngle = Math.round(Math.atan2(scaleX, skewX) * (180 / Math.PI));
if (DEBUG) {
Log.e(TAG, "onUp: " + tx + " " + ty);
Log.e(TAG, "scaleX: " + scaleX+" scaleY: " + scaleY);
Log.e(TAG, "scaleX new : " + sx+" scaleY new: " + sy);
Log.e(TAG, "drawable size: " + drawable.getIntrinsicWidth() + " " + drawable.getIntrinsicHeight());
Log.e(TAG, "Rotate Angle: " + rAngle);
Log.e(TAG, "x center : " + (width/2)+tx + " y center " + (height/2)+ty);
}
matrix.postRotate(rotateAngle, (width/2)+tx, (height/2)+ty);
setImageMatrix(matrix);
invalidate();
}
我在第一次轮换后看到了非常奇怪的行为。 问题:新坐标不符合预期 记录初始值:
ImageView: onUp: 144.0 0.0
ImageView: scaleX: 0.6 scaleY: 0.6
ImageView: scaleX new : 0.6 scaleY new: 0.6
ImageView: drawable size: 800 1280
ImageView: Rotate Angle: 90.0
ImageView: x center : 240.00002144.0 y center 384.00.0
记录:应用旋转后
ImageView: onUp: 0.0 624.0
ImageView: scaleX: 0.0 scaleY: 0.0
ImageView: scaleX new : 0.6 scaleY new: 0.6
ImageView: drawable size: 800 1280
ImageView: Rotate Angle: 0.0
ImageView: x center : 240.000020.0 y center 384.0624.0
如何正确应用旧矩阵中的旋转..?