如何使用矩阵控制IMAGEVIEW的拖拽

时间:2014-10-22 11:47:40

标签: android matrix imageview zoom

我在我的应用中使用Matrix缩放拖动,我使用自定义ImageView

我的问题:

我无法控制拖动的限制(缩放最大值和最小值已完成)

我尝试解决这个问题,根据变焦寻找矩阵的值,但不是更好的方法,因为它会根据设备而改变,我只能控制0.9到1.7之间的缩放并手动

我需要一种更好的方法来控制角落。

我的问题是矩阵的值[2]和值[5]

感谢您的帮助,谢谢! :d

现在代码

我现在使用的方法有两种:

这种方法取决于矩阵的变焦(值[0]和值[4])和 将自定义参数发送到limitCorners(浮动浮动)

 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);
    }
}

&GT;

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 个答案:

答案 0 :(得分:2)

我解决了!

如果有人遇到同样的问题,我会把代码放在这里:

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();
}