在后旋转后使用矩阵控制图像视图的拖动



我在我的应用程序中使用Matrix在ImageView中进行缩放和拖动。

为了控制滚动和缩放的限制,我使用了如何控制图像视图使用矩阵的拖动中公开的方法

一切正常,直到我将PostRotate方法应用于ImageView Matrix对象。

为了了解正在发生的事情,我执行了以下过程: 我在 ImageView 中放置一个图像,然后将其旋转 90º,在每次操作后执行数据转储。

我不明白从 Matrix 对象获得的值,因此我不知道如何调整代码以便在旋转位图时可以应用它。

看:

private void DumpDatosEnMatrix(){
int viewWidth = imagenView.getWidth();
int viewHeight = imagenView.getHeight();
float []m = new float[9];
matrix.getValues(m);
// translation is simple
float tx = m[Matrix.MTRANS_X];
float ty = m[Matrix.MTRANS_Y];
Log.d(TAG,"============================================");
Log.d(TAG,"Translación: transX,transY: "+tx+","+ty);
Log.d(TAG,"Sesgado: skewX,skewY: "+m[Matrix.MSKEW_X]+","+m[Matrix.MSKEW_X]);
// calculate real scale
float scalex = m[Matrix.MSCALE_X];
float skewy = m[Matrix.MSKEW_Y];
float rScale = (float) Math.sqrt(scalex * scalex + skewy * skewy);
Log.d(TAG,"escala: "+rScale);
// calculate the degree of rotation
float rAngle = Math.round(Math.atan2(m[Matrix.MSKEW_X], m[Matrix.MSCALE_X]) * (180 / Math.PI));
Log.d(TAG,"angulo: "+rAngle);
int imageWidth=(int)(bitmap.getWidth()*rScale);
int imageHeight=(int)(bitmap.getHeight()* rScale);
Log.d(TAG,"Tamaño del bitmap(w,h) :"+imageWidth+","+imageHeight);
Log.d(TAG,"Tamaño del ImageView(w,h) :"+viewWidth+","+viewHeight);
}

结果:

==============================================================================================================================================================================================================================================================================

== 埃斯卡拉: 1.0285715 安古洛: 0.0 塔马尼奥德尔位图(宽,高) :720,415 塔马尼奥 德尔图像视图(宽,高) :720,858

======================================================================================================================================================================================================================================================================= -1.0285715,-1.0285715 埃斯卡拉: 1.0285715 安古洛: -90.0 塔马尼奥德尔位图(宽,高) :720,415 塔马尼奥德尔图像视图(宽,小时) :720,858

================================================================================================================================================================================================================================================================================================== 埃斯卡拉: 1.0285715 安古洛: -180.0 塔马尼奥德尔位图(宽,高) :720,415 Tamaño del ImageView(w,h) :720,858

========================================================================================================================================================================================================================================================================== 1.0285715,1.0285715 埃斯卡拉: 1.0285715 安古洛: 90.0 塔马尼奥德尔位图(宽,高) :720,415 塔马尼奥德尔图像视图(宽,高) :720,858

别担心,我找到了解决方案。我不了解矩阵对象的内部工作原理,但它有效。

转动时,坐标轴按以下方式修改:

float []m = new float[9];
matrix.getValues(m);
// calculate the degree of rotation
float rAngle = Math.round(Math.atan2(m[Matrix.MSKEW_X], m[Matrix.MSCALE_X]) * (180 / Math.PI));
boolean aLoAlto=(Math.abs(rAngle)==90f)?true:false;
// calculate real scale
float scalex = m[Matrix.MSCALE_X];
float skewy = m[Matrix.MSKEW_Y];
float rScale = (float) Math.sqrt(scalex * scalex + skewy * skewy);
int imageWidth = (int) (bitmap.getWidth() * rScale);
int imageHeight = (int) (bitmap.getHeight() * rScale);
if (aLoAlto) {
imageHeight = (int) (bitmap.getWidth() * rScale);
imageWidth = (int) (bitmap.getHeight() * rScale);
}

float transX = m[Matrix.MTRANS_X];
float transY = m[Matrix.MTRANS_Y];
nt xAxis=0,yAxis=0;
switch ((int)rAngle){
case 90:
yAxis=imageHeight;
break;
case 180:
case -180:
yAxis=imageHeight;
xAxis=imageWidth;
break;
case -90:
xAxis=imageWidth;
break;
}

对于计算,只需从该代码绕过轴到点 (0,0)

即可
transX-=xAxis;
transY-=yAxis;

当然,这段代码是用于直角的,这正是我所需要的。

最新更新