我在我的应用中使用Matrix来缩放和拖动ImageView。
要控制滚动和缩放的限制,我使用how-to-control-the-drag-of-the-imageview-using-matrix
中公开的方法一切正常,直到我将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);
}
Resultados:
============================================Translación:transX ,transY:0.0,221.22856 Sesgado:skewX,skewY:0.0,0.0 escala:1.0285715 angulo:0.0Tamañodelitmap(w,h):720,415Tamaño del ImageView(w,h):720,858
============================================Translación:transX ,transY:562.49414,70.97687 Sesgado:skewX,skewY: -1.0285715,-1.0285715 escala:1.0285715 angulo:-90.0Tamañodelitmap(w,h):720,415TamañodelImageView(w,h):720,858
============================================Translación:transX ,transY:722.0161,655.5801 Sesgado:skewX,skewY:-0.0,-0.0 escala:1.0285715 angulo:-180.0Tamañodelitmap(w,h):720,415 TamañodelImageView(w,h):720,858
============================================Translación:transX ,transY:167.56201,791.09814 Sesgado:skewX,skewY: 1.0285715,1.0285715 escala:1.0285715 angulo:90.0Tamañodelitmap(w,h):720,415TamañodelImageView(w,h):720,858
答案 0 :(得分:0)
别担心,我找到了解决方案。我不了解Matrix对象的内部工作原理,但它有效。
转弯时,坐标轴会以这种方式修改:
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;
当然,此代码适用于直角,这是我需要的。