I have an ImageView
whose Drawable
can be zoomed in or out. The Drawable
can be dragged inside the ImageView
, but it can be dragged out of the screen so that for example the left border of the Drawable
may be place at the center of the screen. I tried the solution given by Benito Bertoli and it works perfectly for the initial image which is not zoomed. How can I use this solution when the image is zoomed?
Asked
Active
Viewed 188 times
3

Community
- 1
- 1

Mohsen Kamrani
- 7,177
- 5
- 42
- 66
-
see `Matrix#map*` methods – pskink Dec 08 '15 at 11:41
-
@pskink I'm just working on it and trying to make it work somehow. – Mohsen Kamrani Dec 08 '15 at 11:42
-
yes, i know you are working on it, just see those methods – pskink Dec 08 '15 at 11:43
-
@pskink :) I meant you are right, it seems to be a good option and I just found them. – Mohsen Kamrani Dec 08 '15 at 11:45
-
@mok you managed to do this ? – cgr May 01 '17 at 18:22
1 Answers
0
I do this:
if (event.getAction()==MotionEvent.ACTION_POINTER_UP || event.getAction()==MotionEvent.ACTION_UP) {
float[] matrixValues=new float[9];
matrix.getValues(matrixValues);
PointF coordenadas = validatePosition(matrixValues);
if (coordenadas.x!=0 || coordenadas.y!=0){
matrix.postTranslate(coordenadas.x,coordenadas.y);
//view is a imageView
view.setImageMatrix(matrix);
}
}
and this is the methods:
private PointF validatePosition(float[] matrixValues){
SizeF pantalla=getDisplaySize();
SizeF zoomed=getImagenZoomSize(matrixValues[Matrix.MSCALE_X],matrixValues[Matrix.MSCALE_Y]);
//System.out.println("M_TRANS: "+matrixValues[Matrix.M TRANS_X]+","+matrixValues[Matrix.M TRANS_Y]+" Pantalla:"+pantalla.getWidth()+","+pantalla.getHeight()+" Imagen:"+zoomed.getWidth()+","+zoomed.getHeight());
PointF respuesta=new PointF(0,0);
respuesta.set(calculateAbsolutePosition(matrixValues[Matrix.MTRANS_X],zoomed.getWidth(),pantalla.getWidth()),respuesta.y);
respuesta.set(respuesta.x,calculateAbsolutePosition(matrixValues[Matrix.MTRANS_Y],zoomed.getHeight(),pantalla.getHeight()));
return respuesta;
}
private SizeF getDisplaySize(){
Point dimension=new Point();
Display display=getWindowManager().getDefaultDisplay();
display.getSize(dimension);
pantalla=new SizeF(dimension.x,dimension.y);
return pantalla;
}
private SizeF getImagenZoomSize(float scaleX, float scaleY){
SizeF original=getOriginalImageSize();
return new SizeF(original.getWidth()*scaleX,original.getHeight()*scaleY);
}
private float calculateAbsolutePosition(float actual, float medidaImagen, float medidaPantalla){
float max, min;
float desplazamiento;
if (medidaPantalla>medidaImagen){
max=(medidaPantalla-medidaImagen)/2;
min=max;
}
else{
max=0;
min=medidaPantalla-medidaImagen;
}
if (actual<min){
desplazamiento=-actual+min;
}
else if (actual>max){
desplazamiento=-actual+max;
}
else{ //no movemos la imagen (desplazamiento=0)
desplazamiento=0;
}
return desplazamiento;
}

Carlos Gómez
- 357
- 4
- 9