2012-05-04 37 views
1

每一個的圖像比例。 我是Android開發人員。 我想用矩陣從圖像的顯示部分的中心縮放圖像。 因此,我用矩陣縮放了我的圖像。然後用計算的指針移動它。 但是,應用程序無法正常工作。 這找不到正確的中心,所以當它移動時,它向右移動。 這是爲什麼? 我找不到問題。從顯示中心使用矩陣

代碼如下。

  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的? 誰能爲此解釋? 請幫幫我。

回答

2

或者,這是我的錯。 首先,我從原始圖像縮放圖像。 所以,圖像是(寬度,高度)*比例; 然後我計算顯示中心點的絕對位置。然後,將我的ImageView移動到視圖所在的計算位置。我的錯在這裏。 當我計算視圖位置時,我從現在的比例改變位置。 所以,當它縮放時,位置不是<original position> * <now scale>。這是<original position * <scale> * <now scale>,結果是奇怪的位置。 所以我重拍添加來計算從原來的中心位置。

現在模式如下。

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

謝謝。每一個。