问题
I'm using Matrix in my app to zoom an drag, i use a custom ImageView
MY PROBLEM:
I cant control the limits of drag(zoom max and min is done)
I try solve this, looking for the values of the matrix depending the zoom but is not the better way because it change depending the device and I just can control the zoom between 0.9 and 1.7 and manually
I need a better way to control the corners.
My problem is with values[2] and values[5] of the matrix
I appreciate some help, thanks! :D
Now the code
There are the 2 methods i use now for this:
This method chech the zoom of the matrix(values[0]and values[4]) and send custom parameters to limitCorners(float float)
public void checkZoom(){
float[] values = new float[9];
matrix.getValues(values);
//compruebo el zoom
float scaleX = values[Matrix.MSCALE_X];
float scaleY = values[Matrix.MSCALE_Y];
if(scaleX > MAX_ZOOM) {
scaleX = MAX_ZOOM;
} else if(scaleX < MIN_ZOOM) {
scaleX = MIN_ZOOM;
}
if(scaleY > MAX_ZOOM) {
scaleY = MAX_ZOOM;
} else if(scaleY < MIN_ZOOM) {
scaleY = MIN_ZOOM;
}
values[Matrix.MSCALE_X] = scaleX;
values[Matrix.MSCALE_Y] = scaleY;
matrix.setValues(values);
//Second part: depend zoom send values
//a limitaBordes(float, float)
valores = new float[9];
matrix.getValues(valores);
if (valores[0]<=MIN_ZOOM){
valores[0]=MIN_ZOOM;
valores[4]=MIN_ZOOM;
valores[2]=0;
valores[5]=0;
matrix.setValues(valores);
//imageView.setImageMatrix(matrix);
}else if(valores[0]>0.9 && valores[0]<=1){
limitCorners(-65, -40);
}else if(valores[0]>1 && valores[0]<=1.1){
limitCorners(-166, -123);
}else if(valores[0]>1.1 && valores[0]<=1.2){
limitCorners(-246, -201);
}else if(valores[0]>1.2 && valores[0]<=1.3){
limitCorners(-316, -280);
}else if(valores[0]>1.3 && valores[0]<=1.4){
limitCorners(-409, -359);
}else if(valores[0]>1.4 && valores[0]<=1.5){
limitCorners(-484, -435);
}else if(valores[0]>1.5 && valores[0]<=1.6){
limitCorners(-563, -521);
}else {// (valores[0]>1.6f)
limitCorners(-664, -600);
}
}
>
void limitCorners(float valorX, float valorY){
//log("determinando");
//if(x>0)
if(valores[2]>0){
valores[2]=0;
matrix.setValues(valores);
}//if(y>0)
if(valores[5]>0){
valores[5]=0;
matrix.setValues(valores);
}//if(x<valorX)
if(valores[2]< valorX){
valores[2]= valorX;
matrix.setValues(valores);
}//if(y<valorY)
if(valores[5]< valorY){
valores[5]= valorY;
matrix.setValues(valores);
}
imageView.setImageMatrix(matrix);
}
回答1:
I solve it!
I put here the code if somebody have the same problem:
public void checkZoom(){
float[] values = new float[9];
matrix.getValues(values);
float scaleX = values[Matrix.MSCALE_X];
if(scaleX > MAX_ZOOM) {
setZoom(MAX_ZOOM);
}
else if(scaleX < MIN_ZOOM) {
setZoom(MIN_ZOOM);
}
limitCorners();
}
private void limitCorners() {
viewWidth = this.getWidth();
viewHeight = this.getHeight();
float []m = new float[9];
matrix.getValues(m);
float transX = m[Matrix.MTRANS_X];
float transY = m[Matrix.MTRANS_Y];
float fixTransX = getFixTrans(transX, viewWidth, getImageWidth());
float fixTransY = getFixTrans(transY, viewHeight, getImageHeight());
if (fixTransX != 0 || fixTransY != 0) {
matrix.postTranslate(fixTransX, fixTransY);
}
matrix.getValues(m);
if (getImageWidth() < viewWidth) {
m[Matrix.MTRANS_X] = (viewWidth - getImageWidth()) / 2;
}
if (getImageHeight() < viewHeight) {
m[Matrix.MTRANS_Y] = (viewHeight - getImageHeight()) / 2;
}
matrix.setValues(m);
}
private float getFixTrans(float trans, float viewSize, float contentSize) {
float minTrans, maxTrans;
if (contentSize <= viewSize) {
minTrans = 0;
maxTrans = viewSize - contentSize;
} else {
minTrans = viewSize - contentSize;
maxTrans = 0;
}
if (trans < minTrans)
return -trans + minTrans;
if (trans > maxTrans)
return -trans + maxTrans;
return 0;
}
/** Get the width of image (bitmapWidth*Zoom)*/
float getImageWidth(){
float []m = new float[9];
matrix.getValues(m);
return m[Matrix.MSCALE_X]*bitmap.getWidth();
}
/** Get the Height of image (bitmapHeight*Zoom)*/
float getImageHeight(){
float []m = new float[9];
matrix.getValues(m);
return m[Matrix.MSCALE_X]*bitmap.getHeight();
}
来源:https://stackoverflow.com/questions/26506743/how-to-control-the-drag-of-the-imageview-using-matrix