1

每个人。我是一名安卓开发者。我想用矩阵从图像显示部分的中心缩放我的图像。所以,我用矩阵缩放了我的图像。然后用计算出的指针移动它。但是,应用程序无法正常工作。这找不到正确的中心,所以当它找到时,它向右移动。这是为什么?我找不到问题。

代码紧随其后。

            matrix.reset();
            curScale += 0.02f;
            orgImage.getHeight();
            w = orgImage.getWidth();
            matrix.postScale(curScale, curScale);
            rtnBitmap = Bitmap.createBitmap(orgImage, 0, 0, w, h, matrix, true);
            curImageView.setImageBitmap(rtnBitmap);
            Matrix curZoomOutMatrix = new Matrix();

            pointerx =(int ) ((mDisplayWidth/2 - curPosX) * curScale);
            curPosX = - pointerx;
            pointery =(int ) ((mDisplayWidth/2 - curPosY) * curScale);
            curPosY =  - pointery;

            Log.i("ZoomOut-> posX = ", Integer.toString(curPosX));
            Log.i("ZoomOut-> posY = ", Integer.toString(curPosY));
            curZoomOutMatrix.postTranslate(curPosX, curPosY);
            curImageView.setImageMatrix(curZoomOutMatrix);
            curImageView.invalidate();

您是否有任何用于中心 zoomIn 和 zoomOut 矩阵的 imageView 的示例代码?谁能解释一下?请帮我。

4

1 回答 1

2

或者,这是我的错。首先,我从原始图像缩放图像。所以,图像是(宽度,高度)*比例;然后我计算显示中心点的绝对位置。然后,将我的 ImageView 移动到视图所在的计算位置。我的错在这里。当我计算视图位置时,我从现在的比例更改位置。因此,当它缩放时,位置不是<original position> * <now scale>。是<original position * <scale> * <now scale>的,结果是奇怪的位置。所以我重新制作了 add 来计算原始位置的中心位置。

这种模式现在正在遵循。

public void calculate(float offset) {

    float tmpScale = curScale - offset;
    float orgWidth = (mDisplayWidth / 2 - curPosX) / tmpScale;
    float orgHeight = (mDisplayHeight / 2 - curPosY) / tmpScale;
    int tmpPosX = (int)(mDisplayWidth / 2 - orgWidth * curScale);
    int tmpPosY = (int)(mDisplayHeight / 2 - orgHeight * curScale);

    curPosX = tmpPosX;
    curPosY = tmpPosY;

    Matrix matrix = new Matrix();
    matrix.postTranslate(tmpPosX, tmpPosY);

    curImageView.setImageMatrix(matrix);
    curImageView.invalidate();
}

谢谢你。每个人。

于 2012-05-06T05:32:21.760 回答